]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/semget.2
Start of 2.53
[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>
305a0578
MK
25.\" Modified 4 Jan 2002, Michael Kerrisk <mtk-manpages@gmx.net>
26.\" Modified, 27 May 2004, Michael Kerrisk <mtk-manpages@gmx.net>
fea681da 27.\" Added notes on capability requirements
c952e226
MK
28.\" Modified, 11 Nov 2004, Michael Kerrisk <mtk-manpages@gmx.net>
29.\" Language and formatting clean-ups
30.\" Added notes on /proc files
704a18f0 31.\" Rewrote BUGS note about semget()'s failure to initialise
c952e226 32.\" semaphore values
fea681da
MK
33.\"
34.TH SEMGET 2 2004-05-27 "Linux 2.6.6" "Linux Programmer's Manual"
35.SH NAME
36semget \- get a semaphore set identifier
37.SH SYNOPSIS
38.nf
39.B
40#include <sys/types.h>
41.B
42#include <sys/ipc.h>
43.B
44#include <sys/sem.h>
45.fi
46.sp
47.BI "int semget(key_t " key ,
48.BI "int " nsems ,
49.BI "int " semflg );
50.SH DESCRIPTION
c952e226
MK
51The
52.BR semget ()
53system call returns the semaphore set identifier
fea681da
MK
54associated with the argument
55.IR key .
56A new set of
57.I nsems
58semaphores is created if
59.I key
60has the value
61.B IPC_PRIVATE
540036b2 62or if no existing semaphore set is associated with
fea681da
MK
63.I key
64and
65.B IPC_CREAT
c952e226
MK
66is specified in
67.IR semflg .
fea681da 68.PP
c952e226 69If
fea681da 70.I semflg
c952e226 71specifies both
fea681da
MK
72.B IPC_CREAT
73and
74.B IPC_EXCL
75and a semaphore set already exists for
c952e226
MK
76.IR key ,
77then
78.BR semget ()
c13182ef 79fails with
c952e226
MK
80.I errno
81set to
82.BR EEXIST .
c13182ef 83(This is analogous to the effect of the combination
c952e226
MK
84.B O_CREAT | O_EXCL
85for
86.BR open (2).)
fea681da 87.PP
c952e226 88Upon creation, the least significant 9 bits of the argument
fea681da 89.I semflg
c952e226 90define the permissions (for owner, group and others)
fea681da
MK
91for the semaphore set.
92These bits have the same format, and the same
c952e226
MK
93meaning, as the
94.I mode
95argument of
fea681da 96.BR open (2)
c952e226 97(though the execute permissions are
fea681da
MK
98not meaningful for semaphores, and write permissions mean permission
99to alter semaphore values).
100.PP
2c5e151c 101The values of the semaphores in a newly created set are indeterminate.
c13182ef 102(POSIX.1-2001 is explicit on this point.)
2c5e151c
MK
103Although Linux, like many other implementations,
104initialises the semaphore values to 0,
105a portable application cannot rely on this:
106it should explicitly initialise the semaphores to the desired values.
c13182ef
MK
107.\" In truth, every one of the many implementations that I've tested sets
108.\" the values to zero, but I suppose there is/was some obscure
2c5e151c
MK
109.\" implementation out there that does not.
110.PP
fea681da 111When creating a new semaphore set,
c952e226 112.BR semget ()
2c5e151c 113initialises the set's associated data structure,
c952e226 114.I semid_ds
c13182ef 115(see
2c5e151c 116.BR semctl (2)),
fea681da
MK
117as follows:
118.IP
c952e226 119.I sem_perm.cuid
fea681da 120and
c952e226 121.I sem_perm.uid
499d62f2 122are set to the effective user ID of the calling process.
fea681da 123.IP
c952e226 124.I sem_perm.cgid
fea681da 125and
c952e226 126.I sem_perm.gid
499d62f2 127are set to the effective group ID of the calling process.
fea681da 128.IP
c952e226
MK
129The least significant 9 bits of
130.I sem_perm.mode
131are set to the least significant 9 bits of
fea681da
MK
132.IR semflg .
133.IP
c952e226 134.I sem_nsems
fea681da
MK
135is set to the value of
136.IR nsems .
137.IP
c952e226 138.I sem_otime
fea681da
MK
139is set to 0.
140.IP
c952e226 141.I sem_ctime
fea681da
MK
142is set to the current time.
143.PP
144The argument
145.I nsems
c952e226 146can be 0
fea681da
MK
147(a don't care)
148when a semaphore set is not being created.
149Otherwise
150.I nsems
c952e226 151must be greater than 0
fea681da
MK
152and less than or equal to the maximum number of semaphores per semaphore set
153.RB ( SEMMSL ).
154.PP
c952e226 155If the semaphore set already exists, the permissions are
fea681da
MK
156verified.
157.\" and a check is made to see if it is marked for destruction.
158.SH "RETURN VALUE"
159If successful, the return value will be the semaphore set identifier
18be658d 160(a non-negative integer), otherwise \-1
fea681da
MK
161is returned, with
162.I errno
163indicating the error.
164.SH ERRORS
165On failure
166.I errno
167will be set to one of the following:
168.TP 11
169.B EACCES
170A semaphore set exists for
171.IR key ,
172but the calling process does not have permission to access the set,
173and does not have the
174.BR CAP_IPC_OWNER
175capability.
176.TP
177.B EEXIST
178A semaphore set exists for
179.B key
180and
181.I semflg
c952e226 182specified both
fea681da
MK
183.B IPC_CREAT
184and
185.BR IPC_EXCL .
186.\" .TP
187.\" .B EIDRM
188.\" The semaphore set is marked to be deleted.
189.TP
190.B EINVAL
191.IR nsems
192is less than 0 or greater than the limit on the number
193of semaphores per semaphore set
194.RB ( SEMMSL ),
195or a semaphore set corresponding to
196.I key
197already exists, and
198.I nsems
199is larger than the number of semaphores in that set.
200.TP
201.B ENOENT
202No semaphore set exists for
203.I key
204and
205.I semflg
c952e226 206did not specify
fea681da
MK
207.BR IPC_CREAT .
208.TP
209.B ENOMEM
c952e226
MK
210A semaphore set has to be created but the system does not have
211enough memory for the new data structure.
fea681da
MK
212.TP
213.B ENOSPC
214A semaphore set has to be created but the system limit for the maximum
215number of semaphore sets
216.RB ( SEMMNI ),
217or the system wide maximum number of semaphores
218.RB ( SEMMNS ),
219would be exceeded.
220.SH NOTES
221.B IPC_PRIVATE
222isn't a flag field but a
c952e226 223.I key_t
fea681da
MK
224type.
225If this special value is used for
226.IR key ,
c952e226 227the system call ignores everything but the least significant 9 bits of
fea681da
MK
228.I semflg
229and creates a new semaphore set (on success).
230.PP
540036b2 231The following limits on semaphore set resources affect the
c952e226 232.BR semget ()
fea681da
MK
233call:
234.TP 11
235.B SEMMNI
c952e226
MK
236System wide maximum number of semaphore sets: policy dependent
237(on Linux, this limit can be read and modified via the fourth field of
238.IR /proc/sys/kernel/sem ).
239.\" This /proc file is not available in Linux 2.2 and earlier -- MTK
fea681da
MK
240.TP
241.B SEMMSL
242Maximum number of semaphores per semid: implementation dependent
c952e226
MK
243(on Linux, this limit can be read and modified via the first field of
244.IR /proc/sys/kernel/sem ).
fea681da
MK
245.TP
246.B SEMMNS
c952e226
MK
247System wide maximum number of semaphores: policy dependent
248(on Linux, this limit can be read and modified via the second field of
249.IR /proc/sys/kernel/sem ).
fea681da
MK
250Values greater than
251.B SEMMSL * SEMMNI
252makes it irrelevant.
253.SH BUGS
254The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW
255would more clearly show its function.
256.LP
c952e226
MK
257The semaphores in a set are not initialised by
258.BR semget ().
c13182ef 259.\" In fact they are initialised to zero on Linux, but POSIX.1-2001
704a18f0 260.\" does not specify this, and we can't portably rely on it.
c952e226 261In order to initialise the semaphores,
fea681da 262.BR semctl (2)
c952e226 263must be used to perform a
fea681da
MK
264.B SETVAL
265or a
266.B SETALL
c952e226
MK
267operation on the semaphore set.
268(Where multiple peers do not know who will be the first to
269initialise the set, checking for a non-zero
270.I sem_otime
271in the associated data structure retrieved by a
0bfa087b 272.BR semctl (2)
c952e226
MK
273.B IPC_STAT
274operation can be used to avoid races.)
fea681da 275.SH "CONFORMING TO"
97c1eac8
MK
276SVr4, POSIX.1-2001.
277.\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN,
278.\" ERANGE, EFAULT.
fea681da
MK
279.SH "SEE ALSO"
280.BR semctl (2),
281.BR semop (2),
282.BR ftok (3),
f675ea37 283.BR capabilities (7),
2c5e151c
MK
284.BR sem_overview (7),
285.BR svipc (7)