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