]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/shmget.2
execve.2: Remove unneeded ".sh" extension in interpreter script example
[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.\"
3d155313 38.TH SHMGET 2 2014-05-21 "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
MK
114.TP
115.BR SHM_NORESERVE " (since Linux 2.6.15)"
116This flag serves the same purpose as the
117.BR mmap (2)
118.B MAP_NORESERVE
119flag.
c13182ef 120Do not reserve swap space for this segment.
32047971
MK
121When swap space is reserved, one has the guarantee
122that it is possible to modify the segment.
8bd58774
MK
123When swap space is not reserved one might get
124.B SIGSEGV
125upon a write
32047971
MK
126if no physical memory is available.
127See also the discussion of the file
128.I /proc/sys/vm/overcommit_memory
c13182ef 129in
defcceb3 130.BR proc (5).
32047971
MK
131.\" As at 2.6.17-rc2, this flag has no effect if SHM_HUGETLB was also
132.\" specified.
fea681da 133.PP
e1d802d5
MK
134In addition to the above flags, the least significant 9 bits of
135.I shmflg
136specify the permissions granted to the owner, group, and others.
137These bits have the same format, and the same
138meaning, as the
139.I mode
140argument of
141.BR open (2).
142Presently, execute permissions are not used by the system.
143.PP
6319e935 144When a new shared memory segment is created,
d9bfdb9c 145its contents are initialized to zero values, and
6319e935 146its associated data structure,
fea681da 147.I shmid_ds
c952e226 148(see
6319e935 149.BR shmctl (2)),
d9bfdb9c 150is initialized as follows:
fea681da 151.IP
c952e226 152.I shm_perm.cuid
fea681da 153and
c952e226 154.I shm_perm.uid
499d62f2 155are set to the effective user ID of the calling process.
fea681da 156.IP
c952e226 157.I shm_perm.cgid
fea681da 158and
c952e226 159.I shm_perm.gid
499d62f2 160are set to the effective group ID of the calling process.
fea681da 161.IP
c952e226
MK
162The least significant 9 bits of
163.I shm_perm.mode
164are set to the least significant 9 bit of
fea681da
MK
165.IR shmflg .
166.IP
c952e226 167.I shm_segsz
fea681da
MK
168is set to the value of
169.IR size .
170.IP
c952e226
MK
171.IR shm_lpid ,
172.IR shm_nattch ,
a797afac 173.IR shm_atime ,
fea681da 174and
c952e226
MK
175.I shm_dtime
176are set to 0.
fea681da 177.IP
c952e226 178.I shm_ctime
fea681da
MK
179is set to the current time.
180.PP
c952e226 181If the shared memory segment already exists, the permissions are
fea681da 182verified, and a check is made to see if it is marked for destruction.
47297adb 183.SH RETURN VALUE
716c8c3f 184On success, a valid shared memory identifier is returned.
0bca57f3 185On error, \-1 is returned, and
716c8c3f
MK
186.I errno
187is set to indicate the error.
fea681da
MK
188.SH ERRORS
189On failure,
c952e226 190.I errno
fea681da 191is set to one of the following:
89b3c6b8 192.TP
fea681da 193.B EACCES
c13182ef 194The user does not have permission to access the
fea681da
MK
195shared memory segment, and does not have the
196.B CAP_IPC_OWNER
197capability.
198.TP
199.B EEXIST
247e5615
MK
200.BR IPC_CREAT
201and
202.BR IPC_EXCL
203were specified in
204.IR shmflg ,
205but a shared memory segment already exists for
206.IR key .
fea681da 207.TP
fea681da 208.B EINVAL
a350124d
MK
209A new segment was to be created and
210.I size
211is less than
212.B SHMIN
213or greater than
41651a35
MK
214.BR SHMMAX .
215.TP
216.B EINVAL
11bae399 217A segment for the given
41651a35
MK
218.I key
219exists, but \fIsize\fP is greater than the size
fea681da
MK
220of that segment.
221.TP
222.B ENFILE
223.\" [2.6.7] shmem_zero_setup()-->shmem_file_setup()-->get_empty_filp()
224The system limit on the total number of open files has been reached.
225.TP
226.B ENOENT
227No segment exists for the given \fIkey\fP, and
228.B IPC_CREAT
229was not specified.
230.TP
231.B ENOMEM
232No memory could be allocated for segment overhead.
233.TP
234.B ENOSPC
357cf3fe 235All possible shared memory IDs have been taken
fea681da
MK
236.RB ( SHMMNI ),
237or allocating a segment of the requested
238.I size
239would cause the system to exceed the system-wide limit on shared memory
240.RB ( SHMALL ).
eb57338f
MK
241.TP
242.B EPERM
243The
244.B SHM_HUGETLB
245flag was specified, but the caller was not privileged (did not have the
246.B CAP_IPC_LOCK
247capability).
47297adb 248.SH CONFORMING TO
a1d5f77c
MK
249SVr4, POSIX.1-2001.
250.\" SVr4 documents an additional error condition EEXIST.
251
252.B SHM_HUGETLB
53b81588
MK
253and
254.B SHM_NORESERVE
255are Linux extensions.
fea681da 256.SH NOTES
f6652463
MK
257The inclusion of
258.I <sys/types.h>
259and
260.I <sys/ipc.h>
261isn't required on Linux or by any version of POSIX.
262However,
263some old implementations required the inclusion of these header files,
264and the SVID also documented their inclusion.
265Applications intended to be portable to such old systems may need
266to include these header files.
267.\" Like Linux, the FreeBSD man pages still document
268.\" the inclusion of these header files.
269
fea681da
MK
270.B IPC_PRIVATE
271isn't a flag field but a
c952e226 272.I key_t
fea681da
MK
273type.
274If this special value is used for
275.IR key ,
ec64ab7f 276the system call ignores all but the least significant 9 bits of
fea681da 277.I shmflg
ec64ab7f 278and creates a new shared memory segment.
72c68b6a
MK
279.\"
280.SS Shared memory limits
540036b2 281The following limits on shared memory segment resources affect the
c952e226 282.BR shmget ()
fea681da 283call:
89b3c6b8 284.TP
fea681da 285.B SHMALL
b6bf92f6 286System-wide limit on the number of shared memory pages.
96ad3d65 287Since Linux 2.4, the default value for this limit is
e6a38ea6
MK
288
289 SHMMAX / PAGE_SIZE * (SHMMNI / 16)
290
23c95909
MS
291If
292.B SHMMAX
293and
294.B SHMMNI
295are not modified, this yields a limit for the
296total memory used by all shared memory segments of 8 GB:
d65250f5 297with a 4kB page size, this formula yields the value 2^21 (2,097,152);
23c95909 298with 8kB page size, it yields 2^20 (1048576).
e6a38ea6 299
8ab18f6c
MK
300On Linux, this limit can be read and modified via
301.IR /proc/sys/kernel/shmall .
fea681da
MK
302.TP
303.B SHMMAX
c00811f3
MK
304Maximum size in bytes for a shared memory segment.
305Since Linux 2.2, the default value of this limit is 0x2000000 (32MB).
306
8ab18f6c
MK
307On Linux, this limit can be read and modified via
308.IR /proc/sys/kernel/shmmax .
fea681da
MK
309.TP
310.B SHMMIN
311Minimum size in bytes for a shared memory segment: implementation
312dependent (currently 1 byte, though
313.B PAGE_SIZE
314is the effective minimum size).
315.TP
316.B SHMMNI
ab99b254 317System-wide limit on the number of shared memory segments.
098c6998
MK
318In Linux 2.2, the default value for this limit was 128;
319since Linux 2.4, the default value is 4096.
320
8ab18f6c
MK
321On Linux, this limit can be read and modified via
322.IR /proc/sys/kernel/shmmni .
ed978af2 323.\" Kernels between 2.4.x and 2.6.8 had an off-by-one error that meant
cd31a0d6 324.\" that we could create one more segment than SHMMNI -- MTK
c952e226 325.\" This /proc file is not available in Linux 2.2 and earlier -- MTK
fea681da 326.PP
5b55dde2 327The implementation has no specific limits for the per-process maximum
fea681da
MK
328number of shared memory segments
329.RB ( SHMSEG ).
c634028a 330.SS Linux notes
2ddef207 331Until version 2.3.30, Linux would return
0daa9e92 332.B EIDRM
2f0af33b 333for a
4fb31341
MK
334.BR shmget ()
335on a shared memory segment scheduled for deletion.
fea681da 336.SH BUGS
2f0af33b
MK
337The name choice
338.B IPC_PRIVATE
9091595d
MK
339was perhaps unfortunate,
340.B IPC_NEW
fea681da 341would more clearly show its function.
47297adb 342.SH SEE ALSO
fea681da
MK
343.BR shmat (2),
344.BR shmctl (2),
345.BR shmdt (2),
346.BR ftok (3),
92243659 347.BR capabilities (7),
f93af9c6 348.BR shm_overview (7),
2c5e151c 349.BR svipc (7)