DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Interprocess communication

Using shared memory

Sharing memory between processes occurs on a virtual segment basis. There is only one copy of each individual shared memory segment existing in the UNIX operating system at any time.

Before sharing of memory can be realized, a uniquely identified shared memory segment and data structure must be created. The unique identifier created is called the shared memory identifier (shmid); it is used to identify or refer to the associated data structure. This identifier is accessible by any process in the system, subject to normal access restrictions.

The data structure includes the following for each shared memory segment:

In UnixWare, the definition for the associated shared-memory segment data structure shmid_ds is as follows:

   /*
    *    There is a shared mem id data structure for each segment in the system.
    */
   struct shmid_ds {
       struct ipc_perm    shm_perm;      /* operation permission struct */
       int                shm_segsz;     /* segment size */
       struct region      *shm_reg;      /* ptr to region structure */
       char               pad[4];        /* for swap compatibility */
       pid_t              shm_lpid;      /* pid of last shmop */
       pid_t              shm_cpid;      /* pid of creator */
       ushort             shm_nattch;    /* used only for shminfo */
       ushort             shm_cnattch;   /* used only for shminfo */
       time_t             shm_atime;     /* last shmat time */
       time_t             shm_dtime;     /* last shmdt time */
       time_t             shm_ctime;     /* last change time */
   };

The C programming language data structure definition for the shared memory segment data structure shmid_ds is located in the sys/shm.h header file.

Note that the shm_perm member of this structure uses ipc_perm as a template. The ipc_perm data structure is the same for all IPC facilities; it is located in the sys/ipc.h header file and shown in ``ipc_perm data structure''.

The shmget system call performs two tasks:

The task performed is determined by the value of the key argument passed to the shmget system call.

For the first task, if the key is not already in use for an existing shared memory identifier at the security level of the calling process and the IPC_CREAT flag is set in shmflg, a new identifier is returned with an associated shared memory segment data structure created for it provided no system-tunable parameters would be exceeded.

There is also a provision for specifying a key of value zero which is known as the private key (IPC_PRIVATE); when specified, a new shmid is always returned with an associated shared memory segment data structure created for it unless a system-tunable parameter would be exceeded. The ipcs command will show the key field for the shmid as all zeros.

For the second task, if a shmid exists for the key specified, the value of the existing shmid is returned. If it is not desired to have an existing shmid returned, a control command (IPC_EXCL) can be specified (set) in the shmflg argument passed to the system call. ``Using shmget'' discusses how to use this system call.

When performing the first task, the process that calls shmget becomes the owner/creator, and the associated data structure is initialized accordingly. Remember, ownership can be changed, but the creating process always remains the creator (see ``Controlling shared memory''). The creator of the shared memory segment also determines the initial operation permissions for it.

Once a uniquely identified shared memory segment data structure is created, shmop (shared memory segment operations) and shmctl (shared memory control) can be used.

Shared memory segment operations consist of attaching and detaching shared memory segments. shmat and shmdt are provided for each of these operations (see ``Operations for shared memory'' for details of the shmat and shmdt system calls).

The shmctl system call permits you to control the shared memory facility in the following ways:

See the section ``Controlling shared memory'' for details of the shmctl system call.


Next topic: Getting shared memory segments
Previous topic: Shared memory

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004