I am having a Design/Implementation issue with Boost managed_shared_memory
.
I have two processes: Writer
and Reader
.
Here Writer
is a C++ executable which is using boost to write in shared memory. Code snapshot for writing method:
shm_controller.reset(new boost::interprocess::managed_shared_memory( boost::interprocess::open_or_create, shmName, size));
void * addr = shm_controller->allocate(size) ;
shm_controller->deallocate(addr);
Once I have addr pointer, I am using memcpy
to write in SHM.
On other hand I am having Reader
, which is C executable. They both are communicating via message passing application. I wrote Reader
code in such way:
Writer
is passing address, which is stored in "addr" (from above code snapshot) to Reader
.Reader
receive the address and size, I am using memcpy
to write SHM data from Reader
's local variable.Note 1: (This
Reader
is not using any boost shm API, as it's C application )
Note 2: I validated address is same onWriter
andReader
(after communication).
But Reader
's memcpy
to read shm data is leading crash in my executable (Reader
).
boost::interprocess::managed_shared_memory
in C ??Reader
access shm of Writer
via pointer)Q. Can we use boost::interprocess::managed_shared_memory in C ??
No. Technically, you can, of course (C is turing complete), but it's not supported.
Q. Is there any implementation problem in above section. (can't a reader access shm of writer via pointer)
Yes, you cannot usefully pass a pointer from one process to another, because they have isolated address space.
Q. Is there a way to solve this C and C++ limitation like any wrapper implementation etc.
The usual approach is to pass data among processes as part of the message. Use your message passing library (http://man7.org/linux/man-pages/man7/mq_overview.7.html, http://zeromq.org/, etc.).
Note: This also avoids trouble with synchronizing access to shared memory, that you haven't even talked about. This gives me the idea you didn't think of it
The next best idea would be to use raw shm API on both sides and pass the offset into the shared-memory area across processes. You could still use BIP, if you limit yourself to e.g. shared_memory_object
and e.g. offset_ptr