1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
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>
25 .\" Modified 4 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
26 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
27 .\" Added notes on capability requirements
28 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
29 .\" Language and formatting clean-ups
30 .\" Added notes on /proc files
31 .\" Rewrote BUGS note about semget()'s failure to initialize
34 .TH SEMGET 2 2004-05-27 "Linux" "Linux Programmer's Manual"
36 semget \- get a semaphore set identifier
39 .B #include <sys/types.h>
40 .B #include <sys/ipc.h>
41 .B #include <sys/sem.h>
44 .BI "int semget(key_t " key ,
50 system call returns the semaphore set identifier
51 associated with the argument
55 semaphores is created if
59 or if no existing semaphore set is associated with
72 and a semaphore set already exists for
80 (This is analogous to the effect of the combination
85 Upon creation, the least significant 9 bits of the argument
87 define the permissions (for owner, group and others)
88 for the semaphore set.
89 These bits have the same format, and the same
94 (though the execute permissions are
95 not meaningful for semaphores, and write permissions mean permission
96 to alter semaphore values).
98 The values of the semaphores in a newly created set are indeterminate.
99 (POSIX.1-2001 is explicit on this point.)
100 Although Linux, like many other implementations,
101 initializes the semaphore values to 0,
102 a portable application cannot rely on this:
103 it should explicitly initialize the semaphores to the desired values.
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
106 .\" implementation out there that does not.
108 When creating a new semaphore set,
110 initializes the set's associated data structure,
119 are set to the effective user ID of the calling process.
124 are set to the effective group ID of the calling process.
126 The least significant 9 bits of
128 are set to the least significant 9 bits of
132 is set to the value of
139 is set to the current time.
145 when a semaphore set is not being created.
148 must be greater than 0
149 and less than or equal to the maximum number of semaphores per semaphore set
152 If the semaphore set already exists, the permissions are
154 .\" and a check is made to see if it is marked for destruction.
156 If successful, the return value will be the semaphore set identifier
157 (a nonnegative integer), otherwise \-1
160 indicating the error.
164 will be set to one of the following:
167 A semaphore set exists for
169 but the calling process does not have permission to access the set,
170 and does not have the
175 A semaphore set exists for
185 .\" The semaphore set is marked to be deleted.
189 is less than 0 or greater than the limit on the number
190 of semaphores per semaphore set
192 or a semaphore set corresponding to
196 is larger than the number of semaphores in that set.
199 No semaphore set exists for
207 A semaphore set has to be created but the system does not have
208 enough memory for the new data structure.
211 A semaphore set has to be created but the system limit for the maximum
212 number of semaphore sets
214 or the system wide maximum number of semaphores
219 .\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN,
223 isn't a flag field but a
226 If this special value is used for
228 the system call ignores everything but the least significant 9 bits of
230 and creates a new semaphore set (on success).
232 The following limits on semaphore set resources affect the
237 System wide maximum number of semaphore sets: policy dependent
238 (on Linux, this limit can be read and modified via the fourth field of
239 .IR /proc/sys/kernel/sem ).
240 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
243 Maximum number of semaphores per semid: implementation dependent
244 (on Linux, this limit can be read and modified via the first field of
245 .IR /proc/sys/kernel/sem ).
248 System wide maximum number of semaphores: policy dependent
249 (on Linux, this limit can be read and modified via the second field of
250 .IR /proc/sys/kernel/sem ).
257 was perhaps unfortunate,
259 would more clearly show its function.
261 The semaphores in a set are not initialized by
263 .\" In fact they are initialized to zero on Linux, but POSIX.1-2001
264 .\" does not specify this, and we can't portably rely on it.
265 In order to initialize the semaphores,
267 must be used to perform a
271 operation on the semaphore set.
272 (Where multiple peers do not know who will be the first to
273 initialize the set, checking for a nonzero
275 in the associated data structure retrieved by a
278 operation can be used to avoid races.)
283 .BR capabilities (7),
284 .BR sem_overview (7),