1SHMGET(2)                  Linux Programmer's Manual                 SHMGET(2)
2
3
4

NAME

6       shmget - allocates a System V shared memory segment
7

SYNOPSIS

9       #include <sys/ipc.h>
10       #include <sys/shm.h>
11
12       int shmget(key_t key, size_t size, int shmflg);
13

DESCRIPTION

15       shmget()  returns  the identifier of the System V shared memory segment
16       associated with the value of the argument key.   A  new  shared  memory
17       segment,  with size equal to the value of size rounded up to a multiple
18       of PAGE_SIZE, is created if key has the value IPC_PRIVATE or key  isn't
19       IPC_PRIVATE,  no shared memory segment corresponding to key exists, and
20       IPC_CREAT is specified in shmflg.
21
22       If shmflg specifies both IPC_CREAT and IPC_EXCL  and  a  shared  memory
23       segment  already  exists for key, then shmget() fails with errno set to
24       EEXIST.  (This is analogous to the effect of the combination O_CREAT  |
25       O_EXCL for open(2).)
26
27       The value shmflg is composed of:
28
29       IPC_CREAT   to  create  a  new segment.  If this flag is not used, then
30                   shmget() will find the  segment  associated  with  key  and
31                   check  to see if the user has permission to access the seg‐
32                   ment.
33
34       IPC_EXCL    used with  IPC_CREAT  to  ensure  failure  if  the  segment
35                   already exists.
36
37       mode_flags  (least  significant  9  bits)  specifying  the  permissions
38                   granted to the owner, group, and world.   These  bits  have
39                   the same format, and the same meaning, as the mode argument
40                   of open(2).  Presently, the  execute  permissions  are  not
41                   used by the system.
42
43       SHM_HUGETLB (since Linux 2.6)
44                   Allocate  the  segment  using  "huge pages."  See the Linux
45                   kernel  source  file  Documentation/vm/hugetlbpage.txt  for
46                   further information.
47
48       SHM_NORESERVE (since Linux 2.6.15)
49                   This  flag serves the same purpose as the mmap(2) MAP_NORE‐
50                   SERVE flag.  Do not reserve swap space  for  this  segment.
51                   When  swap space is reserved, one has the guarantee that it
52                   is possible to modify the segment.  When swap space is  not
53                   reserved  one might get SIGSEGV upon a write if no physical
54                   memory is available.  See also the discussion of  the  file
55                   /proc/sys/vm/overcommit_memory in proc(5).
56
57       When  a new shared memory segment is created, its contents are initial‐
58       ized to zero values, and its associated data structure,  shmid_ds  (see
59       shmctl(2)), is initialized as follows:
60
61              shm_perm.cuid  and shm_perm.uid are set to the effective user ID
62              of the calling process.
63
64              shm_perm.cgid and shm_perm.gid are set to the effective group ID
65              of the calling process.
66
67              The  least  significant  9  bits of shm_perm.mode are set to the
68              least significant 9 bit of shmflg.
69
70              shm_segsz is set to the value of size.
71
72              shm_lpid, shm_nattch, shm_atime and shm_dtime are set to 0.
73
74              shm_ctime is set to the current time.
75
76       If the shared memory segment already exists, the permissions are  veri‐
77       fied, and a check is made to see if it is marked for destruction.
78

RETURN VALUE

80       On success, a valid shared memory identifier is returned.  On errir, -1
81       is returned, and errno is set to indicate the error.
82

ERRORS

84       On failure, errno is set to one of the following:
85
86       EACCES The user does not have permission to access  the  shared  memory
87              segment, and does not have the CAP_IPC_OWNER capability.
88
89       EEXIST IPC_CREAT | IPC_EXCL was specified and the segment exists.
90
91       EINVAL A new segment was to be created and size < SHMMIN or size > SHM‐
92              MAX, or no new segment was to be created, a segment  with  given
93              key existed, but size is greater than the size of that segment.
94
95       ENFILE The  system  limit  on  the  total number of open files has been
96              reached.
97
98       ENOENT No segment exists for the given key, and IPC_CREAT was not spec‐
99              ified.
100
101       ENOMEM No memory could be allocated for segment overhead.
102
103       ENOSPC All  possible  shared  memory  IDs  have been taken (SHMMNI), or
104              allocating a segment of the requested size would cause the  sys‐
105              tem to exceed the system-wide limit on shared memory (SHMALL).
106
107       EPERM  The SHM_HUGETLB flag was specified, but the caller was not priv‐
108              ileged (did not have the CAP_IPC_LOCK capability).
109

CONFORMING TO

111       SVr4, POSIX.1-2001.
112
113       SHM_HUGETLB is a nonportable Linux extension.
114

NOTES

116       The inclusion of <sys/types.h> and <sys/ipc.h> isn't required on  Linux
117       or by any version of POSIX.  However, some old implementations required
118       the inclusion of these header files, and the SVID also documented their
119       inclusion.   Applications  intended  to be portable to such old systems
120       may need to include these header files.
121
122       IPC_PRIVATE isn't a flag field but a key_t type.  If this special value
123       is  used for key, the system call ignores everything but the least sig‐
124       nificant 9 bits of shmflg and creates a new shared memory  segment  (on
125       success).
126
127       The  following  limits  on  shared  memory segment resources affect the
128       shmget() call:
129
130       SHMALL System wide maximum of shared memory pages (on Linux, this limit
131              can be read and modified via /proc/sys/kernel/shmall).
132
133       SHMMAX Maximum size in bytes for a shared memory segment: policy depen‐
134              dent (on  Linux,  this  limit  can  be  read  and  modified  via
135              /proc/sys/kernel/shmmax).
136
137       SHMMIN Minimum  size  in bytes for a shared memory segment: implementa‐
138              tion dependent (currently 1 byte, though PAGE_SIZE is the effec‐
139              tive minimum size).
140
141       SHMMNI System  wide maximum number of shared memory segments: implemen‐
142              tation dependent (currently 4096, was 128 before  Linux  2.3.99;
143              on Linux, this limit can be read and modified via /proc/sys/ker‐
144              nel/shmmni).
145
146       The implementation has no specific limits for the  per-process  maximum
147       number of shared memory segments (SHMSEG).
148
149   Linux notes
150       Until  version  2.3.30  Linux  would  return  EIDRM for a shmget() on a
151       shared memory segment scheduled for deletion.
152

BUGS

154       The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more
155       clearly show its function.
156

SEE ALSO

158       shmat(2),  shmctl(2),  shmdt(2),  ftok(3),  capabilities(7),  shm_over‐
159       view(7), svipc(7)
160

COLOPHON

162       This page is part of release 3.53 of the Linux  man-pages  project.   A
163       description  of  the project, and information about reporting bugs, can
164       be found at http://www.kernel.org/doc/man-pages/.
165
166
167
168Linux                             2013-04-19                         SHMGET(2)
Impressum