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 2021-03-22 "Linux" "Linux Programmer's Manual"
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 *" 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
53 performs operations on selected semaphores in the set indicated by
57 elements in the array pointed to by
60 specifies an operation to be performed on a single semaphore.
61 The elements of this structure are of type
63 containing the following members:
67 unsigned short sem_num; /* semaphore number */
68 short sem_op; /* semaphore operation */
69 short sem_flg; /* operation flags */
79 If an operation specifies
81 it will be automatically undone when the process terminates.
83 The set of operations contained in
89 that is, the operations are performed either as a complete unit,
91 The behavior of the system call if not all operations can be
92 performed immediately depends on the presence of the
94 flag in the individual
96 fields, as noted below.
98 Each operation is performed on the
100 semaphore of the semaphore set, where the first semaphore of the set
102 There are three types of operation, distinguished by the value of
107 is a positive integer, the operation adds this value to
112 is specified for this operation, the system subtracts the value
114 from the semaphore adjustment
116 value for this semaphore.
117 This operation can always proceed\(emit never forces a thread to wait.
118 The calling process must have alter permission on the semaphore set.
122 is zero, the process must have read permission on the semaphore
124 This is a "wait-for-zero" operation: if
126 is zero, the operation can immediately proceed.
136 (and none of the operations in
141 (the count of threads waiting until this semaphore's value becomes zero)
142 is incremented by one and the thread sleeps until
143 one of the following occurs:
146 becomes 0, at which time the value of
158 The calling thread catches a signal:
170 is less than zero, the process must have alter permission on the
174 is greater than or equal to the absolute value of
176 the operation can proceed immediately:
177 the absolute value of
183 is specified for this operation, the system adds the absolute value of
185 to the semaphore adjustment
187 value for this semaphore.
188 If the absolute value of
201 (and none of the operations in
206 (the counter of threads waiting for this semaphore's value to increase)
207 is incremented by one and the thread sleeps until
208 one of the following occurs:
211 becomes greater than or equal to the absolute value of
213 the operation now proceeds, as described above.
215 The semaphore set is removed from the system:
222 The calling thread catches a signal:
232 On successful completion, the
234 value for each semaphore specified in the array pointed to by
236 is set to the caller's process ID.
241 is set to the current time.
244 behaves identically to
246 except that in those cases where the calling thread would sleep,
247 the duration of that sleep is limited by the amount of elapsed
248 time specified by the
250 structure whose address is passed in the
253 (This sleep interval will be rounded up to the system clock granularity,
254 and kernel scheduling delays mean that the interval
255 may overrun by a small amount.)
256 If the specified time limit has been reached,
262 (and none of the operations in
275 is interrupted by a signal, causing the call to fail with the error
286 On failure, they return \-1, and set
288 to indicate the error.
296 the maximum number of operations allowed per system
300 The calling process does not have the permissions required
301 to perform the specified semaphore operations,
302 and does not have the
304 capability in the user namespace that governs its IPC namespace.
307 An operation could not proceed immediately and either
311 or the time limit specified in
316 An address specified in either the
320 argument isn't accessible.
323 For some operation the value of
325 is less than 0 or greater than or equal to the number
326 of semaphores in the set.
329 The semaphore set was removed.
332 While blocked in this system call, the thread caught a signal; see
336 The semaphore set doesn't exist, or
338 is less than zero, or
340 has a nonpositive value.
345 of some operation specified
347 and the system does not have enough memory to allocate the undo
355 the implementation dependent maximum value for
359 first appeared in Linux 2.5.52,
360 and was subsequently backported into kernel 2.4.22.
363 first appeared in version 2.3.3.
365 POSIX.1-2001, POSIX.1-2008, SVr4.
366 .\" SVr4 documents additional error conditions EINVAL, EFBIG, ENOSPC.
370 structures of a process aren't inherited by the child produced by
372 but they are inherited across an
377 is never automatically restarted after being interrupted by a signal handler,
378 regardless of the setting of the
380 flag when establishing a signal handler.
382 A semaphore adjustment
384 value is a per-process, per-semaphore integer that is the negated sum
385 of all operations performed on a semaphore specifying the
388 Each process has a list of
390 values\(emone value for each semaphore on which it has operated using
392 When a process terminates, each of its per-semaphore
394 values is added to the corresponding semaphore,
395 thus undoing the effect of that process's operations on the semaphore
396 (but see BUGS below).
397 When a semaphore's value is directly set using the
405 values in all processes are cleared.
409 flag allows more than one process to share a
415 The \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, and \fIsemnct\fP values
416 for a semaphore can all be retrieved using appropriate
420 The following limits on semaphore set resources affect the
425 Maximum number of operations allowed for one
429 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
430 the default value for this limit was 32.
431 Since Linux 3.19, the default value is 500.
432 On Linux, this limit can be read and modified via the third field of
433 .IR /proc/sys/kernel/sem .
434 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
436 this limit should not be raised above 1000,
437 .\" See comment in Linux 3.19 source file include/uapi/linux/sem.h
438 because of the risk of that
440 fails due to kernel memory fragmentation when allocating memory to copy the
445 Maximum allowable value for
447 implementation dependent (32767).
449 The implementation has no intrinsic limits for
450 the adjust on exit maximum value
452 the system wide maximum number of undo structures
454 and the per-process maximum number of undo entries system parameters.
456 When a process terminates, its set of associated
458 structures is used to undo the effect of all of the
459 semaphore operations it performed with the
462 This raises a difficulty: if one (or more) of these semaphore adjustments
463 would result in an attempt to decrease a semaphore's value below zero,
464 what should an implementation do?
465 One possible approach would be to block until all the semaphore
466 adjustments could be performed.
467 This is however undesirable since it could force process termination to
468 block for arbitrarily long periods.
469 Another possibility is that such semaphore adjustments could be ignored
470 altogether (somewhat analogously to failing when
472 is specified for a semaphore operation).
473 Linux adopts a third approach: decreasing the semaphore value
474 as far as possible (i.e., to zero) and allowing process
475 termination to proceed immediately.
477 In kernels 2.6.x, x <= 10, there is a bug that in some circumstances
478 prevents a thread that is waiting for a semaphore value to become
479 zero from being woken up when the value does actually become zero.
480 This bug is fixed in kernel 2.6.11.
482 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
484 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
486 The following code segment uses
488 to atomically wait for the value of semaphore 0 to become zero,
489 and then increment the semaphore value by one.
493 struct sembuf sops[2];
496 /* Code to set \fIsemid\fP omitted */
498 sops[0].sem_num = 0; /* Operate on semaphore 0 */
499 sops[0].sem_op = 0; /* Wait for value to equal 0 */
502 sops[1].sem_num = 0; /* Operate on semaphore 0 */
503 sops[1].sem_op = 1; /* Increment value by one */
506 if (semop(semid, sops, 2) == \-1) {
513 A further example of the use of
522 .BR capabilities (7),
523 .BR sem_overview (7),