Need help >> Boost's managed_shared_memory usage in between two processes (C and C++)
Hi, I am having a Design/Implementation issue with Boost managed_shared_memory. Let me describe it: -------------------- Problem ------------------------- I am having two processes (i) Writer (ii) 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. Once reader receive the address and size, I am using memcpy to write SHM data in Reader's local variable. Note1:- (This reader is not using any boost shm API, as it's C application ) Note2:- I validated address is same on Writer and reader (after communication). But reader's memcpy to read shm data is leading crash in my executable (reader). ------------------------------------------------------------- I need help in : 1) Can we use boost::interprocess::managed_shared_memory in C ?? 2) Is there any implementation problem in above section. (can't a reader access shm of writer via pointer) 3) Is there a way to solve this C and C++ limitation like any wrapper implementation etc. Please provide me any kind of help or pointers on this issue, as I am stuck with this and need to resolve it ASAP. Thanks, sharmavijay1991@gmail.com
On 08/10/2017 10:18, vijay sharma via Boost wrote:
Once I have addr pointer, I am using memcpy to write in SHM.
You shouldn't pass raw pointers to another application, usually pointers are only valid inside a process and invalid outside it. Only the distance between the start of the shared memory and an address inside that shared memory is common for both processes. Boost.Interprocess is not prepared to be used from a C application. Maybe you can try the following: http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/managed_memory_se... Take an address of an position in shared memory in application 1 and call get_handle_from_address. Then pass the handle (usually of type size_t, an unsigned integer) using something like pipes or message queues, or a well-known shared memory address so that process 2 can access to it. Then the C application can map the shared memory, and add the base address of that shared memory, add the given handle/offset, and obtain the address of the wanted position in the shared memory for the second process. In any case the code you've posted is invalid. Ion
Thanks Ion, So as per your suggestion I am planning: 1) Get the handle on application 1 and pass it to application 2. 2) Application 2 (a C-lang process) do shm_open with same SHM name. mmap will give me pointer to start. 3) Using [ (char *) cast on mmap's void pointer ] and [ Increment by <handle> ] I will reach to actual data in shm and them read it by memcpy. *This is looking possible to me. Do you this this will work??* I have a small doubt here : 1) Handle are like offset, please correct me if I am wrong. Even I am inserting first element in SHM still it's coming as very high value. Why is it like this ?? Is it because we are using Tree (memory allocation) in boost shared memory ? OR we always start allocation from bottom of SHM ? Thanks, VijaySharma On Sun, Oct 8, 2017 at 2:45 PM, Ion Gaztañaga via Boost < boost@lists.boost.org> wrote:
On 08/10/2017 10:18, vijay sharma via Boost wrote:
Once I have addr pointer, I am using memcpy to write in SHM.
You shouldn't pass raw pointers to another application, usually pointers are only valid inside a process and invalid outside it. Only the distance between the start of the shared memory and an address inside that shared memory is common for both processes.
Boost.Interprocess is not prepared to be used from a C application. Maybe you can try the following:
http://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/m anaged_memory_segments.html#interprocess.managed_memory_segm ents.managed_memory_segment_features.segment_offset
Take an address of an position in shared memory in application 1 and call get_handle_from_address. Then pass the handle (usually of type size_t, an unsigned integer) using something like pipes or message queues, or a well-known shared memory address so that process 2 can access to it. Then the C application can map the shared memory, and add the base address of that shared memory, add the given handle/offset, and obtain the address of the wanted position in the shared memory for the second process.
In any case the code you've posted is invalid.
Ion
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman /listinfo.cgi/boost
On 09/10/2017 7:27, vijay sharma via Boost wrote:
Thanks Ion,
So as per your suggestion I am planning: 1) Get the handle on application 1 and pass it to application 2. 2) Application 2 (a C-lang process) do shm_open with same SHM name. mmap will give me pointer to start. 3) Using [ (char *) cast on mmap's void pointer ] and [ Increment by <handle> ] I will reach to actual data in shm and them read it by memcpy.
*This is looking possible to me. Do you this this will work??*
I haven't tested myself, but it should be close. I can't remember if the offset is exactly from the start of the mapped region.
I have a small doubt here : 1) Handle are like offset, please correct me if I am wrong. Even I am inserting first element in SHM still it's coming as very high value. Why is it like this ?? Is it because we are using Tree (memory allocation) in boost shared memory ? OR we always start allocation from bottom of SHM ?
Yes, the handle is the offset between the address of the buffer and the start of the shared memory and the allocation is based on an intrusive red-black tree. Best, Ion
participants (2)
-
Ion Gaztañaga
-
vijay sharma