Linux shared memory: shmget() vs mmap()?
Solution 1
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
thenmmap
withMAP_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
withMAP_SHARED
is two/three calls. Requireslibrt
on some OSes. - If your OS has
/dev/shm/
thenshm_open
is equivalent to opening a file in/dev/shm/
.
Solution 2
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.
BowPark
Updated on July 05, 2022Comments
-
BowPark almost 2 years
In this thread the OP is suggested to use
mmap()
instead ofshmget()
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 regardingmmap()
.Being almost a newbie, and needing to share some information (in text form) between two processes, should I use the
shmget()
method ormmap()
? And why? -
BowPark over 10 yearsThank you for all the useful advices. I won't use fork for now.
-
BowPark over 10 yearsBut with mmap a file on the hard disk is authomatically modified when you modify the memory? So you cause frequent writings to that file?
-
Sergey L. over 10 years
/dev/shm
is your RAMdisk. If you use file-backed shared memorymmap
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 callingmsync
which is the recommended practice when using file-backed mappings that you wish to write to disk. -
Translucent Pain over 10 yearsCan you explain why you think the System V versions are more powerful?
-
Duck over 10 yearsPowerful 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
-
PSkocik over 8 years@BowPark memory mapped with MAP_ANONYMOUS is NOT backed by a file.
-
Gregory Ray about 7 yearsOn OSX you want mmap as the max shared memory with shmget is only 4mb across all processes sadly.
-
A_P over 5 years@GregoryRay you can use sysctl to change kern.sysv.shmmax and kern.sysv.shmall.