1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\" and Copyright 2004, 2005 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" Modified Tue Oct 22 17:53:56 1996 by Eric S. Raymond <esr@thyrsus.com>
25 .\" Modified Fri Jun 19 10:59:15 1998 by Andries Brouwer <aeb@cwi.nl>
26 .\" Modified Sun Feb 18 01:59:29 2001 by Andries Brouwer <aeb@cwi.nl>
27 .\" Modified 20 Dec 2001, Michael Kerrisk <mtk.manpages@gmail.com>
28 .\" Modified 21 Dec 2001, aeb
29 .\" Modified 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
30 .\" Added notes on CAP_IPC_OWNER requirement
31 .\" Modified 17 Jun 2004, Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" Added notes on CAP_SYS_ADMIN requirement for IPC_SET and IPC_RMID
33 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\" Language and formatting clean-ups
35 .\" Rewrote semun text
36 .\" Added semid_ds and ipc_perm structure definitions
37 .\" 2005-08-02, mtk: Added IPC_INFO, SEM_INFO, SEM_STAT descriptions.
39 .TH SEMCTL 2 2008-08-06 "Linux" "Linux Programmer's Manual"
41 semctl \- semaphore control operations
44 .B #include <sys/types.h>
45 .B #include <sys/ipc.h>
46 .B #include <sys/sem.h>
48 .BI "int semctl(int " semid ", int " semnum ", int " cmd ", ...);"
52 performs the control operation specified by
54 on the semaphore set identified by
58 semaphore of that set.
59 (The semaphores in a set are numbered starting at 0.)
61 This function has three or four arguments, depending on
63 When there are four, the fourth has the type
65 The \fIcalling program\fP must define this union as follows:
70 int val; /* Value for SETVAL */
71 struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
72 unsigned short *array; /* Array for GETALL, SETALL */
73 struct seminfo *__buf; /* Buffer for IPC_INFO
81 data structure is defined in \fI<sys/sem.h>\fP as follows:
86 struct ipc_perm sem_perm; /* Ownership and permissions */
87 time_t sem_otime; /* Last semop time */
88 time_t sem_ctime; /* Last change time */
89 unsigned short sem_nsems; /* No. of semaphores in set */
96 structure is defined in \fI<sys/ipc.h>\fP as follows
97 (the highlighted fields are settable using
103 key_t __key; /* Key supplied to semget(2) */
104 uid_t \fBuid\fP; /* Effective UID of owner */
105 gid_t \fBgid\fP; /* Effective GID of owner */
106 uid_t cuid; /* Effective UID of creator */
107 gid_t cgid; /* Effective GID of creator */
108 unsigned short \fBmode\fP; /* Permissions */
109 unsigned short __seq; /* Sequence number */
119 Copy information from the kernel data structure associated with
123 structure pointed to by
128 The calling process must have read permission on the semaphore set.
131 Write the values of some members of the
133 structure pointed to by
135 to the kernel data structure associated with this semaphore set,
139 The following members of the structure are updated:
142 and (the least significant 9 bits of)
144 The effective UID of the calling process must match the owner
147 .RI ( sem_perm.cuid )
148 of the semaphore set, or the caller must be privileged.
154 Immediately remove the semaphore set,
155 awakening all processes blocked in
157 calls on the set (with an error return and
161 The effective user ID of the calling process must
162 match the creator or owner of the semaphore set,
163 or the caller must be privileged.
168 .BR IPC_INFO " (Linux-specific)"
169 Returns information about system-wide semaphore limits and
170 parameters in the structure pointed to by
172 This structure is of type
178 feature test macro is defined:
183 int semmap; /* Number of entries in semaphore
184 map; unused within kernel */
185 int semmni; /* Maximum number of semaphore sets */
186 int semmns; /* Maximum number of semaphores in all
188 int semmnu; /* System-wide maximum number of undo
189 structures; unused within kernel */
190 int semmsl; /* Maximum number of semaphores in a
192 int semopm; /* Maximum number of operations for
194 int semume; /* Maximum number of undo entries per
195 process; unused within kernel */
196 int semusz; /* Size of struct sem_undo */
197 int semvmx; /* Maximum semaphore value */
198 int semaem; /* Max. value that can be recorded for
199 semaphore adjustment (SEM_UNDO) */
210 settings can be changed via
211 .IR /proc/sys/kernel/sem ;
216 .BR SEM_INFO " (Linux-specific)"
219 structure containing the same information as for
221 except that the following fields are returned with information
222 about system resources consumed by semaphores: the
224 field returns the number of semaphore sets that currently exist
225 on the system; and the
227 field returns the total number of semaphores in all semaphore sets
230 .BR SEM_STAT " (Linux-specific)"
237 argument is not a semaphore identifier, but instead an index into
238 the kernel's internal array that maintains information about
239 all semaphore sets on the system.
244 (i.e., the current value)
245 for all semaphores of the set into
250 The calling process must have read permission on the semaphore set.
253 The system call returns the value of
255 (i.e., the number of processes waiting for the value of
256 this semaphore to increase) for the
259 (i.e., the number of processes waiting for an increase of
263 semaphore of the set).
264 The calling process must have read permission on the semaphore set.
267 The system call returns the value of
272 (i.e. the PID of the process that executed the last
276 semaphore of the set).
277 The calling process must have read permission on the semaphore set.
280 The system call returns the value of
284 semaphore of the set.
285 The calling process must have read permission on the semaphore set.
288 The system call returns the value of
290 (i.e., the number of processes waiting for the value of
291 this semaphore to become zero) for the
294 (i.e. the number of processes waiting for
298 semaphore of the set to become 0).
299 The calling process must have read permission on the semaphore set.
304 for all semaphores of the set using
310 structure associated with the set.
313 are cleared for altered semaphores in all processes.
314 If the changes to semaphore values would permit blocked
316 calls in other processes to proceed, then those processes are woken up.
320 The calling process must have alter (write) permission on
330 semaphore of the set, updating also the
334 structure associated with the set.
335 Undo entries are cleared for altered semaphores in all processes.
336 If the changes to semaphore values would permit blocked
338 calls in other processes to proceed, then those processes are woken up.
339 The calling process must have alter permission on the semaphore set.
346 indicating the error.
348 Otherwise the system call returns a non-negative value depending on
369 the index of the highest used entry in the
370 kernel's internal array recording information about all
372 (This information can be used with repeated
374 operations to obtain information about all semaphore sets on the system.)
381 the identifier of the semaphore set whose index was given in
386 values return 0 on success.
390 will be set to one of the following:
395 has one of the values
406 and the calling process does not have the required
407 permissions on the semaphore set and does not have the
412 The address pointed to by
419 The semaphore set was removed.
428 operation, the index value specified in
430 referred to an array slot that is currently unused.
439 but the effective user ID of the calling process is not the creator
445 of the semaphore set,
446 and the process does not have the
457 and the value to which
459 is to be set (for some semaphore of the set) is less than 0
460 or greater than the implementation limit
464 .\" SVr4 documents more error conditions EINVAL and EOVERFLOW.
471 operations are used by the
473 program to provide information on allocated resources.
474 In the future these may modified or moved to a /proc file system
477 Various fields in a \fIstruct semid_ds\fP were typed as
483 To take advantage of this,
484 a recompilation under glibc-2.1.91 or later should suffice.
485 (The kernel distinguishes old and new calls by an
490 In some earlier versions of glibc, the
492 union was defined in \fI<sys/sem.h>\fP, but POSIX.1-2001 requires
493 that the caller define this union.
494 On versions of glibc where this union is \fInot\fP defined,
496 .B _SEM_SEMUN_UNDEFINED
497 is defined in \fI<sys/sem.h>\fP.
499 The following system limit on semaphore sets affects a
506 implementation dependent (32767).
508 For greater portability it is best to always call
515 .BR capabilities (7),
516 .BR sem_overview (7),