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:
- Shared memory ID
- Pointer to an address
- Flag specifying how the shared memory segment is to be attached
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 ID
- Command the thread wants to perform (remove ID, set data structure values,
receive data structure values)
- Pointer to a buffer from which to set data structure values, or in which to
receive data structure values.
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.
- Teraspace shared memory objects may be attached in read-only mode.
- The address specified by shmaddr is only used when
shmat() is called from a program that uses data model LLP64 and
attaches to a teraspace shared memory segment. Otherwise it is not possible to
specify the address in teraspace at which the shared memory is to be mapped.
The shmaddr parameter on the shmat() function is
ignored.
- After a teraspace shared memory segment is detached, it cannot be addressed
through a pointer saved by the process.
- The maximum size of a teraspace shared memory segment is
4 294 967 295 bytes (4 GB minus 1).
- The maximum number of shared memory segments that can be created
(system-wide) is 2 147 483 646.
- A teraspace shared memory segment may be created such that its size can be
changed after it is created. The maximum size of this type of shared memory
segment is 268 435 456 bytes (256 MB).
The i5/OS nonteraspace shared memory differs from the shared memory definition
in the Single UNIX Specification in the following
ways:
- The nonteraspace shared memory segments are i5/OS space objects and can be
attached only in read/write mode, not in the read-only mode that the
Single UNIX Specification
allows. If the SHM_RDONLY flag is specified in the
shmflg parameter on a shmget() call, the call fails and
the errno variable is set to [EOPNOTSUPP].
- A nonteraspace shared memory segment can be attached only at the actual
address of the i5/OS space object, not at an address specified by the thread.
The shmaddr parameter on the shmat() function is
ignored.
- After a nonteraspace shared memory segment is detached from a process, it
still can be addressed through a pointer saved by the process. For nonteraspace
shared memory segments, i5/OS does not "map" and "unmap" regions of storage to
the address space of a process.
- The maximum size of a nonteraspace shared memory segment is
16 776 960 bytes. Although the maximum size of a shared memory
segment is 16 776 960 bytes, shared memory segments larger than
16 773 120 bytes should be created as teraspace shared memory
segments. When the operating system accesses a nonteraspace shared memory
segment that has a size larger than 16 773 120 bytes, a performance
degradation may be observed.
- The maximum number of shared memory segments that can be created
(system-wide) is 2 147 483 646.
- The size of a nonteraspace shared memory segment may be changed using the
SHM_RESIZE command of shmctl(), up to a maximum size of
16 773 120 bytes.
The shared memory functions are:
- shmat() (Attach Shared Memory Segment to Current Process) returns the address of the shared memory segment associated with the specified shared memory identifier.
- shmctl() (Perform Shared Memory Control Operations) provides shared memory control operations as specified by cmd on the shared memory segment specified by shmid.
- shmdt() (Detach Shared Memory Segment from Calling Process) detaches the shared memory segment specified by shmaddr from the calling process.
- shmget() (Get ID of Shared Memory Segment with Key) returns the shared memory ID associated with the specified shared memory key.
See also IPC Key Generation Functions for additional shared memory functions.