ptg10805159
572 Interprocess Communication Chapter 15
The Single UNIX Specification shared memory objects option includes alternative interfaces,
originally real-time extensions, to access shared memory.Wedon’t discuss them in this text.
We’ve already seen one form of shared memory when multiple processes map the
same file into their address spaces. The XSI shared memory differs from memory-
mapped files in that there is noassociated file. The XSI shared memory segments are
anonymous segments of memory.
The kernel maintains a structurewith at least the following members for each
shared memory segment:
struct shmid_ds {
struct ipc_perm shm_perm; /* see Section 15.6.2 */
size_t shm_segsz; /* size of segment in bytes */
pid_t shm_lpid; /* pid of last shmop() */
pid_t shm_cpid; /* pid of creator */
shmatt_t shm_nattch; /* number of current attaches */
time_t shm_atime; /* last-attach time */
time_t shm_dtime; /* last-detach time */
time_t shm_ctime; /* last-change time */
..
.
};
(Implementations add other structuremembers to support shared memory segments.)
The typeshmatt_tis defined to be an unsigned integer at least as large as an
unsigned short.Figure15.30 lists the system limits that affect shared memory.
Typical values
FreeBSD Linux Mac OS X Solaris
8.0 3.2.0 10.6.8 10
Description
maximum size in bytes of a shared memory segment 33,554,432 32,768 4,194,304 derived
minimum size in bytes of a shared memory segment 11 1 1
maximum number of shared memory segments, systemwide 192 4,096 32 128
maximum number of shared memory segments, per process 128 4,096 8128
Figure 15.30System limits that affect shared memory
The first function called is usuallyshmget, to obtain a shared memory identifier.
#include <sys/shm.h>
int shmget(key_tkey,size_tsize,intflag);
Returns: shared memory ID if OK,−1 on error
In Section 15.6.1, we described the rules for converting thekeyinto an identifier and
whether a new segment is created or an existing segment is referenced. Whenanew
segment is created, the following members of theshmid_dsstructureare initialized.
•Theipc_permstructure is initialized as described in Section 15.6.2. Themode
member of this structure is set to the corresponding permission bits offlag.
These permissions arespecified with the values from Figure15.24.