NAME
shmget - allocates a System V shared memory segment SYNOPSIS
#include
#include
int shmget(keyt key, sizet size, int shmflg); DESCRIPTION shmget() returns the identifier of the System V shared memory segment associated with the value of the argument key. A new shared memory segment, with size equal to the value of size rounded up to a multiple of PAGESIZE, is created if key has the value IPCPRIVATE or key isn't IPCPRIVATE, no shared memory segment corresponding to key exists, and IPCCREAT is specified in shmflg. If shmflg specifies both IPCCREAT and IPCEXCL and a shared memory segment already exists for key, then shmget() fails with errno set to EEXIST. (This is analogous to the effect of the combination OCREAT | OEXCL for open(2).) The value shmflg is composed of: IPCCREAT to create a new segment. If this flag is not used, then shmget() will find the segment associated with key and check to see if the user has permission to access the seg‐ ment. IPCEXCL used with IPCCREAT to ensure failure if the segment already exists. modeflags (least significant 9 bits) specifying the permissions granted to the owner, group, and world. These bits have the same format, and the same meaning, as the mode argument of open(2). Presently, the execute permissions are not used by the system. SHMHUGETLB (since Linux 2.6) Allocate the segment using "huge pages." See the Linux kernel source file Documentation/vm/hugetlbpage.txt for further information. SHMNORESERVE (since Linux 2.6.15) This flag serves the same purpose as the mmap(2) MAPNORE‐ SERVE flag. Do not reserve swap space for this segment. When swap space is reserved, one has the guarantee that it is possible to modify the segment. When swap space is not reserved one might get SIGSEGV upon a write if no physical memory is available. See also the discussion of the file /proc/sys/vm/overcommitmemory in proc(5). When a new shared memory segment is created, its contents are initial‐ ized to zero values, and its associated data structure, shmidds (see shmctl(2)), is initialized as follows: shmperm.cuid and shmperm.uid are set to the effective user ID of the calling process. shmperm.cgid and shmperm.gid are set to the effective group ID of the calling process. The least significant 9 bits of shmperm.mode are set to the least significant 9 bit of shmflg. shmsegsz is set to the value of size. shmlpid, shmnattch, shmatime and shmdtime are set to 0. shmctime is set to the current time. If the shared memory segment already exists, the permissions are veri‐ fied, and a check is made to see if it is marked for destruction. RETURN VALUE On success, a valid shared memory identifier is returned. On errir, -1 is returned, and errno is set to indicate the error. ERRORS On failure, errno is set to one of the following: EACCES The user does not have permission to access the shared memory segment, and does not have the CAPIPCOWNER capability. EEXIST IPCCREAT | IPCEXCL was specified and the segment exists. EINVAL A new segment was to be created and size < SHMMIN or size > SHM‐ MAX, or no new segment was to be created, a segment with given key existed, but size is greater than the size of that segment. ENFILE The system limit on the total number of open files has been reached. ENOENT No segment exists for the given key, and IPCCREAT was not spec‐ ified. ENOMEM No memory could be allocated for segment overhead. ENOSPC All possible shared memory IDs have been taken (SHMMNI), or allocating a segment of the requested size would cause the sys‐
tem to exceed the system-wide limit on shared memory (SHMALL). EPERM The SHMHUGETLB flag was specified, but the caller was not priv‐ ileged (did not have the CAPIPCLOCK capability). CONFORMING TO
SVr4, POSIX.1-2001. SHMHUGETLB is a nonportable Linux extension. NOTES The inclusion of
and isn't required on Linux or by any version of POSIX. However, some old implementations required the inclusion of these header files, and the SVID also documented their inclusion. Applications intended to be portable to such old systems may need to include these header files. IPCPRIVATE isn't a flag field but a keyt type. If this special value is used for key, the system call ignores everything but the least sig‐ nificant 9 bits of shmflg and creates a new shared memory segment (on success). The following limits on shared memory segment resources affect the shmget() call: SHMALL System wide maximum of shared memory pages (on Linux, this limit can be read and modified via /proc/sys/kernel/shmall). SHMMAX Maximum size in bytes for a shared memory segment: policy depen‐ dent (on Linux, this limit can be read and modified via /proc/sys/kernel/shmmax). SHMMIN Minimum size in bytes for a shared memory segment: implementa‐ tion dependent (currently 1 byte, though PAGESIZE is the effec‐ tive minimum size). SHMMNI System wide maximum number of shared memory segments: implemen‐ tation dependent (currently 4096, was 128 before Linux 2.3.99; on Linux, this limit can be read and modified via /proc/sys/ker‐ nel/shmmni). The implementation has no specific limits for the per-process maximum number of shared memory segments (SHMSEG). Linux notes Until version 2.3.30 Linux would return EIDRM for a shmget() on a shared memory segment scheduled for deletion. BUGS The name choice IPCPRIVATE was perhaps unfortunate, IPCNEW would more clearly show its function. SEE ALSO shmat(2), shmctl(2), shmdt(2), ftok(3), capabilities(7), shmover‐ view(7), svipc(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2013-04-19 SHMGET(2)