1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright (C) 2020 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Modified Tue Oct 22 17:54:56 1996 by Eric S. Raymond <esr@thyrsus.com>
7 .\" Modified 1 Jan 2002, Martin Schulze <joey@infodrom.org>
8 .\" Modified 4 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
9 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
10 .\" Added notes on capability requirements
11 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
12 .\" Language and formatting clean-ups
13 .\" Added notes on /proc files
14 .\" Rewrote BUGS note about semget()'s failure to initialize
17 .TH semget 2 (date) "Linux man-pages (unreleased)"
19 semget \- get a System V semaphore set identifier
22 .RI ( libc ", " \-lc )
25 .B #include <sys/sem.h>
28 .BI "int semget(key_t " key ,
34 system call returns the System\ V semaphore set identifier
35 associated with the argument
37 It may be used either to obtain the identifier of a previously created
42 does not have the value
44 or to create a new set.
48 semaphores is created if
52 or if no existing semaphore set is associated with
65 and a semaphore set already exists for
73 (This is analogous to the effect of the combination
78 Upon creation, the least significant 9 bits of the argument
80 define the permissions (for owner, group, and others)
81 for the semaphore set.
82 These bits have the same format, and the same
87 (though the execute permissions are
88 not meaningful for semaphores, and write permissions mean permission
89 to alter semaphore values).
91 When creating a new semaphore set,
93 initializes the set's associated data structure,
102 are set to the effective user ID of the calling process.
107 are set to the effective group ID of the calling process.
109 The least significant 9 bits of
111 are set to the least significant 9 bits of
115 is set to the value of
122 is set to the current time.
128 when a semaphore set is not being created.
131 must be greater than 0
132 and less than or equal to the maximum number of semaphores per semaphore set
135 If the semaphore set already exists, the permissions are
137 .\" and a check is made to see if it is marked for destruction.
141 returns the semaphore set identifier (a nonnegative integer).
142 On failure, \-1 is returned, and
144 is set to indicate the error.
148 A semaphore set exists for
150 but the calling process does not have permission to access the set,
151 and does not have the
153 capability in the user namespace that governs its IPC namespace.
161 but a semaphore set already exists for
165 .\" The semaphore set is marked to be deleted.
169 is less than 0 or greater than the limit on the number
170 of semaphores per semaphore set
174 A semaphore set corresponding to
178 is larger than the number of semaphores in that set.
181 No semaphore set exists for
189 A semaphore set has to be created but the system does not have
190 enough memory for the new data structure.
193 A semaphore set has to be created but the system limit for the maximum
194 number of semaphore sets
196 or the system wide maximum number of semaphores
203 .\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN,
207 isn't a flag field but a
210 If this special value is used for
212 the system call ignores all but the least significant 9 bits of
214 and creates a new semaphore set (on success).
216 .SS Semaphore initialization
217 The values of the semaphores in a newly created set are indeterminate.
218 (POSIX.1-2001 and POSIX.1-2008 are explicit on this point,
219 although POSIX.1-2008 notes that a future version of the standard
220 may require an implementation to initialize the semaphores to 0.)
221 Although Linux, like many other implementations,
222 initializes the semaphore values to 0,
223 a portable application cannot rely on this:
224 it should explicitly initialize the semaphores to the desired values.
225 .\" In truth, every one of the many implementations that I've tested sets
226 .\" the values to zero, but I suppose there is/was some obscure
227 .\" implementation out there that does not.
229 Initialization can be done using
235 Where multiple peers do not know who will be the first to
236 initialize the set, checking for a nonzero
238 in the associated data structure retrieved by a
241 operation can be used to avoid races.
244 The following limits on semaphore set resources affect the
249 System-wide limit on the number of semaphore sets.
251 the default value for this limit was 128.
253 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
254 the default value is 32,000.
255 On Linux, this limit can be read and modified via the fourth field of
256 .IR /proc/sys/kernel/sem .
257 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
260 Maximum number of semaphores per semaphore ID.
262 the default value for this limit was 250.
264 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
265 the default value is 32,000.
266 On Linux, this limit can be read and modified via the first field of
267 .IR /proc/sys/kernel/sem .
270 System-wide limit on the number of semaphores: policy dependent
271 (on Linux, this limit can be read and modified via the second field of
272 .IR /proc/sys/kernel/sem ).
273 Note that the number of semaphores system-wide
274 is also limited by the product of
281 was perhaps unfortunate,
283 would more clearly show its function.
285 The program shown below uses
287 to create a new semaphore set or retrieve the ID of an existing set.
294 The first two command-line arguments are used as the
300 The third command-line argument is an integer that specifies the
304 Command-line options can be used to specify the
310 flags for the call to
312 The usage of this program is demonstrated below.
314 We first create two files that will be used to generate keys using
316 create two semaphore sets using those files, and then list the sets using
321 $ \fBtouch mykey mykey2\fP
322 $ \fB./t_semget \-c mykey p 1\fP
324 $ \fB./t_semget \-c mykey2 p 2\fP
328 \-\-\-\-\-\- Semaphore Arrays \-\-\-\-\-\-\-\-
329 key semid owner perms nsems
330 0x7004136d 9 mtk 600 1
331 0x70041368 10 mtk 600 2
335 Next, we demonstrate that when
339 (as generated by the same arguments to
341 it returns the ID of the already existing semaphore set:
345 $ \fB./t_semget \-c mykey p 1\fP
350 Finally, we demonstrate the kind of collision that can occur when
354 arguments that have the same inode number:
358 $ \fBln mykey link\fP
359 $ \fBls \-i1 link mykey\fP
362 $ \fB./t_semget link p 1\fP # Generates same key as \[aq]mykey\[aq]
368 .\" SRC BEGIN (t_semget.c)
372 Licensed under GNU General Public License v2 or later.
381 usage(const char *pname)
383 fprintf(stderr, "Usage: %s [\-cx] pathname proj\-id num\-sems\en",
385 fprintf(stderr, " \-c Use IPC_CREAT flag\en");
386 fprintf(stderr, " \-x Use IPC_EXCL flag\en");
391 main(int argc, char *argv[])
393 int semid, nsems, flags, opt;
397 while ((opt = getopt(argc, argv, "cx")) != \-1) {
399 case \[aq]c\[aq]: flags |= IPC_CREAT; break;
400 case \[aq]x\[aq]: flags |= IPC_EXCL; break;
401 default: usage(argv[0]);
405 if (argc != optind + 3)
408 key = ftok(argv[optind], argv[optind + 1][0]);
414 nsems = atoi(argv[optind + 2]);
416 semid = semget(key, nsems, flags | 0600);
422 printf("ID = %d\en", semid);
432 .BR capabilities (7),
433 .BR sem_overview (7),