Coder Perfect

shmget() vs. mmap() for Linux shared memory?


In this thread the OP is suggested to use mmap() instead of shmget() to get shared memory in Linux. I visited this page and this page to get some documentation, but the second one gives an obscure example regarding mmap().

Should I use the shmget() technique or mmap() as a rookie who needs to transmit some information (in text form) between two processes? And why is that?

Asked by BowPark

Solution #1

Both are viable options. The mmap approach is more limiting than shmget, but it is simpler to use. shmget is the most widely supported old System V shared memory model. The new POSIX approach of doing shared memory is mmap/shm open, which is more user-friendly. If your operating system allows it, I recommend using POSIX shared memory.

Some hints:

Answered by Sergey L.

Solution #2

A lot of it has to do with the past and the future.

There used to be two main (and sometimes competing) versions of unix: System V and BSD. SysV featured its own IPC implementations, which included shared memory, semaphores, and message queues. POSIX was created to try to bring everything together.

So far, there are two versions available: posix shared memory, MQs, and semaphores, and sysV versions. The sysV versions are also included in posix, which adds to the confusion.

So, do you want to utilize Posix-style shared memory or sysV-style shared memory? Most people, in general, take the long view and choose Posix because it appears to be the path to the future. But, honestly, the sysV stuff is so deeply buried in so many systems that it’s hard to believe it’ll ever go away.

After you’ve ruled out the long-term stuff, it’s all about what makes sense for your project and your preferences. In general, the sysV versions are more powerful, but they feature a clumsy interface that most people find confusing at first. This is especially true when it comes to sysV semaphores and message queues. In terms of shared memory, both sysV and posix can be considered awkward. The sysV versions carries the clunky ftok and key stuff while the posix ends up taking multiple calls and some race conditions to set up. From the outside, the posix versions have an advantage in that they utilize the file system and can be maintained with standard command line functions like ‘rm’ rather

So, which one should you go with? The posix versions, in general. However, you should become quite familiar with sysV versions. They have several features that go beyond the posix versions’ capabilities, which you may want to use in specific situations.

Answered by Duck

Post is based on