Simple C ++ shared memory program written in Linux: segmentation error

#include <stdio.h> #include <sys/shm.h> #include <sys/stat.h> #include <string> #include <vector> #include <iostream> using namespace std; struct LOCK { string name; string type; vector <string> pids; }; int main () { int segment_id; LOCK* shared_memory; struct shmid_ds shmbuffer; int segment_size; const int shared_segment_size = 0x6400; /* Allocate a shared memory segment. */ segment_id = shmget (IPC_PRIVATE, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); /* Attach the shared memory segment. */ shared_memory = (LOCK*) shmat (segment_id, 0, 0); printf ("shared memory attached at address %p\n", shared_memory); /* Determine the segment size. */ shmctl (segment_id, IPC_STAT, &shmbuffer); segment_size = shmbuffer.shm_segsz; printf ("segment size: %d\n", segment_size); /* Write a string to the shared memory segment. */ //sprintf (shared_memory, "Hello, world."); shared_memory -> name = "task 1"; shared_memory -> type = "read"; (shared_memory -> pids).push_back("12345"); (shared_memory -> pids).push_back("67890"); /* Detach the shared memory segment. */ shmdt (shared_memory); /* Reattach the shared memory segment, at a different address. */ shared_memory = (LOCK*) shmat (segment_id, (void*) 0x5000000, 0); printf ("shared memory reattached at address %p\n", shared_memory); /* Print out the string from shared memory. */ //printf ("%s\n", shared_memory -> name); cout << "Name of the shared memory: " + shared_memory -> name << endl; /* Detach the shared memory segment. */ shmdt (shared_memory); /* Deallocate the shared memory segment. */ shmctl (segment_id, IPC_RMID, 0); return 0; } 

I got the code from a shared memory tutorial. It worked until I defined struct LOCK and tried to write LOCK instead of char * into shared memory.

Can someone please help me deal with the problem here that is causing the segmentation error?

+6
source share
3 answers

You put vector and string in shared memory. Both of these classes allocate their own memory, which will be allocated in the address space of any process that generates the distribution, and will issue segfault when accessed from another process. You can try specifying allocators to use this shared memory, but since C ++ 03 assumes allocators are stateless, I'm not sure if this will be possible.

See how Boost.Interprocess works.

+10
source

You have a number of problems. The obvious thing is that you are not creating your object. In the opaque form you are currently doing:

 class Foo; Foo * p = get_memory(); p->bar = 5; // ouch! 

What you should do at least:

 void * addr = get_memory(sizeof(Foo)); Foo * p = ::new (addr) Foo; // do work p->~Foo(); // done 

(Just replace Foo with LOCK for your situation.)

However, it gets complicated: vector and string require dynamic allocations. This memory must be in the same address space as your LOCK . So the standard way to solve this is to write your own dispenser and pass this:

 template <template <typename> class Alloc> struct Lock { typedef std::basic_string<char, std::char_traits<char>, Alloc<char>> shared_string; shared_string name; shared_string type; std::vector<shared_string, Alloc<shared_string>> pids; }; 

Finally, you need to write an appropriate allocator class that puts the memory in the same address space as the one in which your LOCK object will eventually go:

 template <typename T> class shared_allocator { /* write this! */ } typedef Lock<shared_allocator> LOCK; 
+1
source

I know that this is a very long time. But I was looking for a way to (remember) about the general mem, and this topic is interesting.

My two cents for being in demand:

  • Please note that reading and writing to SharedMemory is exactly the same as reading and writing to FileSystem.
  • Shared memory identifier == file_handle from open (...);

    So ... How do you CORRECTly serialize and then read-write std :: string or even std :: vector in FILE? Are you really extending / contracting std :: string or std :: vector from FILE?

Thanks:)

0
source

Source: https://habr.com/ru/post/901849/


All Articles