shmget(2)shmget(2)NAMEshmget - Return (and possibly create) the ID for a shared memory region
SYNOPSIS
#include <sys/shm.h>
int shmget(
key_t key,
size_t size,
int flags );
Application developers might want to specify #include statements for
<sys/types.h> and <sys/ipc.h> before the one for <sys/shm.h> if pro‐
grams are being developed for multiple platforms. The additional
#include statements are not required on Tru64 UNIX systems or by ISO or
XSH specifications, but might be required on other vendors' systems
that conform to these standards.
STANDARDS
Interfaces documented on this reference page conform to industry stan‐
dards as follows:
shmget(): XSH4.0, XSH4.2, XSH5.0
Refer to the standards(5) reference page for more information about
industry standards and associated tags.
PARAMETERS
Specifies the key that identifies the shared memory region. The value
for the key parameter can be IPC_PRIVATE or a random number other than
zero (0). If the value of key is IPC_PRIVATE, it can be used to assure
the return of the identifier of a new, unused shared memory region.
Specifies the minimum number of bytes to allocate for the region.
Specifies the creation options. Possible values are: If the specified
key does not exist, the shmget() function creates a shared memory iden‐
tifier for the specified key.
If the specified key does exist, and IPC_EXCL is not set, the
shared memory identifier for the specified key is returned.
If the specified key does exist and IPC_EXCL is set, the
shmget() function fails and returns an error. If the key
already exists, the shmget() function fails and returns an error
notification.
DESCRIPTION
The shmget() function returns (and possibly creates) the ID for the
shared memory region identified by the key parameter. If IPC_PRIVATE is
used for the key parameter, the shmget() function returns the ID for a
private (that is, newly created) shared memory region. The flags param‐
eter supplies creation options for the shmget() function. If the key
parameter does not already exist, the IPC_CREAT flag instructs the
shmget() function to create a new shared memory region for the key and
return the kernel-assigned ID for the region.
After creating a new shared memory region ID, the shmget() function
initializes the shmid_ds structure associated with the ID as follows:
The shm_perm.cuid and shm_perm.uid fields are set equal to the effec‐
tive user ID of the calling process. The shm_perm.cgid and
shm_perm.gid fields are set equal to the effective group ID of the
calling process. The low-order nine bits of the shm_perm.mode field
are set equal to the low-order nine bits of flags. The shm_segsz field
is set equal to size. The shm_lpid, shm_nattch, shm_atime, and
shm_dtime fields are all set equal to 0 (zero). The shm_ctime field is
set equal to the current time. [Tru64 UNIX] The shm_cpid field is set
to the process ID of the calling process.
[Tru64 UNIX] To reduce the overhead associated with managing large
shared memory regions among many processes, the Tru64 UNIX kernel makes
use of shared page tables, also referred to as segmented shared memory
(SSM). When the segment size requested by shmget() is greater than or
equal to the value of the ssm_threshold system attribute, the memory
region is managed using shared page tables. When attaching to such a
segment, the segment must always be aligned and sized correctly. The
alignment and size factor is given by the system-defined SSM_SIZE value
(see <machine/pmap.h>). When the attachment occurs, the segment's mem‐
ory region is aligned on an SSM_SIZE boundary and its size is rounded
up to the next SSM_SIZE boundary--thus, the segment size specified by
shmget() does not need to be a multiple of SSM_SIZE. Use of segmented
shared memory can be disabled by setting the ssm_threshold attribute to
zero.
For more information on segmented shared memory and interprocess commu‐
nication, see the system attribute descriptions in sys_attrs_ipc(5) and
the discussion of tuning interprocess communication limits in the Sys‐
tem Configuration and Tuning guide.
NOTES
The librt library contains alternative interfaces for interprocess com‐
munication. The names of these routines adhere to the format shm_* and
their reference pages are listed in SEE ALSO.
RETURN VALUES
Upon successful completion, a shared memory identifier is returned. If
the shmget() function fails, a value of -1 is returned and errno is set
to indicate the error.
ERRORS
The shmget() function sets errno to the specified values for the fol‐
lowing conditions: A shared memory region ID already exists for the key
parameter, but operation permission as specified by the low-order nine
bits of the flags parameter was not granted. A shared memory region ID
already exists for the key parameter, but IPC_CREAT and IPC_EXCL were
specified for the flags parameter. The value of the size parameter is
less than the system-defined minimum or greater than the system-defined
maximum. Or, a shared memory region ID already exists for the key
parameter, but the number of bytes allocated for the region is less
than size and size is not equal to 0 (zero). A shared memory region ID
does not exist for the key parameter, and IPC_CREAT was not used for
the flags parameter. An attempt was made to create a shared memory
region ID and its associated shmid_ds structure, but there was not
enough physical memory available. An attempt to create a new shared
memory region ID exceeded the system-wide limit on the maximum number
of IDs allowed.
SEE ALSO
Functions: shmat(2), shmctl(2), shmdt(2), sysconfig(8), table(2),
ftok(3), shm_open(3), shm_unlink(3)
Files: shmid_ds(4)
Standards: standards(5)shmget(2)