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.
41 .TH SEMCTL 2 2015-08-08 "Linux" "Linux Programmer's Manual"
43 semctl \- System V semaphore control operations
46 .B #include <sys/types.h>
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; /* Last change time */
91 unsigned long sem_nsems; /* No. of semaphores in set */
98 structure is defined as follows
99 (the highlighted fields are settable using
105 key_t __key; /* Key supplied to semget(2) */
106 uid_t \fBuid\fP; /* Effective UID of owner */
107 gid_t \fBgid\fP; /* Effective GID of owner */
108 uid_t cuid; /* Effective UID of creator */
109 gid_t cgid; /* Effective GID of creator */
110 unsigned short \fBmode\fP; /* Permissions */
111 unsigned short __seq; /* Sequence number */
121 Copy information from the kernel data structure associated with
125 structure pointed to by
130 The calling process must have read permission on the semaphore set.
133 Write the values of some members of the
135 structure pointed to by
137 to the kernel data structure associated with this semaphore set,
141 The following members of the structure are updated:
144 and (the least significant 9 bits of)
146 The effective UID of the calling process must match the owner
149 .RI ( sem_perm.cuid )
150 of the semaphore set, or the caller must be privileged.
156 Immediately remove the semaphore set,
157 awakening all processes blocked in
159 calls on the set (with an error return and
163 The effective user ID of the calling process must
164 match the creator or owner of the semaphore set,
165 or the caller must be privileged.
170 .BR IPC_INFO " (Linux-specific)"
171 Return information about system-wide semaphore limits and
172 parameters in the structure pointed to by
174 This structure is of type
180 feature test macro is defined:
185 int semmap; /* Number of entries in semaphore
186 map; unused within kernel */
187 int semmni; /* Maximum number of semaphore sets */
188 int semmns; /* Maximum number of semaphores in all
190 int semmnu; /* System-wide maximum number of undo
191 structures; unused within kernel */
192 int semmsl; /* Maximum number of semaphores in a
194 int semopm; /* Maximum number of operations for
196 int semume; /* Maximum number of undo entries per
197 process; unused within kernel */
198 int semusz; /* Size of struct sem_undo */
199 int semvmx; /* Maximum semaphore value */
200 int semaem; /* Max. value that can be recorded for
201 semaphore adjustment (SEM_UNDO) */
212 settings can be changed via
213 .IR /proc/sys/kernel/sem ;
218 .BR SEM_INFO " (Linux-specific)"
221 structure containing the same information as for
223 except that the following fields are returned with information
224 about system resources consumed by semaphores: the
226 field returns the number of semaphore sets that currently exist
227 on the system; and the
229 field returns the total number of semaphores in all semaphore sets
232 .BR SEM_STAT " (Linux-specific)"
239 argument is not a semaphore identifier, but instead an index into
240 the kernel's internal array that maintains information about
241 all semaphore sets on the system.
246 (i.e., the current value)
247 for all semaphores of the set into
252 The calling process must have read permission on the semaphore set.
260 (i.e., the number of processes waiting for an increase of
264 semaphore of the set).
265 The calling process must have read permission on the semaphore set.
272 semaphore of the set.
273 This is the PID of the process that last performed an operation on
274 that semaphore (but see NOTES).
275 The calling process must have read permission on the semaphore set.
282 semaphore of the set.
283 The calling process must have read permission on the semaphore set.
291 (i.e., the number of processes waiting for
295 semaphore of the set to become 0).
296 The calling process must have read permission on the semaphore set.
301 for all semaphores of the set using
307 structure associated with the set.
310 are cleared for altered semaphores in all processes.
311 If the changes to semaphore values would permit blocked
313 calls in other processes to proceed, then those processes are woken up.
317 The calling process must have alter (write) permission on
327 semaphore of the set, updating also the
331 structure associated with the set.
332 Undo entries are cleared for altered semaphores in all processes.
333 If the changes to semaphore values would permit blocked
335 calls in other processes to proceed, then those processes are woken up.
336 The calling process must have alter permission on the semaphore set.
343 indicating the error.
345 Otherwise, the system call returns a nonnegative value depending on
366 the index of the highest used entry in the
367 kernel's internal array recording information about all
369 (This information can be used with repeated
371 operations to obtain information about all semaphore sets on the system.)
378 the identifier of the semaphore set whose index was given in
383 values return 0 on success.
387 will be set to one of the following:
392 has one of the values
403 and the calling process does not have the required
404 permissions on the semaphore set and does not have the
409 The address pointed to by
416 The semaphore set was removed.
425 operation, the index value specified in
427 referred to an array slot that is currently unused.
436 but the effective user ID of the calling process is not the creator
442 of the semaphore set,
443 and the process does not have the
454 and the value to which
456 is to be set (for some semaphore of the set) is less than 0
457 or greater than the implementation limit
460 POSIX.1-2001, POSIX.1-2001, SVr4.
461 .\" SVr4 documents more error conditions EINVAL and EOVERFLOW.
463 POSIX.1 specifies the
464 .\" POSIX.1-2001, POSIX.1-2008
468 structure as having the type
469 .IR "unsigned\ short" ,
470 and the field is so defined on most other systems.
471 It was also so defined on Linux 2.2 and earlier,
472 but, since Linux 2.4, the field has the type
473 .IR "unsigned\ long" .
479 isn't required on Linux or by any version of POSIX.
481 some old implementations required the inclusion of these header files,
482 and the SVID also documented their inclusion.
483 Applications intended to be portable to such old systems may need
484 to include these header files.
485 .\" Like Linux, the FreeBSD man pages still document
486 .\" the inclusion of these header files.
493 operations are used by the
495 program to provide information on allocated resources.
496 In the future these may modified or moved to a
498 filesystem interface.
500 Various fields in a \fIstruct semid_ds\fP were typed as
506 To take advantage of this,
507 a recompilation under glibc-2.1.91 or later should suffice.
508 (The kernel distinguishes old and new calls by an
513 In some earlier versions of glibc, the
515 union was defined in \fI<sys/sem.h>\fP, but POSIX.1 requires
516 .\" POSIX.1-2001, POSIX.1-2008
517 that the caller define this union.
518 On versions of glibc where this union is \fInot\fP defined,
520 .B _SEM_SEMUN_UNDEFINED
521 is defined in \fI<sys/sem.h>\fP.
523 The following system limit on semaphore sets affects a
530 implementation dependent (32767).
532 For greater portability, it is best to always call
539 as the "process ID of [the] last operation" on a semaphore,
540 and explicitly notes that this value is set by a successful
542 call, with the implication that no other interface affects the
546 While some implementations conform to the behavior specified in POSIX.1,
548 (The fault here probably lies with POSIX.1 inasmuch as it likely failed
549 to capture the full range of existing implementation behaviors.)
550 Various other implementations
551 .\" At least OpenSolaris (and, one supposes, older Solaris) and Darwin
554 for the other operations that update the value of a semaphore: the
558 operations, as well as the semaphore adjustments performed
559 on process termination as a consequence of the use of the
568 operations and semaphore adjustments.
569 However, somewhat inconsistently, it does not update
576 behavior might be viewed as a bug, the behavior is longstanding,
577 and is probably unlikely to change.
582 .BR capabilities (7),
583 .BR sem_overview (7),