shmget
Purpose
Gets shared memory segment.
Syntax
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget (key, size, shmflg)
key_t key;
int size, shmflg;
Description
The shmget system call returns the shared memory identi-
fier associated with the specified key. The key param-
eter is either the value IPC_PRIVATE or an IPC key
constructed by the ftok subroutine (or by a similar algo-
rithm). See "ftok" for details about this subroutine.
The size parameter specifies the number of bytes of
shared memory required.
The shmflg parameter is constructed by logically OR-ing
one or more of the following values:
IPC_CREAT Creates the data structure if it does not
already exist.
IPC_EXCL Causes the shmget system call to fail if
IPC_CREAT is also set and the data structure
already exists.
S_IRUSR Permits the process that owns the data
structure to read it.
S_IWUSR Permits the process that owns the data
structure to modify it.
S_IRGRP Permits the group associated with the data
structure to read it.
S_IWGRP Permits the group associated with the data
structure to modify it.
S_IROTH Permits others to read the data structure.
S_IWOTH Permits others to modify the data structure.
The values that begin with S_I- are defined in the
sys/stat.h header file and are a subset of the access
permissions that apply to files.
A shared memory identifier, its associated data struc-
ture, and a shared memory segment equal in bytes to the
value of the size parameter are created for the key
parameter if one of the following is true:
o The key parameter is equal to IPC_PRIVATE.
o The key parameter does not already have a shared
memory identifier associated with it, and IPC_CREAT
is set.
Upon creation, the data structure associated with the new
shared memory identifier is initialized as follows:
o shm_perm.cuid and shm_perm.uid are set equal to the
effective user ID of the calling process.
o shm_perm.cgid and shm_perm.gid are set equal to the
effective group ID of the calling process.
o The low-order nine bits of shm_perm.mode are set
equal to the low-order nine bits of the shmflg param-
eter.
o shm_segsz is set equal to the value of the size
parameter.
o shm_lpid, shm_nattch, shm_atime, and shm_dtime are
set equal to 0.
o shm_ctime is set equal to the current time.
Return Value
Upon successful completion, a shared memory identifier is
returned. If shmget fails, a value of -1 is returned and
errno is set to indicate the error.
Diagnostics
The shmget system call fails if one or more of the fol-
lowing are true:
EINVAL The size parameter is less than the system-
imposed minimum or greater than the system-
imposed maximum.
EACCES A shared memory identifier exists for the key
parameter but operation permission as speci-
fied by the low-order nine bits of the shmflg
parameter is not granted.
EINVAL A shared memory identifier exists for key, but
the size of the segment associated with it is
less than the size parameter and the size
parameter is not equal to 0.
ENOENT A shared memory identifier does not exist for
the key parameter and IPC_CREAT not set.
ENOSPC A shared memory identifier is to be created
but the system-imposed limit on the maximum
number of allowed shared memory identifiers
system wide will be exceeded.
ENOMEM A shared memory identifier and associated
shared memory segment are to be created but
the amount of available physical memory is not
sufficient to fill the request.
EEXIST A shared memory identifier exists for the key
parameter, and both IPC_CREAT and IPC_EXCL are
set.
Related Information
In this book: "shmat," "shmctl," "shmdt," and "ftok."