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 2020-04-11 "Linux" "Linux Programmer's Manual"
39 semget \- get a System V semaphore set identifier
42 .B #include <sys/types.h>
43 .B #include <sys/ipc.h>
44 .B #include <sys/sem.h>
47 .BI "int semget(key_t " key ,
53 system call returns the System\ V semaphore set identifier
54 associated with the argument
56 It may be used either to obtain the identifier of a previously created
61 does not have the value
63 or to create a new set.
67 semaphores is created if
71 or if no existing semaphore set is associated with
84 and a semaphore set already exists for
92 (This is analogous to the effect of the combination
97 Upon creation, the least significant 9 bits of the argument
99 define the permissions (for owner, group, and others)
100 for the semaphore set.
101 These bits have the same format, and the same
106 (though the execute permissions are
107 not meaningful for semaphores, and write permissions mean permission
108 to alter semaphore values).
110 When creating a new semaphore set,
112 initializes the set's associated data structure,
121 are set to the effective user ID of the calling process.
126 are set to the effective group ID of the calling process.
128 The least significant 9 bits of
130 are set to the least significant 9 bits of
134 is set to the value of
141 is set to the current time.
147 when a semaphore set is not being created.
150 must be greater than 0
151 and less than or equal to the maximum number of semaphores per semaphore set
154 If the semaphore set already exists, the permissions are
156 .\" and a check is made to see if it is marked for destruction.
160 returns the semaphore set identifier (a nonnegative integer).
161 On failure, \-1 is returned, and
163 is set to indicate the error.
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
172 capability in the user namespace that governs its IPC namespace.
180 but a semaphore set already exists for
184 .\" The semaphore set is marked to be deleted.
188 is less than 0 or greater than the limit on the number
189 of semaphores per semaphore set
193 A semaphore set corresponding to
197 is larger than the number of semaphores in that set.
200 No semaphore set exists for
208 A semaphore set has to be created but the system does not have
209 enough memory for the new data structure.
212 A semaphore set has to be created but the system limit for the maximum
213 number of semaphore sets
215 or the system wide maximum number of semaphores
220 .\" SVr4 documents additional error conditions EFBIG, E2BIG, EAGAIN,
227 isn't required on Linux or by any version of POSIX.
229 some old implementations required the inclusion of these header files,
230 and the SVID also documented their inclusion.
231 Applications intended to be portable to such old systems may need
232 to include these header files.
233 .\" Like Linux, the FreeBSD man pages still document
234 .\" the inclusion of these header files.
237 isn't a flag field but a
240 If this special value is used for
242 the system call ignores all but the least significant 9 bits of
244 and creates a new semaphore set (on success).
246 .SS Semaphore initialization
247 The values of the semaphores in a newly created set are indeterminate.
248 (POSIX.1-2001 and POSIX.1-2008 are explicit on this point,
249 although POSIX.1-2008 notes that a future version of the standard
250 may require an implementation to initialize the semaphores to 0.)
251 Although Linux, like many other implementations,
252 initializes the semaphore values to 0,
253 a portable application cannot rely on this:
254 it should explicitly initialize the semaphores to the desired values.
255 .\" In truth, every one of the many implementations that I've tested sets
256 .\" the values to zero, but I suppose there is/was some obscure
257 .\" implementation out there that does not.
259 Initialization can be done using
265 Where multiple peers do not know who will be the first to
266 initialize the set, checking for a nonzero
268 in the associated data structure retrieved by a
271 operation can be used to avoid races.
274 The following limits on semaphore set resources affect the
279 System-wide limit on the number of semaphore sets.
280 On Linux systems before version 3.19,
281 the default value for this limit was 128.
283 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
284 the default value is 32,000.
285 On Linux, this limit can be read and modified via the fourth field of
286 .IR /proc/sys/kernel/sem .
287 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
290 Maximum number of semaphores per semaphore ID.
291 On Linux systems before version 3.19,
292 the default value for this limit was 250.
294 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
295 the default value is 32,000.
296 On Linux, this limit can be read and modified via the first field of
297 .IR /proc/sys/kernel/sem .
300 System-wide limit on the number of semaphores: policy dependent
301 (on Linux, this limit can be read and modified via the second field of
302 .IR /proc/sys/kernel/sem ).
303 Note that the number of semaphores system-wide
304 is also limited by the product of
311 was perhaps unfortunate,
313 would more clearly show its function.
315 The program shown below uses
317 to create a new semaphore set or retrieve the ID of an existing set.
324 The first two command-line arguments are used as the
330 The third command-line argument is an integer that specifies the
334 Command-line options can be used to specify the
340 flags for the call to
342 The usage of this program is demonstrated below.
344 We first create two files that will be used to generate keys using
346 create two semaphore sets using those files, and then list the sets using
351 $ \fBtouch mykey mykey2\fP
352 $ \fB./t_semget \-c mykey p 1\fP
354 $ \fB./t_semget \-c mykey2 p 2\fP
358 \-\-\-\-\-\- Semaphore Arrays \-\-\-\-\-\-\-\-
359 key semid owner perms nsems
360 0x7004136d 9 mtk 600 1
361 0x70041368 10 mtk 600 2
365 Next, we demonstrate that when
369 (as generated by the same arguments to
371 it returns the ID of the already existing semaphore set:
375 $ \fB./t_semget \-c mykey p 1\fP
380 Finally, we demonstrate the kind of collision that can occur when
384 arguments that have the same inode number:
388 $ \fBln mykey link\fP
389 $ \fBls \-i1 link mykey\fP
392 $ \fB./t_semget link p 1\fP # Generates same key as \(aqmykey\(aq
401 Licensed under GNU General Public License v2 or later.
403 #include <sys/types.h>
406 #include <sys/stat.h>
412 usage(const char *pname)
414 fprintf(stderr, "Usage: %s [\-cx] pathname proj\-id num\-sems\en",
416 fprintf(stderr, " \-c Use IPC_CREAT flag\en");
417 fprintf(stderr, " \-x Use IPC_EXCL flag\en");
422 main(int argc, char *argv[])
424 int semid, nsems, flags, opt;
428 while ((opt = getopt(argc, argv, "cx")) != \-1) {
430 case \(aqc\(aq: flags |= IPC_CREAT; break;
431 case \(aqx\(aq: flags |= IPC_EXCL; break;
432 default: usage(argv[0]);
436 if (argc != optind + 3)
439 key = ftok(argv[optind], argv[optind + 1][0]);
445 nsems = atoi(argv[optind + 2]);
447 semid = semget(key, nsems, flags | 0600);
453 printf("ID = %d\en", semid);
462 .BR capabilities (7),
463 .BR sem_overview (7),