1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Modified 1996-10-22, Eric S. Raymond <esr@thyrsus.com>
24 .\" Modified 2002-01-08, Michael Kerrisk <mtk.manpages@gmail.com>
25 .\" Modified 2003-04-28, Ernie Petrides <petrides@redhat.com>
26 .\" Modified 2004-05-27, Michael Kerrisk <mtk.manpages@gmail.com>
27 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
28 .\" Language and formatting clean-ups
29 .\" Added notes on /proc files
30 .\" 2005-04-08, mtk, Noted kernel version numbers for semtimedop()
31 .\" 2007-07-09, mtk, Added an EXAMPLE code segment.
33 .TH SEMOP 2 2012-08-27 "Linux" "Linux Programmer's Manual"
35 semop, semtimedop \- semaphore operations
38 .B #include <sys/types.h>
39 .B #include <sys/ipc.h>
40 .B #include <sys/sem.h>
42 .BI "int semop(int " semid ", struct sembuf *" sops ", unsigned " nsops );
44 .BI "int semtimedop(int " semid ", struct sembuf *" sops ", unsigned " nsops ,
45 .BI " struct timespec *" timeout );
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
56 Each semaphore in a semaphore set has the following associated values:
60 unsigned short semval; /* semaphore value */
61 unsigned short semzcnt; /* # waiting for zero */
62 unsigned short semncnt; /* # waiting for increase */
63 pid_t sempid; /* process that did last op */
68 performs operations on selected semaphores in the set indicated by
72 elements in the array pointed to by
74 specifies an operation to be performed on a single semaphore.
75 The elements of this structure are of type
77 containing the following members:
81 unsigned short sem_num; /* semaphore number */
82 short sem_op; /* semaphore operation */
83 short sem_flg; /* operation flags */
93 If an operation specifies
95 it will be automatically undone when the process terminates.
97 The set of operations contained in
103 that is, the operations are performed either as a complete unit,
105 The behavior of the system call if not all operations can be
106 performed immediately depends on the presence of the
108 flag in the individual
110 fields, as noted below.
112 Each operation is performed on the
114 semaphore of the semaphore set, where the first semaphore of the set
116 There are three types of operation, distinguished by the value of
121 is a positive integer, the operation adds this value to
126 is specified for this operation, the system updates the process undo count
129 This operation can always proceed\(emit never forces a process to wait.
130 The calling process must have alter permission on the semaphore set.
134 is zero, the process must have read permission on the semaphore
136 This is a "wait-for-zero" operation: if
138 is zero, the operation can immediately proceed.
148 (and none of the operations in
153 (the count of processes waiting until this semaphore's value becomes zero)
154 is incremented by one and the process sleeps until
155 one of the following occurs:
158 becomes 0, at which time the value of
170 The calling process catches a signal:
180 The time limit specified by
193 is less than zero, the process must have alter permission on the
197 is greater than or equal to the absolute value of
199 the operation can proceed immediately:
200 the absolute value of
206 is specified for this operation, the system updates the process undo count
209 If the absolute value of
222 (and none of the operations in
227 (the counter of processes waiting for this semaphore's value to increase)
228 is incremented by one and the process sleeps until
229 one of the following occurs:
232 becomes greater than or equal to the absolute value of
234 at which time the value of
236 is decremented, the absolute value of
242 is specified for this operation, the system updates the process undo count
246 The semaphore set is removed from the system:
253 The calling process catches a signal:
263 The time limit specified by
267 call expires: the system call fails, with
272 On successful completion, the
274 value for each semaphore specified in the array pointed to by
276 is set to the process ID of the calling process.
281 is set to the current time.
284 behaves identically to
286 except that in those cases were the calling process would sleep,
287 the duration of that sleep is limited by the amount of elapsed
288 time specified by the
290 structure whose address is passed in the
293 (This sleep interval will be rounded up to the system clock granularity,
294 and kernel scheduling delays mean that the interval
295 may overrun by a small amount.)
296 If the specified time limit has been reached,
302 (and none of the operations in
318 otherwise they return \-1
321 indicating the error.
325 is set to one of the following:
332 the maximum number of operations allowed per system
336 The calling process does not have the permissions required
337 to perform the specified semaphore operations,
338 and does not have the
343 An operation could not proceed immediately and either
347 or the time limit specified in
352 An address specified in either the
356 argument isn't accessible.
359 For some operation the value of
361 is less than 0 or greater than or equal to the number
362 of semaphores in the set.
365 The semaphore set was removed.
368 While blocked in this system call, the process caught a signal; see
372 The semaphore set doesn't exist, or
374 is less than zero, or
376 has a nonpositive value.
381 of some operation specified
383 and the system does not have enough memory to allocate the undo
391 the implementation dependent maximum value for
395 first appeared in Linux 2.5.52,
396 and was subsequently backported into kernel 2.4.22.
399 first appeared in version 2.3.3.
402 .\" SVr4 documents additional error conditions EINVAL, EFBIG, ENOSPC.
408 isn't required on Linux or by any version of POSIX.
410 some old implementations required the inclusion of these header files,
411 and the SVID also documented their inclusion.
412 Applications intended to be portable to such old systems may need
413 to include these header files.
414 .\" Like Linux, the FreeBSD man pages still document
415 .\" the inclusion of these header files.
419 structures of a process aren't inherited by the child produced by
421 but they are inherited across an
426 is never automatically restarted after being interrupted by a signal handler,
427 regardless of the setting of the
429 flag when establishing a signal handler.
432 is a per-process integer which is simply the (negative) count
433 of all semaphore operations performed specifying the
436 When a semaphore's value is directly set using the
444 values in all processes are cleared.
446 The \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, and \fIsemnct\fP values
447 for a semaphore can all be retrieved using appropriate
451 The following limits on semaphore set resources affect the
456 Maximum number of operations allowed for one
459 (on Linux, this limit can be read and modified via the third field of
460 .IR /proc/sys/kernel/sem ).
461 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
464 Maximum allowable value for
466 implementation dependent (32767).
468 The implementation has no intrinsic limits for
469 the adjust on exit maximum value
471 the system wide maximum number of undo structures
473 and the per-process maximum number of undo entries system parameters.
475 When a process terminates, its set of associated
477 structures is used to undo the effect of all of the
478 semaphore operations it performed with the
481 This raises a difficulty: if one (or more) of these semaphore adjustments
482 would result in an attempt to decrease a semaphore's value below zero,
483 what should an implementation do?
484 One possible approach would be to block until all the semaphore
485 adjustments could be performed.
486 This is however undesirable since it could force process termination to
487 block for arbitrarily long periods.
488 Another possibility is that such semaphore adjustments could be ignored
489 altogether (somewhat analogously to failing when
491 is specified for a semaphore operation).
492 Linux adopts a third approach: decreasing the semaphore value
493 as far as possible (i.e., to zero) and allowing process
494 termination to proceed immediately.
496 In kernels 2.6.x, x <= 10, there is a bug that in some circumstances
497 prevents a process that is waiting for a semaphore value to become
498 zero from being woken up when the value does actually become zero.
499 This bug is fixed in kernel 2.6.11.
501 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
503 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
505 The following code segment uses
507 to atomically wait for the value of semaphore 0 to become zero,
508 and then increment the semaphore value by one.
511 struct sembuf sops[2];
514 /* Code to set \fIsemid\fP omitted */
516 sops[0].sem_num = 0; /* Operate on semaphore 0 */
517 sops[0].sem_op = 0; /* Wait for value to equal 0 */
520 sops[1].sem_num = 0; /* Operate on semaphore 0 */
521 sops[1].sem_op = 1; /* Increment value by one */
524 if (semop(semid, sops, 2) == \-1) {
534 .BR capabilities (7),
535 .BR sem_overview (7),