Shared Memory is an efficeint means of passing data between programs. One program will create a memory portion which other processes (if permitted) can access.

A process creates a shared memory segment using shmget()|. The original owner of a shared memory segment can assign ownership to another user with shmctl(). It can also revoke this assignment. Other processes with proper permission can perform various control functions on the shared memory segment using shmctl(). Once created, a shared segment can be attached to a process address space using shmat(). It can be detached using shmdt() (see shmop()). The attaching process must have the appropriate permissions for shmat(). Once attached, the process can read or write to the segment, as allowed by the permission requested in the attach operation. A shared segment can be attached multiple times by the same process. A shared memory segment is described by a control structure with a unique ID that points to an area of physical memory. The identifier of the segment is called the shmid. The structure definition for the shared memory segment control structures and prototypews can be found in <sys/shm.h>.

 The following source code is a C++ Template Class for a Shared Object




  1. Thanks for the source code! It really helped me. Is it possible to have dynamic shared memory somehow? For example a shared vector or something like that?

    • Io.D. Reply

      Hello! Unfortunately there is not an API providing it. However i’ll give a shoot..

Write A Comment