]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/semget.2
pldd.1, bpf.2, chdir.2, clone.2, fanotify_init.2, fanotify_mark.2, intro.2, ipc.2...
[thirdparty/man-pages.git] / man2 / semget.2
CommitLineData
fea681da
MK
1.\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
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 Tue Oct 22 17:54:56 1996 by Eric S. Raymond <esr@thyrsus.com>
26.\" Modified 1 Jan 2002, Martin Schulze <joey@infodrom.org>
c11b1abf
MK
27.\" Modified 4 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
28.\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 29.\" Added notes on capability requirements
c11b1abf 30.\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
c952e226
MK
31.\" Language and formatting clean-ups
32.\" Added notes on /proc files
d9bfdb9c 33.\" Rewrote BUGS note about semget()'s failure to initialize
c952e226 34.\" semaphore values
fea681da 35.\"
63121bd4 36.TH SEMGET 2 2019-08-02 "Linux" "Linux Programmer's Manual"
fea681da 37.SH NAME
47f065d6 38semget \- get a System V semaphore set identifier
fea681da
MK
39.SH SYNOPSIS
40.nf
2fadbfb5
MK
41.B #include <sys/types.h>
42.B #include <sys/ipc.h>
43.B #include <sys/sem.h>
fea681da 44.fi
68e4db0a 45.PP
fea681da
MK
46.BI "int semget(key_t " key ,
47.BI "int " nsems ,
48.BI "int " semflg );
49.SH DESCRIPTION
c952e226
MK
50The
51.BR semget ()
efbfd7ec 52system call returns the System\ V semaphore set identifier
fea681da
MK
53associated with the argument
54.IR key .
f8ac3de3
MK
55It may be used either to obtain the identifier of a previously created
56semaphore set (when
57.I semflg
58is zero and
59.I key
60does not have the value
61.BR IPC_PRIVATE ),
62or to create a new set.
63.PP
fea681da
MK
64A new set of
65.I nsems
66semaphores is created if
67.I key
68has the value
69.B IPC_PRIVATE
540036b2 70or if no existing semaphore set is associated with
fea681da
MK
71.I key
72and
73.B IPC_CREAT
c952e226
MK
74is specified in
75.IR semflg .
fea681da 76.PP
c952e226 77If
fea681da 78.I semflg
c952e226 79specifies both
fea681da
MK
80.B IPC_CREAT
81and
82.B IPC_EXCL
83and a semaphore set already exists for
c952e226
MK
84.IR key ,
85then
86.BR semget ()
c13182ef 87fails with
c952e226
MK
88.I errno
89set to
90.BR EEXIST .
c13182ef 91(This is analogous to the effect of the combination
c952e226
MK
92.B O_CREAT | O_EXCL
93for
94.BR open (2).)
fea681da 95.PP
c952e226 96Upon creation, the least significant 9 bits of the argument
fea681da 97.I semflg
c952e226 98define the permissions (for owner, group and others)
fea681da
MK
99for the semaphore set.
100These bits have the same format, and the same
c952e226
MK
101meaning, as the
102.I mode
103argument of
fea681da 104.BR open (2)
c952e226 105(though the execute permissions are
fea681da
MK
106not meaningful for semaphores, and write permissions mean permission
107to alter semaphore values).
108.PP
109When creating a new semaphore set,
c952e226 110.BR semget ()
d9bfdb9c 111initializes the set's associated data structure,
c952e226 112.I semid_ds
c13182ef 113(see
2c5e151c 114.BR semctl (2)),
fea681da
MK
115as follows:
116.IP
c952e226 117.I sem_perm.cuid
fea681da 118and
c952e226 119.I sem_perm.uid
499d62f2 120are set to the effective user ID of the calling process.
fea681da 121.IP
c952e226 122.I sem_perm.cgid
fea681da 123and
c952e226 124.I sem_perm.gid
499d62f2 125are set to the effective group ID of the calling process.
fea681da 126.IP
c952e226
MK
127The least significant 9 bits of
128.I sem_perm.mode
129are set to the least significant 9 bits of
fea681da
MK
130.IR semflg .
131.IP
c952e226 132.I sem_nsems
fea681da
MK
133is set to the value of
134.IR nsems .
135.IP
c952e226 136.I sem_otime
fea681da
MK
137is set to 0.
138.IP
c952e226 139.I sem_ctime
fea681da
MK
140is set to the current time.
141.PP
142The argument
143.I nsems
c952e226 144can be 0
fea681da
MK
145(a don't care)
146when a semaphore set is not being created.
e9001b56 147Otherwise,
fea681da 148.I nsems
c952e226 149must be greater than 0
fea681da
MK
150and less than or equal to the maximum number of semaphores per semaphore set
151.RB ( SEMMSL ).
152.PP
c952e226 153If the semaphore set already exists, the permissions are
fea681da
MK
154verified.
155.\" and a check is made to see if it is marked for destruction.
47297adb 156.SH RETURN VALUE
fea681da 157If successful, the return value will be the semaphore set identifier
32cdf39c 158(a nonnegative integer), otherwise, \-1
fea681da
MK
159is returned, with
160.I errno
161indicating the error.
162.SH ERRORS
90db854a 163On failure,
fea681da
MK
164.I errno
165will be set to one of the following:
89b3c6b8 166.TP
fea681da
MK
167.B EACCES
168A semaphore set exists for
169.IR key ,
170but the calling process does not have permission to access the set,
171and does not have the
0daa9e92 172.B CAP_IPC_OWNER
3294109d 173capability in the user namespace that governs its IPC namespace.
fea681da
MK
174.TP
175.B EEXIST
fea681da
MK
176.B IPC_CREAT
177and
df036ba6
MK
178.BR IPC_EXCL
179were specified in
3fcc7a7a 180.IR semflg ,
df036ba6
MK
181but a semaphore set already exists for
182.IR key .
fea681da
MK
183.\" .TP
184.\" .B EIDRM
185.\" The semaphore set is marked to be deleted.
186.TP
187.B EINVAL
0daa9e92 188.I nsems
fea681da
MK
189is less than 0 or greater than the limit on the number
190of semaphores per semaphore set
a6cad8ff
MK
191.RB ( SEMMSL ).
192.TP
193.B EINVAL
194A semaphore set corresponding to
fea681da 195.I key
a6cad8ff 196already exists, but
fea681da
MK
197.I nsems
198is larger than the number of semaphores in that set.
199.TP
200.B ENOENT
201No semaphore set exists for
202.I key
203and
204.I semflg
c952e226 205did not specify
fea681da
MK
206.BR IPC_CREAT .
207.TP
208.B ENOMEM
c952e226
MK
209A semaphore set has to be created but the system does not have
210enough memory for the new data structure.
fea681da
MK
211.TP
212.B ENOSPC
213A semaphore set has to be created but the system limit for the maximum
214number of semaphore sets
215.RB ( SEMMNI ),
216or the system wide maximum number of semaphores
217.RB ( SEMMNS ),
218would be exceeded.
47297adb 219.SH CONFORMING TO
a1d5f77c
MK
220SVr4, POSIX.1-2001.
221.\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN,
222.\" ERANGE, EFAULT.
fea681da 223.SH NOTES
b93dd595
MK
224The inclusion of
225.I <sys/types.h>
226and
227.I <sys/ipc.h>
228isn't required on Linux or by any version of POSIX.
229However,
230some old implementations required the inclusion of these header files,
231and the SVID also documented their inclusion.
232Applications intended to be portable to such old systems may need
233to include these header files.
234.\" Like Linux, the FreeBSD man pages still document
235.\" the inclusion of these header files.
efeece04 236.PP
b93dd595
MK
237.B IPC_PRIVATE
238isn't a flag field but a
239.I key_t
240type.
241If this special value is used for
242.IR key ,
243the system call ignores all but the least significant 9 bits of
244.I semflg
245and creates a new semaphore set (on success).
246.\"
247.SS Semaphore initialization
143f4ec3
MK
248The values of the semaphores in a newly created set are indeterminate.
249(POSIX.1-2001 and POSIX.1-2008 are explicit on this point,
250although POSIX.1-2008 notes that a future version of the standard
251may require an implementation to initialize the semaphores to 0.)
252Although Linux, like many other implementations,
253initializes the semaphore values to 0,
254a portable application cannot rely on this:
255it should explicitly initialize the semaphores to the desired values.
256.\" In truth, every one of the many implementations that I've tested sets
257.\" the values to zero, but I suppose there is/was some obscure
258.\" implementation out there that does not.
efeece04 259.PP
143f4ec3
MK
260Initialization can be done using
261.BR semctl (2)
262.B SETVAL
263or
264.B SETALL
265operation.
0e94942d 266Where multiple peers do not know who will be the first to
143f4ec3
MK
267initialize the set, checking for a nonzero
268.I sem_otime
269in the associated data structure retrieved by a
270.BR semctl (2)
271.B IPC_STAT
0e94942d 272operation can be used to avoid races.
098c4f64
MK
273.\"
274.SS Semaphore limits
540036b2 275The following limits on semaphore set resources affect the
c952e226 276.BR semget ()
fea681da 277call:
89b3c6b8 278.TP
fea681da 279.B SEMMNI
8a3e6dc7
MK
280System-wide limit on the number of semaphore sets.
281On Linux systems before version 3.19,
282the default value for this limit was 128.
283Since Linux 3.19,
284.\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
285the default value is 32,000.
286On Linux, this limit can be read and modified via the fourth field of
287.IR /proc/sys/kernel/sem .
c952e226 288.\" This /proc file is not available in Linux 2.2 and earlier -- MTK
fea681da
MK
289.TP
290.B SEMMSL
8a3e6dc7
MK
291Maximum number of semaphores per semaphore ID.
292On Linux systems before version 3.19,
293the default value for this limit was 250.
294Since Linux 3.19,
295.\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
296the default value is 32,000.
297On Linux, this limit can be read and modified via the first field of
298.IR /proc/sys/kernel/sem .
fea681da
MK
299.TP
300.B SEMMNS
80816b0b 301System-wide limit on the number of semaphores: policy dependent
c952e226
MK
302(on Linux, this limit can be read and modified via the second field of
303.IR /proc/sys/kernel/sem ).
8f891eb0
MK
304Note that the number of semaphores system-wide
305is also limited by the product of
96c1bf31
MK
306.B SEMMSL
307and
308.BR SEMMNI .
fea681da 309.SH BUGS
682edefb
MK
310The name choice
311.B IPC_PRIVATE
9091595d
MK
312was perhaps unfortunate,
313.B IPC_NEW
fea681da 314would more clearly show its function.
47297adb 315.SH SEE ALSO
fea681da
MK
316.BR semctl (2),
317.BR semop (2),
318.BR ftok (3),
f675ea37 319.BR capabilities (7),
2c5e151c 320.BR sem_overview (7),
343cdc5a 321.BR sysvipc (7)