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() function. 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, a data structure is also created to contain information about the shared memory segment. This structure is defined in the <sys/shm.h> header file as follows:

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;

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() function. The thread also can use the shmctl() function 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 and is addressable from only one process. 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() or if a shared memory segment is created from a program that 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.



Top | UNIX-Type APIs | APIs by category