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 2008-10-04 "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 \(em it 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 If the specified time limit has been reached,
299 (and none of the operations in
315 otherwise they return \-1
318 indicating the error.
322 is set to one of the following:
329 the maximum number of operations allowed per system
333 The calling process does not have the permissions required
334 to perform the specified semaphore operations,
335 and does not have the
340 An operation could not proceed immediately and either
344 or the time limit specified in
349 An address specified in either the
353 argument isn't accessible.
356 For some operation the value of
358 is less than 0 or greater than or equal to the number
359 of semaphores in the set.
362 The semaphore set was removed.
365 While blocked in this system call, the process caught a signal; see
369 The semaphore set doesn't exist, or
371 is less than zero, or
373 has a nonpositive value.
378 of some operation specified
380 and the system does not have enough memory to allocate the undo
388 the implementation dependent maximum value for
392 first appeared in Linux 2.5.52,
393 and was subsequently backported into kernel 2.4.22.
396 first appeared in version 2.3.3.
399 .\" SVr4 documents additional error conditions EINVAL, EFBIG, ENOSPC.
403 structures of a process aren't inherited by the child produced by
405 but they are inherited across an
410 is never automatically restarted after being interrupted by a signal handler,
411 regardless of the setting of the
413 flag when establishing a signal handler.
416 is a per-process integer which is simply the (negative) count
417 of all semaphore operations performed specifying the
420 When a semaphore's value is directly set using the
428 values in all processes are cleared.
430 The \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, and \fIsemnct\fP values
431 for a semaphore can all be retrieved using appropriate
435 The following limits on semaphore set resources affect the
440 Maximum number of operations allowed for one
443 (on Linux, this limit can be read and modified via the third field of
444 .IR /proc/sys/kernel/sem ).
445 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
448 Maximum allowable value for
450 implementation dependent (32767).
452 The implementation has no intrinsic limits for
453 the adjust on exit maximum value
455 the system wide maximum number of undo structures
457 and the per-process maximum number of undo entries system parameters.
459 When a process terminates, its set of associated
461 structures is used to undo the effect of all of the
462 semaphore operations it performed with the
465 This raises a difficulty: if one (or more) of these semaphore adjustments
466 would result in an attempt to decrease a semaphore's value below zero,
467 what should an implementation do?
468 One possible approach would be to block until all the semaphore
469 adjustments could be performed.
470 This is however undesirable since it could force process termination to
471 block for arbitrarily long periods.
472 Another possibility is that such semaphore adjustments could be ignored
473 altogether (somewhat analogously to failing when
475 is specified for a semaphore operation).
476 Linux adopts a third approach: decreasing the semaphore value
477 as far as possible (i.e., to zero) and allowing process
478 termination to proceed immediately.
480 In kernels 2.6.x, x <= 10, there is a bug that in some circumstances
481 prevents a process that is waiting for a semaphore value to become
482 zero from being woken up when the value does actually become zero.
483 This bug is fixed in kernel 2.6.11.
485 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
487 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
489 The following code segment uses
491 to atomically wait for the value of semaphore 0 to become zero,
492 and then increment the semaphore value by one.
495 struct sembuf sops[2];
498 /* Code to set \fIsemid\fP omitted */
500 sops[0].sem_num = 0; /* Operate on semaphore 0 */
501 sops[0].sem_op = 0; /* Wait for value to equal 0 */
504 sops[1].sem_num = 0; /* Operate on semaphore 0 */
505 sops[1].sem_op = 1; /* Increment value by one */
508 if (semop(semid, sops, 2) == \-1) {
517 .BR capabilities (7),
518 .BR sem_overview (7),