How to share memory between applications written in C/C++

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?

5

But I cannot get my head around to how the boost library does this.

The boost interprocess mechanism has three necessary components to work:

  1. 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.

  2. 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).

  3. 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.

2

shared memory is not the complete picture for IPC, its a data-passing mechanism but you still need some way to inform the other process that some data has been updated and is available to be read. How you do this is up to you, typically you’d use an OS mutex or event object, each process waits on this to be set, the application writing sets it once its finished writing. Then threads in the other programs wake up and read.

Alternatively you can poll, read the data regularly for a value that changes when the data is updated (eg a incrementing counter).

Boost uses memory mapping of a file.

Both unix and windows support creation of files that don’t exist on the normal file system for just this purpose.

Then you will need to synchronize access to that memory like you would if different threads were to access it. Meaning concurrent reads can happen without synchronization but as soon as one process want to write you will need to prevent the others from accessing it.

Atomic operations on the shared memory is still possible if you want a lockless synchronization.

3

Shared memory is still just memory. You can put a mutex, spinlock or any other synchronization primitive in there, and use them to synchronize your processes’ access to the shared memory, exactly like threads use those primitives to synchronize access to the memory visible to them.

The only real differences are:

  1. threads share all memory and the same address space, so raw pointers work for them. Memory shared between processes works exactly the same, but may be mapped at different addresses in each process, so you can’t simply pass raw pointers between them

    • NB. this has a knock-on effect on some implementation details of virtual methods, runtime type information, and some other C++ mechanisms. Stick to trivially-initializable (plain old data) types with no virtual methods or dynamic casts in your shared memory, don’t use typeid on them, and you should be fine.
  2. some synchronization primitives may need special flags or attributes to work correctly between processes (see the PTHREAD_PROCESS_SHARED attribute for POSIX thread mutexes, for example). This isn’t really to do with the memory and synchronization in themselves, but due to the kernel/scheduler interaction needed to wake up sleeping waiters.


So:

but how do they communicate?

The same way different threads communicate, allowing for the caveats above

isn’t it unsafe to do this?

Yes, it’s exactly as unsafe for processes to communicate via shared memory as it is for threads to communicate via shared memory, and they need equivalent (or identical) synchronization to make it safe.

Notice that C and C++ are different languages.

Shared memory is impossible in purely standard C11, or C++11 (since the standard does not define that), or even C++14 (whose n3690 draft, and presumably official standard, does not mention shared memory outside of multi-threading). So you need extra libraries to get shared memory. But some operating systems have support for shared memory. So several libraries providing shared memory, built above existing operating system services, exist. You could perhaps consider using the POCO framework library (which abstracts over OS specific details)

For Linux (and perhaps POSIX), look into shm_overview(7). You’ll need to synchronize, so see also sem_overview(7)

VXWorks (which I don’t know, but googled for it) has VxMP

You need to carefully understand what is really happening. You probably want to share only plain old data struct-s (not C++ classes!) and you should be very careful about the addresses (each process might get different address for the common shared memory segment) and about synchronization.

Alternatively, use threads. Notice that C++11 standard defines a thread library.

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật