1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright 2004, 2005 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:53:56 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified Fri Jun 19 10:59:15 1998 by Andries Brouwer <aeb@cwi.nl>
28 .\" Modified Sun Feb 18 01:59:29 2001 by Andries Brouwer <aeb@cwi.nl>
29 .\" Modified 20 Dec 2001, Michael Kerrisk <mtk.manpages@gmail.com>
30 .\" Modified 21 Dec 2001, aeb
31 .\" Modified 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" Added notes on CAP_IPC_OWNER requirement
33 .\" Modified 17 Jun 2004, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\" Added notes on CAP_SYS_ADMIN requirement for IPC_SET and IPC_RMID
35 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" Language and formatting clean-ups
37 .\" Rewrote semun text
38 .\" Added semid_ds and ipc_perm structure definitions
39 .\" 2005-08-02, mtk: Added IPC_INFO, SEM_INFO, SEM_STAT descriptions.
40 .\" 2018-03-20, dbueso: Added SEM_STAT_ANY description.
42 .TH SEMCTL 2 2021-03-22 "Linux" "Linux Programmer's Manual"
44 semctl \- System V semaphore control operations
47 .B #include <sys/ipc.h>
48 .B #include <sys/sem.h>
50 .BI "int semctl(int " semid ", int " semnum ", int " cmd ", ...);"
54 performs the control operation specified by
56 on the System\ V semaphore set identified by
60 semaphore of that set.
61 (The semaphores in a set are numbered starting at 0.)
63 This function has three or four arguments, depending on
65 When there are four, the fourth has the type
67 The \fIcalling program\fP must define this union as follows:
72 int val; /* Value for SETVAL */
73 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
74 unsigned short *array; /* Array for GETALL, SETALL */
75 struct seminfo *__buf; /* Buffer for IPC_INFO
83 data structure is defined in \fI<sys/sem.h>\fP as follows:
88 struct ipc_perm sem_perm; /* Ownership and permissions */
89 time_t sem_otime; /* Last semop time */
90 time_t sem_ctime; /* Creation time/time of last
91 modification via semctl() */
92 unsigned long sem_nsems; /* No. of semaphores in set */
99 structure are as follows:
104 structure (see below) that specifies the access permissions on the semaphore
113 Time of creation of semaphore set or time of last
122 Number of semaphores in the set.
123 Each semaphore of the set is referenced by a nonnegative integer
131 structure is defined as follows
132 (the highlighted fields are settable using
138 key_t __key; /* Key supplied to semget(2) */
139 uid_t \fBuid\fP; /* Effective UID of owner */
140 gid_t \fBgid\fP; /* Effective GID of owner */
141 uid_t cuid; /* Effective UID of creator */
142 gid_t cgid; /* Effective GID of creator */
143 unsigned short \fBmode\fP; /* Permissions */
144 unsigned short __seq; /* Sequence number */
149 The least significant 9 bits of the
153 structure define the access permissions for the shared memory segment.
154 The permission bits are as follows:
165 In effect, "write" means "alter" for a semaphore set.
166 Bits 0100, 0010, and 0001 (the execute bits) are unused by the system.
173 Copy information from the kernel data structure associated with
177 structure pointed to by
182 The calling process must have read permission on the semaphore set.
185 Write the values of some members of the
187 structure pointed to by
189 to the kernel data structure associated with this semaphore set,
194 The following members of the structure are updated:
197 and (the least significant 9 bits of)
200 The effective UID of the calling process must match the owner
203 .RI ( sem_perm.cuid )
204 of the semaphore set, or the caller must be privileged.
210 Immediately remove the semaphore set,
211 awakening all processes blocked in
213 calls on the set (with an error return and
217 The effective user ID of the calling process must
218 match the creator or owner of the semaphore set,
219 or the caller must be privileged.
224 .BR IPC_INFO " (Linux\-specific)"
225 Return information about system-wide semaphore limits and
226 parameters in the structure pointed to by
228 This structure is of type
234 feature test macro is defined:
239 int semmap; /* Number of entries in semaphore
240 map; unused within kernel */
241 int semmni; /* Maximum number of semaphore sets */
242 int semmns; /* Maximum number of semaphores in all
244 int semmnu; /* System\-wide maximum number of undo
245 structures; unused within kernel */
246 int semmsl; /* Maximum number of semaphores in a
248 int semopm; /* Maximum number of operations for
250 int semume; /* Maximum number of undo entries per
251 process; unused within kernel */
252 int semusz; /* Size of struct sem_undo */
253 int semvmx; /* Maximum semaphore value */
254 int semaem; /* Max. value that can be recorded for
255 semaphore adjustment (SEM_UNDO) */
266 settings can be changed via
267 .IR /proc/sys/kernel/sem ;
272 .BR SEM_INFO " (Linux-specific)"
275 structure containing the same information as for
277 except that the following fields are returned with information
278 about system resources consumed by semaphores: the
280 field returns the number of semaphore sets that currently exist
281 on the system; and the
283 field returns the total number of semaphores in all semaphore sets
286 .BR SEM_STAT " (Linux-specific)"
293 argument is not a semaphore identifier, but instead an index into
294 the kernel's internal array that maintains information about
295 all semaphore sets on the system.
297 .BR SEM_STAT_ANY " (Linux-specific, since Linux 4.17)"
304 is not checked for read access for
306 meaning that any user can employ this operation (just as any user may read
307 .IR /proc/sysvipc/sem
308 to obtain the same information).
313 (i.e., the current value)
314 for all semaphores of the set into
319 The calling process must have read permission on the semaphore set.
327 (i.e., the number of processes waiting for the semaphore's value to increase).
328 The calling process must have read permission on the semaphore set.
335 semaphore of the set.
336 This is the PID of the process that last performed an operation on
337 that semaphore (but see NOTES).
338 The calling process must have read permission on the semaphore set.
343 (i.e., the semaphore value) for the
345 semaphore of the set.
346 The calling process must have read permission on the semaphore set.
354 (i.e., the number of processes waiting for the semaphore value to become 0).
355 The calling process must have read permission on the semaphore set.
360 values for all semaphores of the set using
366 structure associated with the set.
369 are cleared for altered semaphores in all processes.
370 If the changes to semaphore values would permit blocked
372 calls in other processes to proceed, then those processes are woken up.
376 The calling process must have alter (write) permission on
380 Set the semaphore value
386 semaphore of the set, updating also the
390 structure associated with the set.
391 Undo entries are cleared for altered semaphores in all processes.
392 If the changes to semaphore values would permit blocked
394 calls in other processes to proceed, then those processes are woken up.
395 The calling process must have alter permission on the semaphore set.
399 returns a nonnegative value depending on
420 the index of the highest used entry in the
421 kernel's internal array recording information about all
423 (This information can be used with repeated
427 operations to obtain information about all semaphore sets on the system.)
434 the identifier of the semaphore set whose index was given in
443 values return 0 on success.
449 to indicate the error.
455 has one of the values
467 and the calling process does not have the required
468 permissions on the semaphore set and does not have the
470 capability in the user namespace that governs its IPC namespace.
473 The address pointed to by
480 The semaphore set was removed.
489 operation, the index value specified in
491 referred to an array slot that is currently unused.
500 but the effective user ID of the calling process is not the creator
506 of the semaphore set,
507 and the process does not have the
518 and the value to which
520 is to be set (for some semaphore of the set) is less than 0
521 or greater than the implementation limit
524 POSIX.1-2001, POSIX.1-2008, SVr4.
525 .\" SVr4 documents more error conditions EINVAL and EOVERFLOW.
527 POSIX.1 specifies the
528 .\" POSIX.1-2001, POSIX.1-2008
532 structure as having the type
533 .IR "unsigned\ short" ,
534 and the field is so defined on most other systems.
535 It was also so defined on Linux 2.2 and earlier,
536 but, since Linux 2.4, the field has the type
537 .IR "unsigned\ long" .
541 isn't required on Linux or by any version of POSIX.
543 some old implementations required the inclusion of this header file,
544 and the SVID also documented its inclusion.
545 Applications intended to be portable to such old systems may need
546 to include this header file.
547 .\" Like Linux, the FreeBSD man pages still document
548 .\" the inclusion of these header files.
555 operations are used by the
557 program to provide information on allocated resources.
558 In the future these may modified or moved to a
560 filesystem interface.
562 Various fields in a \fIstruct semid_ds\fP were typed as
568 To take advantage of this,
569 a recompilation under glibc-2.1.91 or later should suffice.
570 (The kernel distinguishes old and new calls by an
575 In some earlier versions of glibc, the
577 union was defined in \fI<sys/sem.h>\fP, but POSIX.1 requires
578 .\" POSIX.1-2001, POSIX.1-2008
579 that the caller define this union.
580 On versions of glibc where this union is \fInot\fP defined,
582 .B _SEM_SEMUN_UNDEFINED
583 is defined in \fI<sys/sem.h>\fP.
585 The following system limit on semaphore sets affects a
592 implementation dependent (32767).
594 For greater portability, it is best to always call
601 as the "process ID of [the] last operation" on a semaphore,
602 and explicitly notes that this value is set by a successful
604 call, with the implication that no other interface affects the
608 While some implementations conform to the behavior specified in POSIX.1,
610 (The fault here probably lies with POSIX.1 inasmuch as it likely failed
611 to capture the full range of existing implementation behaviors.)
612 Various other implementations
613 .\" At least OpenSolaris (and, one supposes, older Solaris) and Darwin
616 for the other operations that update the value of a semaphore: the
620 operations, as well as the semaphore adjustments performed
621 on process termination as a consequence of the use of the
630 operations and semaphore adjustments.
631 However, somewhat inconsistently, up to and including Linux 4.5,
632 the kernel did not update
638 .\" commit a5f4db877177d2a3d7ae62a7bac3a5a27e083d7f
647 .BR capabilities (7),
648 .BR sem_overview (7),