1 .\" Copyright (c) 1993 Luigi P. Bai (lpb@softint.com) July 28, 1993
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Modified Wed Jul 28 10:57:35 1993, Rik Faith <faith@cs.unc.edu>
26 .\" Modified Sun Nov 28 16:43:30 1993, Rik Faith <faith@cs.unc.edu>
27 .\" with material from Giorgio Ciucci <giorgio@crcc.it>
28 .\" Portions Copyright 1993 Giorgio Ciucci <giorgio@crcc.it>
29 .\" Modified Tue Oct 22 22:03:17 1996 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified, 8 Jan 2003, Michael Kerrisk, <mtk.manpages@gmail.com>
31 .\" Removed EIDRM from errors - that can't happen...
32 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Added notes on capability requirements
34 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
35 .\" Language and formatting clean-ups
36 .\" Added notes on /proc files
38 .TH SHMGET 2 2021-03-22 "Linux" "Linux Programmer's Manual"
40 shmget \- allocates a System V shared memory segment
44 .B #include <sys/shm.h>
46 .BI "int shmget(key_t " key ", size_t " size ", int " shmflg );
51 returns the identifier of the System\ V shared memory segment
52 associated with the value of the argument
54 It may be used either to obtain the identifier of a previously created
55 shared memory segment (when
59 does not have the value
61 or to create a new set.
63 A new shared memory segment, with size equal to the value of
65 rounded up to a multiple of
75 no shared memory segment corresponding to
88 and a shared memory segment already exists for
96 (This is analogous to the effect of the combination
106 Create a new segment.
107 If this flag is not used, then
109 will find the segment associated with \fIkey\fP and check to see if
110 the user has permission to access the segment.
113 This flag is used with
115 to ensure that this call creates the segment.
116 If the segment already exists, the call fails.
118 .BR SHM_HUGETLB " (since Linux 2.6)"
119 Allocate the segment using "huge" pages.
120 See the Linux kernel source file
121 .I Documentation/admin\-guide/mm/hugetlbpage.rst
122 for further information.
124 .BR SHM_HUGE_2MB ", " SHM_HUGE_1GB " (since Linux 3.8)"
125 .\" See https://lwn.net/Articles/533499/
126 Used in conjunction with
128 to select alternative hugetlb page sizes (respectively, 2\ MB and 1\ GB)
129 on systems that support multiple hugetlb page sizes.
131 More generally, the desired huge page size can be configured by encoding
132 the base-2 logarithm of the desired page size in the six bits at the offset
134 Thus, the above two constants are defined as:
138 #define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
139 #define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
143 For some additional details,
144 see the discussion of the similarly named constants in
147 .BR SHM_NORESERVE " (since Linux 2.6.15)"
148 This flag serves the same purpose as the
152 Do not reserve swap space for this segment.
153 When swap space is reserved, one has the guarantee
154 that it is possible to modify the segment.
155 When swap space is not reserved one might get
158 if no physical memory is available.
159 See also the discussion of the file
160 .I /proc/sys/vm/overcommit_memory
163 .\" As at 2.6.17-rc2, this flag has no effect if SHM_HUGETLB was also
166 In addition to the above flags, the least significant 9 bits of
168 specify the permissions granted to the owner, group, and others.
169 These bits have the same format, and the same
174 Presently, execute permissions are not used by the system.
176 When a new shared memory segment is created,
177 its contents are initialized to zero values, and
178 its associated data structure,
182 is initialized as follows:
187 are set to the effective user ID of the calling process.
192 are set to the effective group ID of the calling process.
194 The least significant 9 bits of
196 are set to the least significant 9 bit of
200 is set to the value of
211 is set to the current time.
213 If the shared memory segment already exists, the permissions are
214 verified, and a check is made to see if it is marked for destruction.
216 On success, a valid shared memory identifier is returned.
217 On error, \-1 is returned, and
219 is set to indicate the error.
223 The user does not have permission to access the
224 shared memory segment, and does not have the
226 capability in the user namespace that governs its IPC namespace.
234 but a shared memory segment already exists for
238 A new segment was to be created and
246 A segment for the given
248 exists, but \fIsize\fP is greater than the size
252 .\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp()
253 The system-wide limit on the total number of open files has been reached.
256 No segment exists for the given \fIkey\fP, and
261 No memory could be allocated for segment overhead.
264 All possible shared memory IDs have been taken
266 or allocating a segment of the requested
268 would cause the system to exceed the system-wide limit on shared memory
274 flag was specified, but the caller was not privileged (did not have the
277 and is not a member of the
278 .I sysctl_hugetlb_shm_group
279 group; see the description of
280 .I /proc/sys/vm/sysctl_hugetlb_shm_group
284 POSIX.1-2001, POSIX.1-2008, SVr4.
285 .\" SVr4 documents an additional error condition EEXIST.
290 are Linux extensions.
293 isn't a flag field but a
296 If this special value is used for
298 the system call ignores all but the least significant 9 bits of
300 and creates a new shared memory segment.
302 .SS Shared memory limits
303 The following limits on shared memory segment resources affect the
308 System-wide limit on the total amount of shared memory,
309 measured in units of the system page size.
311 On Linux, this limit can be read and modified via
312 .IR /proc/sys/kernel/shmall .
314 .\" commit 060028bac94bf60a65415d1d55a359c3a17d5c31
315 the default value for this limit is:
319 The effect of this value
320 (which is suitable for both 32-bit and 64-bit systems)
321 is to impose no limitation on allocations.
322 This value, rather than
324 was chosen as the default to prevent some cases where historical
325 applications simply raised the existing limit without first checking
327 Such applications would cause the value to overflow if the limit was set at
330 From Linux 2.4 up to Linux 3.15,
331 the default value for this limit was:
333 SHMMAX / PAGE_SIZE * (SHMMNI / 16)
339 were not modified, then multiplying the result of this formula
340 by the page size (to get a value in bytes) yielded a value of 8\ GB
341 as the limit on the total memory used by all shared memory segments.
344 Maximum size in bytes for a shared memory segment.
346 On Linux, this limit can be read and modified via
347 .IR /proc/sys/kernel/shmmax .
349 .\" commit 060028bac94bf60a65415d1d55a359c3a17d5c31
350 the default value for this limit is:
354 The effect of this value
355 (which is suitable for both 32-bit and 64-bit systems)
356 is to impose no limitation on allocations.
357 See the description of
359 for a discussion of why this default value (rather than
363 From Linux 2.2 up to Linux 3.15, the default value of
364 this limit was 0x2000000 (32\ MB).
366 Because it is not possible to map just part of a shared memory segment,
367 the amount of virtual memory places another limit on the maximum size of a
369 for example, on i386 the largest segments that can be mapped have a
370 size of around 2.8\ GB, and on x86-64 the limit is around 127 TB.
373 Minimum size in bytes for a shared memory segment: implementation
374 dependent (currently 1 byte, though
376 is the effective minimum size).
379 System-wide limit on the number of shared memory segments.
380 In Linux 2.2, the default value for this limit was 128;
381 since Linux 2.4, the default value is 4096.
383 On Linux, this limit can be read and modified via
384 .IR /proc/sys/kernel/shmmni .
385 .\" Kernels between 2.4.x and 2.6.8 had an off-by-one error that meant
386 .\" that we could create one more segment than SHMMNI -- MTK
387 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
389 The implementation has no specific limits for the per-process maximum
390 number of shared memory segments
393 Until version 2.3.30, Linux would return
397 on a shared memory segment scheduled for deletion.
401 was perhaps unfortunate,
403 would more clearly show its function.
408 .BR memfd_create (2),
413 .BR capabilities (7),
414 .BR shm_overview (7),