I\'m going through a program written in C/C++ for control in robotics. Basically
ID: 659134 • Letter: I
Question
I'm going through a program written in C/C++ for control in robotics. Basically, three different programs run at the same time, and they communicate via shared memory. Google-ling around I found thinks like vxWorks and the boost libraries interprocess headers (Boost documentation: Sharing memory between processes).
Now, I don't want to look at the implementation, I can read the link above. But I cannot get my head around to how the boost library does this. I mean, one application allocates memory, and other access that memory, but how do they communicate? isn't it unsafe to do this?
Explanation / Answer
But I cannot get my head around to how the boost library does this.
The boost interprocess mechanism has three necessary components to work:
memory-mapped file: a memory-mapped file needs to be created and passed to a boost.interprocess allocator. This allocator will take chunks of the file and use them as if they were returned by a std::allocator, with mapping applied so that the memory is compatible to in-process specific memory.
boost.interprocess container; this kind of container will use the memory returned by the allocator and offer a std::container like interface (begin/end/size/push_back, etc).
synchronization mechanism; this can be any interprocess mutex and should be used to prevent data access race conditions.
I mean, one application allocates memory, and other access that memory, but how do they communicate? isn't it unsafe to do this?
The allocated memory is actually a shared memory-mapped file The communication is indirect, with both applications setting or reading the data, as they need. The safety comes from using interprocess synchronization primitives.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.