简体   繁体   中英

Boost c++ Interprocess

I am a beginner starting with boost c++ interprocess library and ipc mechanism. What is the difference between managed shared memory and managed heap memory?

In managed shared memory to increase the size of segment we need to grow it offline using segment::grow() method.

Does heap memory allow to increase the size of memory segment online(when processes are accessing it)?

I would like to know if the map object existing in a process accessed by different threads can be shared to other process executing different threads?

What is the best technique to implement this? I wasn't able to get this from boost interprocess documentation.

Your question is a bit broad and comes down to "I'm afraid to do it wrong". So allow me to address the concerns briefly.

Does heap memory allow to increase the size of memory segment online(when processes are accessing it)?

If you mean regular process heap (as facilitated by the (C) standard libraries), then, yes. I don't think you've ever needed to explicitly state the size of the heap (let alone grow it) in the last 20-30 years.

Yes, process heaps are grown transparently by the OS¹.

What is the difference between managed shared memory and managed heap memory?

Again, if by "managed heap" you mean regular [standard library] process heap, the differences are significant.

Anything that's shared across processes must be clear of absolute memory references, because the mapping might will be at a different base-address in the other processes. This means that only strictly POD data types are automatically supported. All the rest, like containers, only if using a special-purpose allocator that avoids absolute pointers.

Sidenote: if you were thinking of "managed heaps" like the JVM or CLR concepts, that's a different story. Reference types in those VMs can usually be transparently moved, but the mechanics thereof are significantly different (as they serve different goals: avoiding fragmentation and parititioning in generational garbage collectors ).

Does heap memory allow to increase the size of memory segment online(when processes are accessing it)?

That kind of heap cannot be accessed from more than one process, at all.

I would like to know if the map object existing in a process accessed by different threads can be shared to other process executing different threads?

For the purpose of thread-safety, you can consider processes equivalent to threads. The same goes for threads from other processes. You have to synchronize access to the shared data - whether it's from a thread within the same process, or one outside it.

This can be achieved with atomic data types ( https://www.boost.org/doc/libs/1_66_0/doc/html/atomic.html or https://en.cppreference.com/w/cpp/atomic ) as well as with the classical synchronization primitives.

Sharing these primitives across processes can often be done in two ways:

  • using Boost's inteprocess primitives inside the shared memory
  • using help from the OS, in the form of named interprocess synchronization primitives

See https://www.boost.org/doc/libs/1_68_0/doc/html/interprocess/synchronization_mechanisms.html for details.

Summarizing

The question you did NOT ask but that might be behind your thinking, is "What does a Boost Interprocess segment manager manage"?

In practice it does afford heap-like management of shared memory (with one of the limitations: not being able to grow online). This is then used to facilitate the Boost Interprocess allocator that you can use to store non-POD data types (like strings or other containers) inside your shared memory segment.

If you look at my answers, you'll find many many examples of how to do that:


¹ on mainstream operating systems of the last decades

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM