]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/semget.2
semget.2: grfix
[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.\"
f6652463 36.TH SEMGET 2 2012-05-31 "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
MK
44.fi
45.sp
46.BI "int semget(key_t " key ,
47.BI "int " nsems ,
48.BI "int " semflg );
49.SH DESCRIPTION
c952e226
MK
50The
51.BR semget ()
47f065d6 52system call returns the System V semaphore set identifier
fea681da
MK
53associated with the argument
54.IR key .
55A new set of
56.I nsems
57semaphores is created if
58.I key
59has the value
60.B IPC_PRIVATE
540036b2 61or if no existing semaphore set is associated with
fea681da
MK
62.I key
63and
64.B IPC_CREAT
c952e226
MK
65is specified in
66.IR semflg .
fea681da 67.PP
c952e226 68If
fea681da 69.I semflg
c952e226 70specifies both
fea681da
MK
71.B IPC_CREAT
72and
73.B IPC_EXCL
74and a semaphore set already exists for
c952e226
MK
75.IR key ,
76then
77.BR semget ()
c13182ef 78fails with
c952e226
MK
79.I errno
80set to
81.BR EEXIST .
c13182ef 82(This is analogous to the effect of the combination
c952e226
MK
83.B O_CREAT | O_EXCL
84for
85.BR open (2).)
fea681da 86.PP
c952e226 87Upon creation, the least significant 9 bits of the argument
fea681da 88.I semflg
c952e226 89define the permissions (for owner, group and others)
fea681da
MK
90for the semaphore set.
91These bits have the same format, and the same
c952e226
MK
92meaning, as the
93.I mode
94argument of
fea681da 95.BR open (2)
c952e226 96(though the execute permissions are
fea681da
MK
97not meaningful for semaphores, and write permissions mean permission
98to alter semaphore values).
99.PP
2c5e151c 100The values of the semaphores in a newly created set are indeterminate.
c13182ef 101(POSIX.1-2001 is explicit on this point.)
2c5e151c 102Although Linux, like many other implementations,
d9bfdb9c 103initializes the semaphore values to 0,
2c5e151c 104a portable application cannot rely on this:
d9bfdb9c 105it should explicitly initialize the semaphores to the desired values.
c13182ef
MK
106.\" In truth, every one of the many implementations that I've tested sets
107.\" the values to zero, but I suppose there is/was some obscure
2c5e151c
MK
108.\" implementation out there that does not.
109.PP
fea681da 110When creating a new semaphore set,
c952e226 111.BR semget ()
d9bfdb9c 112initializes the set's associated data structure,
c952e226 113.I semid_ds
c13182ef 114(see
2c5e151c 115.BR semctl (2)),
fea681da
MK
116as follows:
117.IP
c952e226 118.I sem_perm.cuid
fea681da 119and
c952e226 120.I sem_perm.uid
499d62f2 121are set to the effective user ID of the calling process.
fea681da 122.IP
c952e226 123.I sem_perm.cgid
fea681da 124and
c952e226 125.I sem_perm.gid
499d62f2 126are set to the effective group ID of the calling process.
fea681da 127.IP
c952e226
MK
128The least significant 9 bits of
129.I sem_perm.mode
130are set to the least significant 9 bits of
fea681da
MK
131.IR semflg .
132.IP
c952e226 133.I sem_nsems
fea681da
MK
134is set to the value of
135.IR nsems .
136.IP
c952e226 137.I sem_otime
fea681da
MK
138is set to 0.
139.IP
c952e226 140.I sem_ctime
fea681da
MK
141is set to the current time.
142.PP
143The argument
144.I nsems
c952e226 145can be 0
fea681da
MK
146(a don't care)
147when a semaphore set is not being created.
e9001b56 148Otherwise,
fea681da 149.I nsems
c952e226 150must be greater than 0
fea681da
MK
151and less than or equal to the maximum number of semaphores per semaphore set
152.RB ( SEMMSL ).
153.PP
c952e226 154If the semaphore set already exists, the permissions are
fea681da
MK
155verified.
156.\" and a check is made to see if it is marked for destruction.
47297adb 157.SH RETURN VALUE
fea681da 158If successful, the return value will be the semaphore set identifier
32cdf39c 159(a nonnegative integer), otherwise, \-1
fea681da
MK
160is returned, with
161.I errno
162indicating the error.
163.SH ERRORS
90db854a 164On failure,
fea681da
MK
165.I errno
166will be set to one of the following:
89b3c6b8 167.TP
fea681da
MK
168.B EACCES
169A semaphore set exists for
170.IR key ,
171but the calling process does not have permission to access the set,
172and does not have the
0daa9e92 173.B CAP_IPC_OWNER
fea681da
MK
174capability.
175.TP
176.B EEXIST
177A semaphore set exists for
66ee0c7e 178.I key
fea681da
MK
179and
180.I semflg
c952e226 181specified both
fea681da
MK
182.B IPC_CREAT
183and
184.BR IPC_EXCL .
185.\" .TP
186.\" .B EIDRM
187.\" The semaphore set is marked to be deleted.
188.TP
189.B EINVAL
0daa9e92 190.I nsems
fea681da
MK
191is less than 0 or greater than the limit on the number
192of semaphores per semaphore set
193.RB ( SEMMSL ),
194or a semaphore set corresponding to
195.I key
196already exists, and
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
f6652463
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.
236
fea681da
MK
237.B IPC_PRIVATE
238isn't a flag field but a
c952e226 239.I key_t
fea681da
MK
240type.
241If this special value is used for
242.IR key ,
c952e226 243the system call ignores everything but the least significant 9 bits of
fea681da
MK
244.I semflg
245and creates a new semaphore set (on success).
246.PP
540036b2 247The following limits on semaphore set resources affect the
c952e226 248.BR semget ()
fea681da 249call:
89b3c6b8 250.TP
fea681da 251.B SEMMNI
c952e226
MK
252System wide maximum number of semaphore sets: policy dependent
253(on Linux, this limit can be read and modified via the fourth field of
254.IR /proc/sys/kernel/sem ).
255.\" This /proc file is not available in Linux 2.2 and earlier -- MTK
fea681da
MK
256.TP
257.B SEMMSL
258Maximum number of semaphores per semid: implementation dependent
c952e226
MK
259(on Linux, this limit can be read and modified via the first field of
260.IR /proc/sys/kernel/sem ).
fea681da
MK
261.TP
262.B SEMMNS
c952e226
MK
263System wide maximum number of semaphores: policy dependent
264(on Linux, this limit can be read and modified via the second field of
265.IR /proc/sys/kernel/sem ).
fea681da
MK
266Values greater than
267.B SEMMSL * SEMMNI
268makes it irrelevant.
269.SH BUGS
682edefb
MK
270The name choice
271.B IPC_PRIVATE
9091595d
MK
272was perhaps unfortunate,
273.B IPC_NEW
fea681da
MK
274would more clearly show its function.
275.LP
d9bfdb9c 276The semaphores in a set are not initialized by
c952e226 277.BR semget ().
d9bfdb9c 278.\" In fact they are initialized to zero on Linux, but POSIX.1-2001
704a18f0 279.\" does not specify this, and we can't portably rely on it.
d9bfdb9c 280In order to initialize the semaphores,
fea681da 281.BR semctl (2)
c952e226 282must be used to perform a
fea681da
MK
283.B SETVAL
284or a
285.B SETALL
c952e226
MK
286operation on the semaphore set.
287(Where multiple peers do not know who will be the first to
c7094399 288initialize the set, checking for a nonzero
c952e226
MK
289.I sem_otime
290in the associated data structure retrieved by a
0bfa087b 291.BR semctl (2)
c952e226
MK
292.B IPC_STAT
293operation can be used to avoid races.)
47297adb 294.SH SEE ALSO
fea681da
MK
295.BR semctl (2),
296.BR semop (2),
297.BR ftok (3),
f675ea37 298.BR capabilities (7),
2c5e151c
MK
299.BR sem_overview (7),
300.BR svipc (7)