]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/shmget.2
shmctl.2: CONFORMING TO: add POSIX.1-2008
[thirdparty/man-pages.git] / man2 / shmget.2
CommitLineData
fea681da
MK
1.\" Copyright (c) 1993 Luigi P. Bai (lpb@softint.com) July 28, 1993
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
7.\"
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.
c13182ef 12.\"
fea681da
MK
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
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da
MK
24.\"
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>
c11b1abf 30.\" Modified, 8 Jan 2003, Michael Kerrisk, <mtk.manpages@gmail.com>
fea681da 31.\" Removed EIDRM from errors - that can't happen...
c11b1abf 32.\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 33.\" Added notes on capability requirements
c11b1abf 34.\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
c952e226
MK
35.\" Language and formatting clean-ups
36.\" Added notes on /proc files
fea681da 37.\"
67d2c687 38.TH SHMGET 2 2015-05-07 "Linux" "Linux Programmer's Manual"
fea681da 39.SH NAME
47f065d6 40shmget \- allocates a System V shared memory segment
fea681da
MK
41.SH SYNOPSIS
42.ad l
43.B #include <sys/ipc.h>
d314dbfd 44.br
fea681da
MK
45.B #include <sys/shm.h>
46.sp
f7174672 47.BI "int shmget(key_t " key ", size_t " size ", int " shmflg );
fea681da
MK
48.ad b
49.SH DESCRIPTION
c952e226 50.BR shmget ()
efbfd7ec 51returns the identifier of the System\ V shared memory segment
fea681da
MK
52associated with the value of the argument
53.IR key .
54A new shared memory segment, with size equal to the value of
55.I size
56rounded up to a multiple of
57.BR PAGE_SIZE ,
58is created if
59.I key
60has the value
61.B IPC_PRIVATE
62or
63.I key
64isn't
65.BR IPC_PRIVATE ,
66no shared memory segment corresponding to
0daa9e92 67.I key
fea681da
MK
68exists, and
69.B IPC_CREAT
c952e226
MK
70is specified in
71.IR shmflg .
72.PP
73If
fea681da 74.I shmflg
c952e226
MK
75specifies both
76.B IPC_CREAT
77and
78.B IPC_EXCL
79and a shared memory segment already exists for
80.IR key ,
81then
82.BR shmget ()
c13182ef 83fails with
c952e226
MK
84.I errno
85set to
86.BR EEXIST .
c13182ef 87(This is analogous to the effect of the combination
c952e226
MK
88.B O_CREAT | O_EXCL
89for
90.BR open (2).)
fea681da
MK
91.PP
92The value
93.I shmflg
94is composed of:
95.TP 12
96.B IPC_CREAT
130a878d 97Create a new segment.
c13182ef 98If this flag is not used, then
c952e226 99.BR shmget ()
c13182ef 100will find the segment associated with \fIkey\fP and check to see if
fea681da
MK
101the user has permission to access the segment.
102.TP
103.B IPC_EXCL
fbaf6d6e
MK
104This flag is used with
105.B IPC_CREAT
106to ensure that this call creates the segment.
107If the segment already exists, the call fails.
fea681da 108.TP
32047971 109.BR SHM_HUGETLB " (since Linux 2.6)"
c13182ef 110Allocate the segment using "huge pages."
66a9882e 111See the Linux kernel source file
eb57338f
MK
112.I Documentation/vm/hugetlbpage.txt
113for further information.
32047971 114.TP
6ca52c17
MK
115.BR SHM_HUGE_2MB ", " SHM_HUGE_1GB " (since Linux 3.8)"
116.\" See https://lwn.net/Articles/533499/
117Used in conjunction with
118.B SHM_HUGETLB
119to select alternative hugetlb page sizes (respectively, 2 MB and 1 GB)
120on systems that support multiple hugetlb page sizes.
121
122More generally, the desired huge page size can be configured by encoding
123the base-2 logarithm of the desired page size in the six bits at the offset
124.BR SHM_HUGE_SHIFT .
125Thus, the above two constants are defined as:
126
127.nf
128.in +4
129#define SHM_HUGE_2MB (21 << SHM_HUGE_SHIFT)
130#define SHM_HUGE_1GB (30 << SHM_HUGE_SHIFT)
131.in
132.fi
133
134For some additional details,
135see the discussion of the similarly named constants in
136.BR mmap (2).
137.TP
32047971
MK
138.BR SHM_NORESERVE " (since Linux 2.6.15)"
139This flag serves the same purpose as the
140.BR mmap (2)
141.B MAP_NORESERVE
142flag.
c13182ef 143Do not reserve swap space for this segment.
32047971
MK
144When swap space is reserved, one has the guarantee
145that it is possible to modify the segment.
8bd58774
MK
146When swap space is not reserved one might get
147.B SIGSEGV
148upon a write
32047971
MK
149if no physical memory is available.
150See also the discussion of the file
151.I /proc/sys/vm/overcommit_memory
c13182ef 152in
defcceb3 153.BR proc (5).
32047971
MK
154.\" As at 2.6.17-rc2, this flag has no effect if SHM_HUGETLB was also
155.\" specified.
fea681da 156.PP
e1d802d5
MK
157In addition to the above flags, the least significant 9 bits of
158.I shmflg
159specify the permissions granted to the owner, group, and others.
160These bits have the same format, and the same
161meaning, as the
162.I mode
163argument of
164.BR open (2).
165Presently, execute permissions are not used by the system.
166.PP
6319e935 167When a new shared memory segment is created,
d9bfdb9c 168its contents are initialized to zero values, and
6319e935 169its associated data structure,
fea681da 170.I shmid_ds
c952e226 171(see
6319e935 172.BR shmctl (2)),
d9bfdb9c 173is initialized as follows:
fea681da 174.IP
c952e226 175.I shm_perm.cuid
fea681da 176and
c952e226 177.I shm_perm.uid
499d62f2 178are set to the effective user ID of the calling process.
fea681da 179.IP
c952e226 180.I shm_perm.cgid
fea681da 181and
c952e226 182.I shm_perm.gid
499d62f2 183are set to the effective group ID of the calling process.
fea681da 184.IP
c952e226
MK
185The least significant 9 bits of
186.I shm_perm.mode
187are set to the least significant 9 bit of
fea681da
MK
188.IR shmflg .
189.IP
c952e226 190.I shm_segsz
fea681da
MK
191is set to the value of
192.IR size .
193.IP
c952e226
MK
194.IR shm_lpid ,
195.IR shm_nattch ,
a797afac 196.IR shm_atime ,
fea681da 197and
c952e226
MK
198.I shm_dtime
199are set to 0.
fea681da 200.IP
c952e226 201.I shm_ctime
fea681da
MK
202is set to the current time.
203.PP
c952e226 204If the shared memory segment already exists, the permissions are
fea681da 205verified, and a check is made to see if it is marked for destruction.
47297adb 206.SH RETURN VALUE
716c8c3f 207On success, a valid shared memory identifier is returned.
0bca57f3 208On error, \-1 is returned, and
716c8c3f
MK
209.I errno
210is set to indicate the error.
fea681da
MK
211.SH ERRORS
212On failure,
c952e226 213.I errno
fea681da 214is set to one of the following:
89b3c6b8 215.TP
fea681da 216.B EACCES
c13182ef 217The user does not have permission to access the
fea681da
MK
218shared memory segment, and does not have the
219.B CAP_IPC_OWNER
220capability.
221.TP
222.B EEXIST
247e5615
MK
223.BR IPC_CREAT
224and
225.BR IPC_EXCL
226were specified in
227.IR shmflg ,
228but a shared memory segment already exists for
229.IR key .
fea681da 230.TP
fea681da 231.B EINVAL
a350124d
MK
232A new segment was to be created and
233.I size
234is less than
8804c8da 235.B SHMMIN
a350124d 236or greater than
41651a35
MK
237.BR SHMMAX .
238.TP
239.B EINVAL
11bae399 240A segment for the given
41651a35
MK
241.I key
242exists, but \fIsize\fP is greater than the size
fea681da
MK
243of that segment.
244.TP
245.B ENFILE
246.\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp()
247The system limit on the total number of open files has been reached.
248.TP
249.B ENOENT
250No segment exists for the given \fIkey\fP, and
251.B IPC_CREAT
252was not specified.
253.TP
254.B ENOMEM
255No memory could be allocated for segment overhead.
256.TP
257.B ENOSPC
357cf3fe 258All possible shared memory IDs have been taken
fea681da
MK
259.RB ( SHMMNI ),
260or allocating a segment of the requested
261.I size
262would cause the system to exceed the system-wide limit on shared memory
263.RB ( SHMALL ).
eb57338f
MK
264.TP
265.B EPERM
266The
267.B SHM_HUGETLB
268flag was specified, but the caller was not privileged (did not have the
269.B CAP_IPC_LOCK
270capability).
47297adb 271.SH CONFORMING TO
a1d5f77c
MK
272SVr4, POSIX.1-2001.
273.\" SVr4 documents an additional error condition EEXIST.
274
275.B SHM_HUGETLB
53b81588
MK
276and
277.B SHM_NORESERVE
278are Linux extensions.
fea681da 279.SH NOTES
f6652463
MK
280The inclusion of
281.I <sys/types.h>
282and
283.I <sys/ipc.h>
284isn't required on Linux or by any version of POSIX.
285However,
286some old implementations required the inclusion of these header files,
287and the SVID also documented their inclusion.
288Applications intended to be portable to such old systems may need
289to include these header files.
290.\" Like Linux, the FreeBSD man pages still document
291.\" the inclusion of these header files.
292
fea681da
MK
293.B IPC_PRIVATE
294isn't a flag field but a
c952e226 295.I key_t
fea681da
MK
296type.
297If this special value is used for
298.IR key ,
ec64ab7f 299the system call ignores all but the least significant 9 bits of
fea681da 300.I shmflg
ec64ab7f 301and creates a new shared memory segment.
72c68b6a
MK
302.\"
303.SS Shared memory limits
540036b2 304The following limits on shared memory segment resources affect the
c952e226 305.BR shmget ()
fea681da 306call:
89b3c6b8 307.TP
fea681da 308.B SHMALL
24ade49d
MK
309System-wide limit on the total amount of shared memory,
310measured in units of the system page size.
e5cceccf 311
9a413675
DB
312On Linux, this limit can be read and modified via
313.IR /proc/sys/kernel/shmall .
562ac2f2
MK
314Since Linux 3.16,
315.\" commit 060028bac94bf60a65415d1d55a359c3a17d5c31
9a413675
DB
316the default value for this limit is:
317
318 ULONG_MAX - 2^24
319
320The effect of this value
321(which is suitable for both 32-bit and 64-bit systems)
322is to impose no limitation on allocations.
323This value, rather than
324.BR ULONG_MAX ,
325was chosen as the default to prevent some cases where historical
326applications simply raised the existing limit without first checking
327its current value.
328Such applications would cause the value to overflow if the limit was set at
329.BR ULONG_MAX .
330
f037b098 331From Linux 2.4 up to Linux 3.15,
d4bf702f 332the default value for this limit was:
e6a38ea6
MK
333
334 SHMMAX / PAGE_SIZE * (SHMMNI / 16)
335
23c95909
MS
336If
337.B SHMMAX
338and
339.B SHMMNI
d4bf702f
MK
340were not modified, then multiplying the result of this formula
341by the page size (to get a value in bytes) yielded a value of 8 GB
342as the limit on the total memory used by all shared memory segments.
fea681da
MK
343.TP
344.B SHMMAX
c00811f3 345Maximum size in bytes for a shared memory segment.
e5cceccf 346
9a413675
DB
347On Linux, this limit can be read and modified via
348.IR /proc/sys/kernel/shmmax .
562ac2f2
MK
349Since Linux 3.16,
350.\" commit 060028bac94bf60a65415d1d55a359c3a17d5c31
9a413675
DB
351the default value for this limit is:
352
353 ULONG_MAX - 2^24
354
355The effect of this value
356(which is suitable for both 32-bit and 64-bit systems)
357is to impose no limitation on allocations.
358See the description of
359.BR SHMALL
360for a discussion of why this default value (rather than
361.BR ULONG_MAX )
362is used.
363
f037b098 364From Linux 2.2 up to Linux 3.15, the default value of
e5cceccf
MS
365this limit was 0x2000000 (32MB).
366
1fabbf12 367Because it is not possible to map just part of a shared memory segment,
e5cceccf 368the amount of virtual memory places another limit on the maximum size of a
1fabbf12
MK
369usable segment:
370for example, on i386 the largest segments that can be mapped have a
371size of around 2.8 GB, and on x86_64 the limit is around 127 TB.
fea681da
MK
372.TP
373.B SHMMIN
374Minimum size in bytes for a shared memory segment: implementation
375dependent (currently 1 byte, though
376.B PAGE_SIZE
377is the effective minimum size).
378.TP
379.B SHMMNI
ab99b254 380System-wide limit on the number of shared memory segments.
098c6998
MK
381In Linux 2.2, the default value for this limit was 128;
382since Linux 2.4, the default value is 4096.
383
8ab18f6c
MK
384On Linux, this limit can be read and modified via
385.IR /proc/sys/kernel/shmmni .
ed978af2 386.\" Kernels between 2.4.x and 2.6.8 had an off-by-one error that meant
cd31a0d6 387.\" that we could create one more segment than SHMMNI -- MTK
c952e226 388.\" This /proc file is not available in Linux 2.2 and earlier -- MTK
fea681da 389.PP
5b55dde2 390The implementation has no specific limits for the per-process maximum
fea681da
MK
391number of shared memory segments
392.RB ( SHMSEG ).
c634028a 393.SS Linux notes
2ddef207 394Until version 2.3.30, Linux would return
0daa9e92 395.B EIDRM
2f0af33b 396for a
4fb31341
MK
397.BR shmget ()
398on a shared memory segment scheduled for deletion.
fea681da 399.SH BUGS
2f0af33b
MK
400The name choice
401.B IPC_PRIVATE
9091595d
MK
402was perhaps unfortunate,
403.B IPC_NEW
fea681da 404would more clearly show its function.
47297adb 405.SH SEE ALSO
c4d76cd9 406.BR memfd_create (2),
fea681da
MK
407.BR shmat (2),
408.BR shmctl (2),
409.BR shmdt (2),
410.BR ftok (3),
92243659 411.BR capabilities (7),
f93af9c6 412.BR shm_overview (7),
2c5e151c 413.BR svipc (7)