79

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

Being almost a newbie, and needing to share some information (in text form) between two processes, should I use the shmget() method or mmap()? And why?

2 Answers 2

106

Both methods are viable. mmap method is a little bit more restrictive then shmget, but easier to use. shmget is the old System V shared memory model and has the widest support. mmap/shm_open is the new POSIX way to do shared memory and is easier to use. If your OS permits the use of POSIX shared memory then I would suggest going with that.

Some hints:

  • If you create your children via fork then mmap with MAP_ANONYMOUS | MAP_SHARED is by far the easiest way - just one call. MAP_ANONYMOUS is however a Linux extension not specified by POSIX.
  • If you start the processes independently, but can supply them with a shared memory name then shm_open (+ ftruncate) + mmap with MAP_SHARED is two/three calls. Requires librt on some OSes.
  • If your OS has /dev/shm/ then shm_open is equivalent to opening a file in /dev/shm/.
6
  • Thank you for all the useful advices. I won't use fork for now.
    – BowPark
    Jan 23, 2014 at 15:14
  • 2
    But with mmap a file on the hard disk is authomatically modified when you modify the memory? So you cause frequent writings to that file?
    – BowPark
    Jan 23, 2014 at 15:15
  • 7
    /dev/shm is your RAMdisk. If you use file-backed shared memory mmap a file that is on an actual disk then it will be up to the OS to schedule updates. Normally the mapped file will not be updated until long after you stop using the mapping or the paging daemon decided to swap your pages out. You can force an update by calling msync which is the recommended practice when using file-backed mappings that you wish to write to disk.
    – Sergey L.
    Jan 23, 2014 at 15:18
  • 7
    @BowPark memory mapped with MAP_ANONYMOUS is NOT backed by a file. Aug 14, 2015 at 13:14
  • 3
    On OSX you want mmap as the max shared memory with shmget is only 4mb across all processes sadly. Mar 25, 2017 at 6:38
47

A lot of this has to do with history and future directions.

Once upon a time there were two main (and somewhat competing) versions of unix - system V and BSD. SysV had its own versions of IPC, including the big 3 - shared memory, semaphores, and message queues. POSIX came along to try and unite things.

So at present we have two versions - posix shared memory, MQs, and semaphores and the sysV versions. Just to make things a little more confusing the sysV versions are also part of posix.

So basically your question is do you want to use Posix or sysV style shared memory? In general most people take the long term view and opt for Posix because that seems to be road to the future. But, realistically, sysV stuff is so embedded in so many systems you have to have serious doubts that it will ever go away.

So, eliminating the long term stuff, it comes down to what makes sense for your project and your tastes. In general the sysV versions tend to actually be somewhat more powerful but they have an clunky interface that most people find a little bewildering at first contact. The is particularly true of sysV semaphores and message queues. In terms of shared memory it can be argued both sysV and posix are 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 than relying on separate utility programs (e.g. ipcs) that sysV requires.

So which should you use? As a rule, the posix versions. But you should really familiarize yourself with sysV versions. They have some features that go beyond the capabilities of the posix versions which you may want to take advantage of in specific situations.

2
  • 3
    Can you explain why you think the System V versions are more powerful? Jan 23, 2014 at 16:08
  • 14
    Powerful might be the wrong word. Fuller featured perhaps? For instance sysv semaphores range from positive thru negatives numbers, can be added or subtracted, can undo an operation if a process dies, can report the last process to operate on them (that would be more useful if that was extended to report threads), exist in kernel so you don't have to mess with shared memory, and you can create an entire set of them in one shot. sysv MQs have msgtypes which allow many processes to easily share the same queue and just process a specific type. Posix MQs always read oldest msg w/i a priority
    – Duck
    Jan 24, 2014 at 4:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.