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 2021-03-22 "Linux" "Linux Programmer's Manual"
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
201 .\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN,
205 isn't a flag field but a
208 If this special value is used for
210 the system call ignores all but the least significant 9 bits of
212 and creates a new semaphore set (on success).
214 .SS Semaphore initialization
215 The values of the semaphores in a newly created set are indeterminate.
216 (POSIX.1-2001 and POSIX.1-2008 are explicit on this point,
217 although POSIX.1-2008 notes that a future version of the standard
218 may require an implementation to initialize the semaphores to 0.)
219 Although Linux, like many other implementations,
220 initializes the semaphore values to 0,
221 a portable application cannot rely on this:
222 it should explicitly initialize the semaphores to the desired values.
223 .\" In truth, every one of the many implementations that I've tested sets
224 .\" the values to zero, but I suppose there is/was some obscure
225 .\" implementation out there that does not.
227 Initialization can be done using
233 Where multiple peers do not know who will be the first to
234 initialize the set, checking for a nonzero
236 in the associated data structure retrieved by a
239 operation can be used to avoid races.
242 The following limits on semaphore set resources affect the
247 System-wide limit on the number of semaphore sets.
248 On Linux systems before version 3.19,
249 the default value for this limit was 128.
251 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
252 the default value is 32,000.
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
258 Maximum number of semaphores per semaphore ID.
259 On Linux systems before version 3.19,
260 the default value for this limit was 250.
262 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
263 the default value is 32,000.
264 On Linux, this limit can be read and modified via the first field of
265 .IR /proc/sys/kernel/sem .
268 System-wide limit on the number of semaphores: policy dependent
269 (on Linux, this limit can be read and modified via the second field of
270 .IR /proc/sys/kernel/sem ).
271 Note that the number of semaphores system-wide
272 is also limited by the product of
279 was perhaps unfortunate,
281 would more clearly show its function.
283 The program shown below uses
285 to create a new semaphore set or retrieve the ID of an existing set.
292 The first two command-line arguments are used as the
298 The third command-line argument is an integer that specifies the
302 Command-line options can be used to specify the
308 flags for the call to
310 The usage of this program is demonstrated below.
312 We first create two files that will be used to generate keys using
314 create two semaphore sets using those files, and then list the sets using
319 $ \fBtouch mykey mykey2\fP
320 $ \fB./t_semget \-c mykey p 1\fP
322 $ \fB./t_semget \-c mykey2 p 2\fP
326 \-\-\-\-\-\- Semaphore Arrays \-\-\-\-\-\-\-\-
327 key semid owner perms nsems
328 0x7004136d 9 mtk 600 1
329 0x70041368 10 mtk 600 2
333 Next, we demonstrate that when
337 (as generated by the same arguments to
339 it returns the ID of the already existing semaphore set:
343 $ \fB./t_semget \-c mykey p 1\fP
348 Finally, we demonstrate the kind of collision that can occur when
352 arguments that have the same inode number:
356 $ \fBln mykey link\fP
357 $ \fBls \-i1 link mykey\fP
360 $ \fB./t_semget link p 1\fP # Generates same key as \(aqmykey\(aq
366 .\" SRC BEGIN (t_semget.c)
370 Licensed under GNU General Public License v2 or later.
379 usage(const char *pname)
381 fprintf(stderr, "Usage: %s [\-cx] pathname proj\-id num\-sems\en",
383 fprintf(stderr, " \-c Use IPC_CREAT flag\en");
384 fprintf(stderr, " \-x Use IPC_EXCL flag\en");
389 main(int argc, char *argv[])
391 int semid, nsems, flags, opt;
395 while ((opt = getopt(argc, argv, "cx")) != \-1) {
397 case \(aqc\(aq: flags |= IPC_CREAT; break;
398 case \(aqx\(aq: flags |= IPC_EXCL; break;
399 default: usage(argv[0]);
403 if (argc != optind + 3)
406 key = ftok(argv[optind], argv[optind + 1][0]);
412 nsems = atoi(argv[optind + 2]);
414 semid = semget(key, nsems, flags | 0600);
420 printf("ID = %d\en", semid);
430 .BR capabilities (7),
431 .BR sem_overview (7),