1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" Modified 1996-10-22, Eric S. Raymond <esr@thyrsus.com>
6 .\" Modified 2002-01-08, Michael Kerrisk <mtk.manpages@gmail.com>
7 .\" Modified 2003-04-28, Ernie Petrides <petrides@redhat.com>
8 .\" Modified 2004-05-27, Michael Kerrisk <mtk.manpages@gmail.com>
9 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
10 .\" Language and formatting clean-ups
11 .\" Added notes on /proc files
12 .\" 2005-04-08, mtk, Noted kernel version numbers for semtimedop()
13 .\" 2007-07-09, mtk, Added an EXAMPLE code segment.
15 .TH semop 2 (date) "Linux man-pages (unreleased)"
17 semop, semtimedop \- System V semaphore operations
20 .RI ( libc ", " \-lc )
23 .B #include <sys/sem.h>
25 .BI "int semop(int " semid ", struct sembuf *" sops ", size_t " nsops );
26 .BI "int semtimedop(int " semid ", struct sembuf *" sops ", size_t " nsops ,
27 .BI " const struct timespec *_Nullable " timeout );
31 Feature Test Macro Requirements for glibc (see
32 .BR feature_test_macros (7)):
40 Each semaphore in a System\ V semaphore set
41 has the following associated values:
45 unsigned short semval; /* semaphore value */
46 unsigned short semzcnt; /* # waiting for zero */
47 unsigned short semncnt; /* # waiting for increase */
48 pid_t sempid; /* PID of process that last
49 modified the semaphore value */
54 performs operations on selected semaphores in the set indicated by
58 elements in the array pointed to by
61 specifies an operation to be performed on a single semaphore.
62 The elements of this structure are of type
64 containing the following members:
68 unsigned short sem_num; /* semaphore number */
69 short sem_op; /* semaphore operation */
70 short sem_flg; /* operation flags */
80 If an operation specifies
82 it will be automatically undone when the process terminates.
84 The set of operations contained in
90 that is, the operations are performed either as a complete unit,
92 The behavior of the system call if not all operations can be
93 performed immediately depends on the presence of the
95 flag in the individual
97 fields, as noted below.
99 Each operation is performed on the
101 semaphore of the semaphore set, where the first semaphore of the set
103 There are three types of operation, distinguished by the value of
108 is a positive integer, the operation adds this value to
113 is specified for this operation, the system subtracts the value
115 from the semaphore adjustment
117 value for this semaphore.
118 This operation can always proceed\[em]it never forces a thread to wait.
119 The calling process must have alter permission on the semaphore set.
123 is zero, the process must have read permission on the semaphore
125 This is a "wait-for-zero" operation: if
127 is zero, the operation can immediately proceed.
137 (and none of the operations in
142 (the count of threads waiting until this semaphore's value becomes zero)
143 is incremented by one and the thread sleeps until
144 one of the following occurs:
147 becomes 0, at which time the value of
159 The calling thread catches a signal:
171 is less than zero, the process must have alter permission on the
175 is greater than or equal to the absolute value of
177 the operation can proceed immediately:
178 the absolute value of
184 is specified for this operation, the system adds the absolute value of
186 to the semaphore adjustment
188 value for this semaphore.
189 If the absolute value of
202 (and none of the operations in
207 (the counter of threads waiting for this semaphore's value to increase)
208 is incremented by one and the thread sleeps until
209 one of the following occurs:
212 becomes greater than or equal to the absolute value of
214 the operation now proceeds, as described above.
216 The semaphore set is removed from the system:
223 The calling thread catches a signal:
233 On successful completion, the
235 value for each semaphore specified in the array pointed to by
237 is set to the caller's process ID.
242 is set to the current time.
245 behaves identically to
247 except that in those cases where the calling thread would sleep,
248 the duration of that sleep is limited by the amount of elapsed
249 time specified by the
251 structure whose address is passed in the
254 (This sleep interval will be rounded up to the system clock granularity,
255 and kernel scheduling delays mean that the interval
256 may overrun by a small amount.)
257 If the specified time limit has been reached,
263 (and none of the operations in
276 is interrupted by a signal, causing the call to fail with the error
287 On failure, they return \-1, and set
289 to indicate the error.
297 the maximum number of operations allowed per system
301 The calling process does not have the permissions required
302 to perform the specified semaphore operations,
303 and does not have the
305 capability in the user namespace that governs its IPC namespace.
308 An operation could not proceed immediately and either
312 or the time limit specified in
317 An address specified in either the
321 argument isn't accessible.
324 For some operation the value of
326 is less than 0 or greater than or equal to the number
327 of semaphores in the set.
330 The semaphore set was removed.
333 While blocked in this system call, the thread caught a signal; see
337 The semaphore set doesn't exist, or
339 is less than zero, or
341 has a nonpositive value.
346 of some operation specified
348 and the system does not have enough memory to allocate the undo
356 the implementation dependent maximum value for
361 Linux 2.5.52 (backported into Linux 2.4.22),
364 .\" SVr4 documents additional error conditions EINVAL, EFBIG, ENOSPC.
368 structures of a process aren't inherited by the child produced by
370 but they are inherited across an
375 is never automatically restarted after being interrupted by a signal handler,
376 regardless of the setting of the
378 flag when establishing a signal handler.
380 A semaphore adjustment
382 value is a per-process, per-semaphore integer that is the negated sum
383 of all operations performed on a semaphore specifying the
386 Each process has a list of
388 values\[em]one value for each semaphore on which it has operated using
390 When a process terminates, each of its per-semaphore
392 values is added to the corresponding semaphore,
393 thus undoing the effect of that process's operations on the semaphore
394 (but see BUGS below).
395 When a semaphore's value is directly set using the
403 values in all processes are cleared.
407 flag allows more than one process to share a
413 The \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, and \fIsemnct\fP values
414 for a semaphore can all be retrieved using appropriate
418 The following limits on semaphore set resources affect the
423 Maximum number of operations allowed for one
427 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
428 the default value for this limit was 32.
429 Since Linux 3.19, the default value is 500.
430 On Linux, this limit can be read and modified via the third field of
431 .IR /proc/sys/kernel/sem .
432 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
434 this limit should not be raised above 1000,
435 .\" See comment in Linux 3.19 source file include/uapi/linux/sem.h
436 because of the risk of that
438 fails due to kernel memory fragmentation when allocating memory to copy the
443 Maximum allowable value for
445 implementation dependent (32767).
447 The implementation has no intrinsic limits for
448 the adjust on exit maximum value
450 the system wide maximum number of undo structures
452 and the per-process maximum number of undo entries system parameters.
454 When a process terminates, its set of associated
456 structures is used to undo the effect of all of the
457 semaphore operations it performed with the
460 This raises a difficulty: if one (or more) of these semaphore adjustments
461 would result in an attempt to decrease a semaphore's value below zero,
462 what should an implementation do?
463 One possible approach would be to block until all the semaphore
464 adjustments could be performed.
465 This is however undesirable since it could force process termination to
466 block for arbitrarily long periods.
467 Another possibility is that such semaphore adjustments could be ignored
468 altogether (somewhat analogously to failing when
470 is specified for a semaphore operation).
471 Linux adopts a third approach: decreasing the semaphore value
472 as far as possible (i.e., to zero) and allowing process
473 termination to proceed immediately.
475 In Linux 2.6.x, x <= 10, there is a bug that in some circumstances
476 prevents a thread that is waiting for a semaphore value to become
477 zero from being woken up when the value does actually become zero.
478 This bug is fixed in Linux 2.6.11.
480 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
482 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
484 The following code segment uses
486 to atomically wait for the value of semaphore 0 to become zero,
487 and then increment the semaphore value by one.
491 struct sembuf sops[2];
494 /* Code to set \fIsemid\fP omitted */
496 sops[0].sem_num = 0; /* Operate on semaphore 0 */
497 sops[0].sem_op = 0; /* Wait for value to equal 0 */
500 sops[1].sem_num = 0; /* Operate on semaphore 0 */
501 sops[1].sem_op = 1; /* Increment value by one */
504 if (semop(semid, sops, 2) == \-1) {
511 A further example of the use of
520 .BR capabilities (7),
521 .BR sem_overview (7),