Shared Memory Functions

Processes and threads can communicate directly with one another by sharing parts of their memory space and then reading and writing the data stored in the shared memory. Synchronization of shared memory is the responsibility of the application program. Semaphores and mutexes provide ways to synchronize shared memory use across processes and threads.

A thread gets a shared memory identifier by calling the shmget() or shmget64() functions. Depending on the key and shmflg parameters passed in, either a new shared memory segment is created or an existing shared memory segment is accessed. The size of the shared memory segment is specified by the size parameter. When a new shared memory segment is created, the system also creates and maintains information about the shared memory segment. This information can be accessed or modified using either a basic or extended (64-Bit Enabled) data structure format. These data structures are defined in the <sys/shm.h> header file as follows:

/* Basic shared memory segment information */
typedef struct shmid_ds {
    struct ipc_perm shm_perm;   /* Operation permission struct*/
    int             shm_segsz;  /* Segment size               */
    pid_t           shm_lpid;   /* Process id of last shmop   */
    pid_t           shm_cpid;   /* Process id of creator      */
    int             shm_nattch; /* Current # attached         */
    time_t          shm_atime;  /* Last shmat time            */
    time_t          shm_dtime;  /* Last shmdt time            */
    time_t          shm_ctime;  /* Last change time           */
} shmtablentry_t;
/* Extended (64-Bit Enabled) shared memory segment information*/
typedef struct shmid_ds64 {
    struct ipc_perm  shm_perm;   /* Permissions               */
    pid_t            shm_lpid;   /* Process ID of last shared
                                      memory operation        */
    pid_t            shm_cpid;   /* Process ID of creator     */
    shmatt_t         shm_nattch; /* Number of current attaches*/
    size64_t         shm_segsz;  /* Size of segment in bytes  */
    char             shm_reserved1[4]; /* Reserved space      */
    time_t           shm_atime;  /* Time of last shmat()      */
    char             shm_reserved2[4]; /* Reserved space      */
    time_t           shm_dtime;  /* Time of last shmdt()      */
    char             shm_reserved3[4]; /* Reserved space      */
    time_t           shm_ctime;  /* Time of last change by
                                      shmctl()                */
    psize_t          shm_pagesize;      /* Page size          */
    char             shm_reserved4[24]; /* Reserved space     */
} shmtablentry64_t;

A process gets addressability to the shared memory segment by attaching to it using the shmat() function. The following parameters are passed in:

A process detaches a shared memory segment by calling the shmdt() function. The only parameter passed in is the shared memory segment address. The process implicitly detaches from the shared memory when the process ends.

A thread removes a shared memory ID by calling the shmctl() or shmctl64() function. The thread also can use the shmctl() and shmctl64() functions to change the data structure values associated with the shared memory ID or to retrieve the data structure values associated with the shared memory ID. The following parameters are passed in:


Shared Memory Differences and Restrictions

Shared memory segments are created as teraspace-shared memory segments or as nonteraspace-shared memory segments. A teraspace shared memory segment is accessed by adding the shared memory segment to a process's teraspace. A teraspace is a space that has a much larger capacity than other i5/OS® spaces. A nonteraspace shared memory segment creates shared memory using i5/OS space objects.

A teraspace shared memory segment is created if SHM_TS_NP is specified on the shmflag parameter of shmget(), if the shmget64() function is used to create the shared memory segment, or if the program creating the shared memory segment was compiled using the TERASPACE(*YES *TSIFC) option of CRTBNDC or CRTCMOD. The following capabilities and restrictions apply for teraspace shared memory segments.

The i5/OS nonteraspace shared memory differs from the shared memory definition in the Single UNIX® Specification in the following ways:

The shared memory functions are:

See also IPC Key Generation Functions for additional shared memory functions.



[ Back to top | UNIX-Type APIs | APIs by category ]