]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/semget.2
futex.2, netlink.3, stdin.3, wavelan.4, netlink.7: srcfix
[thirdparty/man-pages.git] / man2 / semget.2
CommitLineData
fea681da
MK
1.\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
c13182ef 11.\"
fea681da
MK
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
c13182ef 19.\"
fea681da
MK
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
23.\" Modified Tue Oct 22 17:54:56 1996 by Eric S. Raymond <esr@thyrsus.com>
24.\" Modified 1 Jan 2002, Martin Schulze <joey@infodrom.org>
c11b1abf
MK
25.\" Modified 4 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
26.\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 27.\" Added notes on capability requirements
c11b1abf 28.\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
c952e226
MK
29.\" Language and formatting clean-ups
30.\" Added notes on /proc files
d9bfdb9c 31.\" Rewrote BUGS note about semget()'s failure to initialize
c952e226 32.\" semaphore values
fea681da 33.\"
f6652463 34.TH SEMGET 2 2012-05-31 "Linux" "Linux Programmer's Manual"
fea681da
MK
35.SH NAME
36semget \- get a semaphore set identifier
37.SH SYNOPSIS
38.nf
2fadbfb5
MK
39.B #include <sys/types.h>
40.B #include <sys/ipc.h>
41.B #include <sys/sem.h>
fea681da
MK
42.fi
43.sp
44.BI "int semget(key_t " key ,
45.BI "int " nsems ,
46.BI "int " semflg );
47.SH DESCRIPTION
c952e226
MK
48The
49.BR semget ()
cfadad46 50system call returns the semaphore set identifier
fea681da
MK
51associated with the argument
52.IR key .
53A new set of
54.I nsems
55semaphores is created if
56.I key
57has the value
58.B IPC_PRIVATE
540036b2 59or if no existing semaphore set is associated with
fea681da
MK
60.I key
61and
62.B IPC_CREAT
c952e226
MK
63is specified in
64.IR semflg .
fea681da 65.PP
c952e226 66If
fea681da 67.I semflg
c952e226 68specifies both
fea681da
MK
69.B IPC_CREAT
70and
71.B IPC_EXCL
72and a semaphore set already exists for
c952e226
MK
73.IR key ,
74then
75.BR semget ()
c13182ef 76fails with
c952e226
MK
77.I errno
78set to
79.BR EEXIST .
c13182ef 80(This is analogous to the effect of the combination
c952e226
MK
81.B O_CREAT | O_EXCL
82for
83.BR open (2).)
fea681da 84.PP
c952e226 85Upon creation, the least significant 9 bits of the argument
fea681da 86.I semflg
c952e226 87define the permissions (for owner, group and others)
fea681da
MK
88for the semaphore set.
89These bits have the same format, and the same
c952e226
MK
90meaning, as the
91.I mode
92argument of
fea681da 93.BR open (2)
c952e226 94(though the execute permissions are
fea681da
MK
95not meaningful for semaphores, and write permissions mean permission
96to alter semaphore values).
97.PP
2c5e151c 98The values of the semaphores in a newly created set are indeterminate.
c13182ef 99(POSIX.1-2001 is explicit on this point.)
2c5e151c 100Although Linux, like many other implementations,
d9bfdb9c 101initializes the semaphore values to 0,
2c5e151c 102a portable application cannot rely on this:
d9bfdb9c 103it should explicitly initialize the semaphores to the desired values.
c13182ef
MK
104.\" In truth, every one of the many implementations that I've tested sets
105.\" the values to zero, but I suppose there is/was some obscure
2c5e151c
MK
106.\" implementation out there that does not.
107.PP
fea681da 108When creating a new semaphore set,
c952e226 109.BR semget ()
d9bfdb9c 110initializes the set's associated data structure,
c952e226 111.I semid_ds
c13182ef 112(see
2c5e151c 113.BR semctl (2)),
fea681da
MK
114as follows:
115.IP
c952e226 116.I sem_perm.cuid
fea681da 117and
c952e226 118.I sem_perm.uid
499d62f2 119are set to the effective user ID of the calling process.
fea681da 120.IP
c952e226 121.I sem_perm.cgid
fea681da 122and
c952e226 123.I sem_perm.gid
499d62f2 124are set to the effective group ID of the calling process.
fea681da 125.IP
c952e226
MK
126The least significant 9 bits of
127.I sem_perm.mode
128are set to the least significant 9 bits of
fea681da
MK
129.IR semflg .
130.IP
c952e226 131.I sem_nsems
fea681da
MK
132is set to the value of
133.IR nsems .
134.IP
c952e226 135.I sem_otime
fea681da
MK
136is set to 0.
137.IP
c952e226 138.I sem_ctime
fea681da
MK
139is set to the current time.
140.PP
141The argument
142.I nsems
c952e226 143can be 0
fea681da
MK
144(a don't care)
145when a semaphore set is not being created.
146Otherwise
147.I nsems
c952e226 148must be greater than 0
fea681da
MK
149and less than or equal to the maximum number of semaphores per semaphore set
150.RB ( SEMMSL ).
151.PP
c952e226 152If the semaphore set already exists, the permissions are
fea681da
MK
153verified.
154.\" and a check is made to see if it is marked for destruction.
47297adb 155.SH RETURN VALUE
fea681da 156If successful, the return value will be the semaphore set identifier
2fda57bd 157(a nonnegative integer), otherwise \-1
fea681da
MK
158is returned, with
159.I errno
160indicating the error.
161.SH ERRORS
162On failure
163.I errno
164will be set to one of the following:
89b3c6b8 165.TP
fea681da
MK
166.B EACCES
167A semaphore set exists for
168.IR key ,
169but the calling process does not have permission to access the set,
170and does not have the
0daa9e92 171.B CAP_IPC_OWNER
fea681da
MK
172capability.
173.TP
174.B EEXIST
175A semaphore set exists for
66ee0c7e 176.I key
fea681da
MK
177and
178.I semflg
c952e226 179specified both
fea681da
MK
180.B IPC_CREAT
181and
182.BR IPC_EXCL .
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
191.RB ( SEMMSL ),
192or a semaphore set corresponding to
193.I key
194already exists, and
195.I nsems
196is larger than the number of semaphores in that set.
197.TP
198.B ENOENT
199No semaphore set exists for
200.I key
201and
202.I semflg
c952e226 203did not specify
fea681da
MK
204.BR IPC_CREAT .
205.TP
206.B ENOMEM
c952e226
MK
207A semaphore set has to be created but the system does not have
208enough memory for the new data structure.
fea681da
MK
209.TP
210.B ENOSPC
211A semaphore set has to be created but the system limit for the maximum
212number of semaphore sets
213.RB ( SEMMNI ),
214or the system wide maximum number of semaphores
215.RB ( SEMMNS ),
216would be exceeded.
47297adb 217.SH CONFORMING TO
a1d5f77c
MK
218SVr4, POSIX.1-2001.
219.\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN,
220.\" ERANGE, EFAULT.
fea681da 221.SH NOTES
f6652463
MK
222The inclusion of
223.I <sys/types.h>
224and
225.I <sys/ipc.h>
226isn't required on Linux or by any version of POSIX.
227However,
228some old implementations required the inclusion of these header files,
229and the SVID also documented their inclusion.
230Applications intended to be portable to such old systems may need
231to include these header files.
232.\" Like Linux, the FreeBSD man pages still document
233.\" the inclusion of these header files.
234
fea681da
MK
235.B IPC_PRIVATE
236isn't a flag field but a
c952e226 237.I key_t
fea681da
MK
238type.
239If this special value is used for
240.IR key ,
c952e226 241the system call ignores everything but the least significant 9 bits of
fea681da
MK
242.I semflg
243and creates a new semaphore set (on success).
244.PP
540036b2 245The following limits on semaphore set resources affect the
c952e226 246.BR semget ()
fea681da 247call:
89b3c6b8 248.TP
fea681da 249.B SEMMNI
c952e226
MK
250System wide maximum number of semaphore sets: policy dependent
251(on Linux, this limit can be read and modified via the fourth field of
252.IR /proc/sys/kernel/sem ).
253.\" This /proc file is not available in Linux 2.2 and earlier -- MTK
fea681da
MK
254.TP
255.B SEMMSL
256Maximum number of semaphores per semid: implementation dependent
c952e226
MK
257(on Linux, this limit can be read and modified via the first field of
258.IR /proc/sys/kernel/sem ).
fea681da
MK
259.TP
260.B SEMMNS
c952e226
MK
261System wide maximum number of semaphores: policy dependent
262(on Linux, this limit can be read and modified via the second field of
263.IR /proc/sys/kernel/sem ).
fea681da
MK
264Values greater than
265.B SEMMSL * SEMMNI
266makes it irrelevant.
267.SH BUGS
682edefb
MK
268The name choice
269.B IPC_PRIVATE
9091595d
MK
270was perhaps unfortunate,
271.B IPC_NEW
fea681da
MK
272would more clearly show its function.
273.LP
d9bfdb9c 274The semaphores in a set are not initialized by
c952e226 275.BR semget ().
d9bfdb9c 276.\" In fact they are initialized to zero on Linux, but POSIX.1-2001
704a18f0 277.\" does not specify this, and we can't portably rely on it.
d9bfdb9c 278In order to initialize the semaphores,
fea681da 279.BR semctl (2)
c952e226 280must be used to perform a
fea681da
MK
281.B SETVAL
282or a
283.B SETALL
c952e226
MK
284operation on the semaphore set.
285(Where multiple peers do not know who will be the first to
c7094399 286initialize the set, checking for a nonzero
c952e226
MK
287.I sem_otime
288in the associated data structure retrieved by a
0bfa087b 289.BR semctl (2)
c952e226
MK
290.B IPC_STAT
291operation can be used to avoid races.)
47297adb 292.SH SEE ALSO
fea681da
MK
293.BR semctl (2),
294.BR semop (2),
295.BR ftok (3),
f675ea37 296.BR capabilities (7),
2c5e151c
MK
297.BR sem_overview (7),
298.BR svipc (7)