1SHMGET(2) Linux Programmer's Manual SHMGET(2)
2
3
4
6 shmget - allocates a System V shared memory segment
7
9 #include <sys/ipc.h>
10 #include <sys/shm.h>
11
12 int shmget(key_t key, size_t size, int shmflg);
13
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 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 This flag is used with IPC_CREAT to ensure that this call
35 creates the segment. If the segment already exists, the
36 call fails.
37
38 SHM_HUGETLB (since Linux 2.6)
39 Allocate the segment using "huge pages." See the Linux
40 kernel source file Documentation/vm/hugetlbpage.txt for
41 further information.
42
43 SHM_HUGE_2MB, SHM_HUGE_1GB (since Linux 3.8)
44 Used in conjunction with SHM_HUGETLB to select alternative
45 hugetlb page sizes (respectively, 2 MB and 1 GB) on systems
46 that support multiple hugetlb page sizes.
47
48 More generally, the desired huge page size can be config‐
49 ured by encoding the base-2 logarithm of the desired page
50 size in the six bits at the offset SHM_HUGE_SHIFT. Thus,
51 the above two constants are defined as:
52
53 #define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
54 #define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
55
56 For some additional details, see the discussion of the sim‐
57 ilarly named constants in mmap(2).
58
59 SHM_NORESERVE (since Linux 2.6.15)
60 This flag serves the same purpose as the mmap(2) MAP_NORE‐
61 SERVE flag. Do not reserve swap space for this segment.
62 When swap space is reserved, one has the guarantee that it
63 is possible to modify the segment. When swap space is not
64 reserved one might get SIGSEGV upon a write if no physical
65 memory is available. See also the discussion of the file
66 /proc/sys/vm/overcommit_memory in proc(5).
67
68 In addition to the above flags, the least significant 9 bits of shmflg
69 specify the permissions granted to the owner, group, and others. These
70 bits have the same format, and the same meaning, as the mode argument
71 of open(2). Presently, execute permissions are not used by the system.
72
73 When a new shared memory segment is created, its contents are initial‐
74 ized to zero values, and its associated data structure, shmid_ds (see
75 shmctl(2)), is initialized as follows:
76
77 shm_perm.cuid and shm_perm.uid are set to the effective user ID
78 of the calling process.
79
80 shm_perm.cgid and shm_perm.gid are set to the effective group ID
81 of the calling process.
82
83 The least significant 9 bits of shm_perm.mode are set to the
84 least significant 9 bit of shmflg.
85
86 shm_segsz is set to the value of size.
87
88 shm_lpid, shm_nattch, shm_atime, and shm_dtime are set to 0.
89
90 shm_ctime is set to the current time.
91
92 If the shared memory segment already exists, the permissions are veri‐
93 fied, and a check is made to see if it is marked for destruction.
94
96 On success, a valid shared memory identifier is returned. On error, -1
97 is returned, and errno is set to indicate the error.
98
100 On failure, errno is set to one of the following:
101
102 EACCES The user does not have permission to access the shared memory
103 segment, and does not have the CAP_IPC_OWNER capability in the
104 user namespace that governs its IPC namespace.
105
106 EEXIST IPC_CREAT and IPC_EXCL were specified in shmflg, but a shared
107 memory segment already exists for key.
108
109 EINVAL A new segment was to be created and size is less than SHMMIN or
110 greater than SHMMAX.
111
112 EINVAL A segment for the given key exists, but size is greater than the
113 size of that segment.
114
115 ENFILE The system-wide limit on the total number of open files has been
116 reached.
117
118 ENOENT No segment exists for the given key, and IPC_CREAT was not spec‐
119 ified.
120
121 ENOMEM No memory could be allocated for segment overhead.
122
123 ENOSPC All possible shared memory IDs have been taken (SHMMNI), or
124 allocating a segment of the requested size would cause the sys‐
125 tem to exceed the system-wide limit on shared memory (SHMALL).
126
127 EPERM The SHM_HUGETLB flag was specified, but the caller was not priv‐
128 ileged (did not have the CAP_IPC_LOCK capability).
129
131 POSIX.1-2001, POSIX.1-2008, SVr4.
132
133 SHM_HUGETLB and SHM_NORESERVE are Linux extensions.
134
136 The inclusion of <sys/types.h> and <sys/ipc.h> isn't required on Linux
137 or by any version of POSIX. However, some old implementations required
138 the inclusion of these header files, and the SVID also documented their
139 inclusion. Applications intended to be portable to such old systems
140 may need to include these header files.
141
142 IPC_PRIVATE isn't a flag field but a key_t type. If this special value
143 is used for key, the system call ignores all but the least significant
144 9 bits of shmflg and creates a new shared memory segment.
145
146 Shared memory limits
147 The following limits on shared memory segment resources affect the
148 shmget() call:
149
150 SHMALL System-wide limit on the total amount of shared memory, measured
151 in units of the system page size.
152
153 On Linux, this limit can be read and modified via /proc/sys/ker‐
154 nel/shmall. Since Linux 3.16, the default value for this limit
155 is:
156
157 ULONG_MAX - 2^24
158
159 The effect of this value (which is suitable for both 32-bit and
160 64-bit systems) is to impose no limitation on allocations. This
161 value, rather than ULONG_MAX, was chosen as the default to pre‐
162 vent some cases where historical applications simply raised the
163 existing limit without first checking its current value. Such
164 applications would cause the value to overflow if the limit was
165 set at ULONG_MAX.
166
167 From Linux 2.4 up to Linux 3.15, the default value for this
168 limit was:
169
170 SHMMAX / PAGE_SIZE * (SHMMNI / 16)
171
172 If SHMMAX and SHMMNI were not modified, then multiplying the
173 result of this formula by the page size (to get a value in
174 bytes) yielded a value of 8 GB as the limit on the total memory
175 used by all shared memory segments.
176
177 SHMMAX Maximum size in bytes for a shared memory segment.
178
179 On Linux, this limit can be read and modified via /proc/sys/ker‐
180 nel/shmmax. Since Linux 3.16, the default value for this limit
181 is:
182
183 ULONG_MAX - 2^24
184
185 The effect of this value (which is suitable for both 32-bit and
186 64-bit systems) is to impose no limitation on allocations. See
187 the description of SHMALL for a discussion of why this default
188 value (rather than ULONG_MAX) is used.
189
190 From Linux 2.2 up to Linux 3.15, the default value of this limit
191 was 0x2000000 (32 MB).
192
193 Because it is not possible to map just part of a shared memory
194 segment, the amount of virtual memory places another limit on
195 the maximum size of a usable segment: for example, on i386 the
196 largest segments that can be mapped have a size of around
197 2.8 GB, and on x86-64 the limit is around 127 TB.
198
199 SHMMIN Minimum size in bytes for a shared memory segment: implementa‐
200 tion dependent (currently 1 byte, though PAGE_SIZE is the effec‐
201 tive minimum size).
202
203 SHMMNI System-wide limit on the number of shared memory segments. In
204 Linux 2.2, the default value for this limit was 128; since Linux
205 2.4, the default value is 4096.
206
207 On Linux, this limit can be read and modified via /proc/sys/ker‐
208 nel/shmmni.
209
210 The implementation has no specific limits for the per-process maximum
211 number of shared memory segments (SHMSEG).
212
213 Linux notes
214 Until version 2.3.30, Linux would return EIDRM for a shmget() on a
215 shared memory segment scheduled for deletion.
216
218 The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more
219 clearly show its function.
220
222 memfd_create(2), shmat(2), shmctl(2), shmdt(2), ftok(3), capabili‐
223 ties(7), shm_overview(7), svipc(7)
224
226 This page is part of release 4.15 of the Linux man-pages project. A
227 description of the project, information about reporting bugs, and the
228 latest version of this page, can be found at
229 https://www.kernel.org/doc/man-pages/.
230
231
232
233Linux 2017-09-15 SHMGET(2)