1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright (C) 2020 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified Tue Oct 22 17:54:56 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified 1 Jan 2002, Martin Schulze <joey@infodrom.org>
28 .\" Modified 4 Jan 2002, Michael Kerrisk <mtk.manpages@gmail.com>
29 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
30 .\" Added notes on capability requirements
31 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" Language and formatting clean-ups
33 .\" Added notes on /proc files
34 .\" Rewrote BUGS note about semget()'s failure to initialize
37 .TH SEMGET 2 2021-03-22 "Linux" "Linux Programmer's Manual"
39 semget \- get a System V semaphore set identifier
42 .B #include <sys/sem.h>
45 .BI "int semget(key_t " key ,
51 system call returns the System\ V semaphore set identifier
52 associated with the argument
54 It may be used either to obtain the identifier of a previously created
59 does not have the value
61 or to create a new set.
65 semaphores is created if
69 or if no existing semaphore set is associated with
82 and a semaphore set already exists for
90 (This is analogous to the effect of the combination
95 Upon creation, the least significant 9 bits of the argument
97 define the permissions (for owner, group, and others)
98 for the semaphore set.
99 These bits have the same format, and the same
104 (though the execute permissions are
105 not meaningful for semaphores, and write permissions mean permission
106 to alter semaphore values).
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.
158 returns the semaphore set identifier (a nonnegative integer).
159 On failure, \-1 is returned, and
161 is set to indicate the error.
165 A semaphore set exists for
167 but the calling process does not have permission to access the set,
168 and does not have the
170 capability in the user namespace that governs its IPC namespace.
178 but a semaphore set already exists for
182 .\" The semaphore set is marked to be deleted.
186 is less than 0 or greater than the limit on the number
187 of semaphores per semaphore set
191 A semaphore set corresponding to
195 is larger than the number of semaphores in that set.
198 No semaphore set exists for
206 A semaphore set has to be created but the system does not have
207 enough memory for the new data structure.
210 A semaphore set has to be created but the system limit for the maximum
211 number of semaphore sets
213 or the system wide maximum number of semaphores
218 .\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN,
222 isn't a flag field but a
225 If this special value is used for
227 the system call ignores all but the least significant 9 bits of
229 and creates a new semaphore set (on success).
231 .SS Semaphore initialization
232 The values of the semaphores in a newly created set are indeterminate.
233 (POSIX.1-2001 and POSIX.1-2008 are explicit on this point,
234 although POSIX.1-2008 notes that a future version of the standard
235 may require an implementation to initialize the semaphores to 0.)
236 Although Linux, like many other implementations,
237 initializes the semaphore values to 0,
238 a portable application cannot rely on this:
239 it should explicitly initialize the semaphores to the desired values.
240 .\" In truth, every one of the many implementations that I've tested sets
241 .\" the values to zero, but I suppose there is/was some obscure
242 .\" implementation out there that does not.
244 Initialization can be done using
250 Where multiple peers do not know who will be the first to
251 initialize the set, checking for a nonzero
253 in the associated data structure retrieved by a
256 operation can be used to avoid races.
259 The following limits on semaphore set resources affect the
264 System-wide limit on the number of semaphore sets.
265 On Linux systems before version 3.19,
266 the default value for this limit was 128.
268 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
269 the default value is 32,000.
270 On Linux, this limit can be read and modified via the fourth field of
271 .IR /proc/sys/kernel/sem .
272 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
275 Maximum number of semaphores per semaphore ID.
276 On Linux systems before version 3.19,
277 the default value for this limit was 250.
279 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
280 the default value is 32,000.
281 On Linux, this limit can be read and modified via the first field of
282 .IR /proc/sys/kernel/sem .
285 System-wide limit on the number of semaphores: policy dependent
286 (on Linux, this limit can be read and modified via the second field of
287 .IR /proc/sys/kernel/sem ).
288 Note that the number of semaphores system-wide
289 is also limited by the product of
296 was perhaps unfortunate,
298 would more clearly show its function.
300 The program shown below uses
302 to create a new semaphore set or retrieve the ID of an existing set.
309 The first two command-line arguments are used as the
315 The third command-line argument is an integer that specifies the
319 Command-line options can be used to specify the
325 flags for the call to
327 The usage of this program is demonstrated below.
329 We first create two files that will be used to generate keys using
331 create two semaphore sets using those files, and then list the sets using
336 $ \fBtouch mykey mykey2\fP
337 $ \fB./t_semget \-c mykey p 1\fP
339 $ \fB./t_semget \-c mykey2 p 2\fP
343 \-\-\-\-\-\- Semaphore Arrays \-\-\-\-\-\-\-\-
344 key semid owner perms nsems
345 0x7004136d 9 mtk 600 1
346 0x70041368 10 mtk 600 2
350 Next, we demonstrate that when
354 (as generated by the same arguments to
356 it returns the ID of the already existing semaphore set:
360 $ \fB./t_semget \-c mykey p 1\fP
365 Finally, we demonstrate the kind of collision that can occur when
369 arguments that have the same inode number:
373 $ \fBln mykey link\fP
374 $ \fBls \-i1 link mykey\fP
377 $ \fB./t_semget link p 1\fP # Generates same key as \(aqmykey\(aq
386 Licensed under GNU General Public License v2 or later.
388 #include <sys/types.h>
391 #include <sys/stat.h>
397 usage(const char *pname)
399 fprintf(stderr, "Usage: %s [\-cx] pathname proj\-id num\-sems\en",
401 fprintf(stderr, " \-c Use IPC_CREAT flag\en");
402 fprintf(stderr, " \-x Use IPC_EXCL flag\en");
407 main(int argc, char *argv[])
409 int semid, nsems, flags, opt;
413 while ((opt = getopt(argc, argv, "cx")) != \-1) {
415 case \(aqc\(aq: flags |= IPC_CREAT; break;
416 case \(aqx\(aq: flags |= IPC_EXCL; break;
417 default: usage(argv[0]);
421 if (argc != optind + 3)
424 key = ftok(argv[optind], argv[optind + 1][0]);
430 nsems = atoi(argv[optind + 2]);
432 semid = semget(key, nsems, flags | 0600);
438 printf("ID = %d\en", semid);
447 .BR capabilities (7),
448 .BR sem_overview (7),