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
forkthenmmapwithMAP_ANONYMOUS | MAP_SHAREDis by far the easiest way – just one call.MAP_ANONYMOUSis 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) +mmapwithMAP_SHAREDis two/three calls. Requireslibrton some OSes. - If your OS has
/dev/shm/thenshm_openis equivalent to opening a file in/dev/shm/.