1SEMOP(2)                   Linux Programmer's Manual                  SEMOP(2)
2
3
4

NAME

6       semop, semtimedop - System V semaphore operations
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/ipc.h>
11       #include <sys/sem.h>
12
13       int semop(int semid, struct sembuf *sops, unsigned nsops);
14
15       int semtimedop(int semid, struct sembuf *sops, unsigned nsops,
16                      struct timespec *timeout);
17
18   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20       semtimedop(): _GNU_SOURCE
21

DESCRIPTION

23       Each semaphore in a System V semaphore set has the following associated
24       values:
25
26           unsigned short  semval;   /* semaphore value */
27           unsigned short  semzcnt;  /* # waiting for zero */
28           unsigned short  semncnt;  /* # waiting for increase */
29           pid_t           sempid;   /* ID of process that did last op */
30
31       semop() performs operations on selected semaphores in the set indicated
32       by  semid.   Each of the nsops elements in the array pointed to by sops
33       specifies an operation to be performed on a single semaphore.  The ele‐
34       ments  of this structure are of type struct sembuf, containing the fol‐
35       lowing members:
36
37           unsigned short sem_num;  /* semaphore number */
38           short          sem_op;   /* semaphore operation */
39           short          sem_flg;  /* operation flags */
40
41       Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO.  If an  opera‐
42       tion  specifies  SEM_UNDO,  it  will  be  automatically undone when the
43       process terminates.
44
45       The set of operations contained in sops is performed  in  array  order,
46       and  atomically, that is, the operations are performed either as a com‐
47       plete unit, or not at all.  The behavior of the system call if not  all
48       operations  can be performed immediately depends on the presence of the
49       IPC_NOWAIT flag in the individual sem_flg fields, as noted below.
50
51       Each operation is performed on the sem_num-th semaphore  of  the  sema‐
52       phore  set,  where the first semaphore of the set is numbered 0.  There
53       are three types of operation, distinguished by the value of sem_op.
54
55       If sem_op is a positive integer, the operation adds this value  to  the
56       semaphore  value  (semval).   Furthermore, if SEM_UNDO is specified for
57       this operation, the system subtracts the value sem_op  from  the  sema‐
58       phore adjustment (semadj) value for this semaphore.  This operation can
59       always proceed—it never forces a thread to wait.  The  calling  process
60       must have alter permission on the semaphore set.
61
62       If  sem_op  is zero, the process must have read permission on the sema‐
63       phore set.  This is a "wait-for-zero" operation: if semval is zero, the
64       operation  can immediately proceed.  Otherwise, if IPC_NOWAIT is speci‐
65       fied in sem_flg, semop() fails with errno set to EAGAIN  (and  none  of
66       the  operations in sops is performed).  Otherwise semzcnt (the count of
67       threads waiting until this semaphore's value becomes  zero)  is  incre‐
68       mented by one and the thread sleeps until one of the following occurs:
69
70       ·  semval becomes 0, at which time the value of semzcnt is decremented.
71
72       ·  The  semaphore  set  is  removed:  semop()  fails, with errno set to
73          EIDRM.
74
75       ·  The calling thread catches a signal: the value of semzcnt is  decre‐
76          mented and semop() fails, with errno set to EINTR.
77
78       ·  The  time limit specified by timeout in a semtimedop() call expires:
79          semop() fails, with errno set to EAGAIN.
80
81       If sem_op is less than zero, the process must have alter permission  on
82       the  semaphore set.  If semval is greater than or equal to the absolute
83       value of sem_op, the operation can proceed  immediately:  the  absolute
84       value  of  sem_op is subtracted from semval, and, if SEM_UNDO is speci‐
85       fied for this operation, the system adds the absolute value  of  sem_op
86       to  the semaphore adjustment (semadj) value for this semaphore.  If the
87       absolute value of sem_op is greater  than  semval,  and  IPC_NOWAIT  is
88       specified in sem_flg, semop() fails, with errno set to EAGAIN (and none
89       of the operations  in  sops  is  performed).   Otherwise  semncnt  (the
90       counter  of  threads waiting for this semaphore's value to increase) is
91       incremented by one and the thread sleeps until  one  of  the  following
92       occurs:
93
94       ·  semval  becomes  greater  than  or  equal  to  the absolute value of
95          sem_op: the operation now proceeds, as described above.
96
97       ·  The semaphore set is removed from the system:  semop()  fails,  with
98          errno set to EIDRM.
99
100       ·  The  calling thread catches a signal: the value of semncnt is decre‐
101          mented and semop() fails, with errno set to EINTR.
102
103       ·  The time limit specified by timeout in a semtimedop() call  expires:
104          the system call fails, with errno set to EAGAIN.
105
106       On successful completion, the sempid value for each semaphore specified
107       in the array pointed to by sops is set to the caller's process ID.   In
108       addition, the sem_otime is set to the current time.
109
110       semtimedop()  behaves identically to semop() except that in those cases
111       where the calling thread would sleep, the duration  of  that  sleep  is
112       limited  by the amount of elapsed time specified by the timespec struc‐
113       ture whose address is passed in  the  timeout  argument.   (This  sleep
114       interval will be rounded up to the system clock granularity, and kernel
115       scheduling delays mean  that  the  interval  may  overrun  by  a  small
116       amount.)   If  the  specified time limit has been reached, semtimedop()
117       fails with errno set to EAGAIN (and none of the operations in  sops  is
118       performed).  If the timeout argument is NULL, then semtimedop() behaves
119       exactly like semop().
120

RETURN VALUE

122       If successful semop() and semtimedop() return 0; otherwise they  return
123       -1 with errno indicating the error.
124

ERRORS

126       On failure, errno is set to one of the following:
127
128       E2BIG  The argument nsops is greater than SEMOPM, the maximum number of
129              operations allowed per system call.
130
131       EACCES The calling process does not have the  permissions  required  to
132              perform  the  specified  semaphore operations, and does not have
133              the CAP_IPC_OWNER capability.
134
135       EAGAIN An operation could not proceed immediately and either IPC_NOWAIT
136              was  specified in sem_flg or the time limit specified in timeout
137              expired.
138
139       EFAULT An address specified in either the sops or the timeout  argument
140              isn't accessible.
141
142       EFBIG  For  some  operation  the  value  of  sem_num  is less than 0 or
143              greater than or equal to the number of semaphores in the set.
144
145       EIDRM  The semaphore set was removed.
146
147       EINTR  While blocked in this system call, the thread caught  a  signal;
148              see signal(7).
149
150       EINVAL The  semaphore set doesn't exist, or semid is less than zero, or
151              nsops has a nonpositive value.
152
153       ENOMEM The sem_flg of some operation specified SEM_UNDO and the  system
154              does not have enough memory to allocate the undo structure.
155
156       ERANGE For  some  operation  sem_op+semval  is greater than SEMVMX, the
157              implementation dependent maximum value for semval.
158

VERSIONS

160       semtimedop() first appeared in Linux 2.5.52, and was subsequently back‐
161       ported  into  kernel  2.4.22.   Glibc  support  for  semtimedop() first
162       appeared in version 2.3.3.
163

CONFORMING TO

165       SVr4, POSIX.1-2001.
166

NOTES

168       The inclusion of <sys/types.h> and <sys/ipc.h> isn't required on  Linux
169       or by any version of POSIX.  However, some old implementations required
170       the inclusion of these header files, and the SVID also documented their
171       inclusion.   Applications  intended  to be portable to such old systems
172       may need to include these header files.
173
174       The sem_undo structures of a process aren't inherited by the child pro‐
175       duced  by  fork(2),  but  they are inherited across an execve(2) system
176       call.
177
178       semop() is never automatically restarted after being interrupted  by  a
179       signal  handler,  regardless of the setting of the SA_RESTART flag when
180       establishing a signal handler.
181
182       A semaphore adjustment (semadj) value is a  per-process,  per-semaphore
183       integer  that is the negated sum of all operations performed on a sema‐
184       phore specifying the SEM_UNDO flag.  Each process has a list of  semadj
185       values—one  value  for  each  semaphore  on which it has operated using
186       SEM_UNDO.  When a process terminates, each of its per-semaphore  semadj
187       values is added to the corresponding semaphore, thus undoing the effect
188       of that process's operations on the semaphore  (but  see  BUGS  below).
189       When  a  semaphore's  value  is directly set using the SETVAL or SETALL
190       request to semctl(2), the corresponding semadj values in all  processes
191       are cleared.
192
193       The  semval, sempid, semzcnt, and semnct values for a semaphore can all
194       be retrieved using appropriate semctl(2) calls.
195
196       The following limits on semaphore  set  resources  affect  the  semop()
197       call:
198
199       SEMOPM Maximum  number  of operations allowed for one semop() call (32)
200              (on Linux, this limit can be read and  modified  via  the  third
201              field of /proc/sys/kernel/sem).
202
203       SEMVMX Maximum  allowable  value  for  semval: implementation dependent
204              (32767).
205
206       The implementation has no intrinsic limits for the adjust on exit maxi‐
207       mum  value  (SEMAEM), the system wide maximum number of undo structures
208       (SEMMNU) and the per-process maximum  number  of  undo  entries  system
209       parameters.
210

BUGS

212       When  a  process terminates, its set of associated semadj structures is
213       used to undo the effect of all of the semaphore operations it performed
214       with  the SEM_UNDO flag.  This raises a difficulty: if one (or more) of
215       these semaphore adjustments would result in an attempt  to  decrease  a
216       semaphore's  value  below  zero, what should an implementation do?  One
217       possible approach would be to block until all the semaphore adjustments
218       could  be  performed.  This is however undesirable since it could force
219       process termination to block for  arbitrarily  long  periods.   Another
220       possibility  is  that such semaphore adjustments could be ignored alto‐
221       gether (somewhat analogously to failing when  IPC_NOWAIT  is  specified
222       for  a semaphore operation).  Linux adopts a third approach: decreasing
223       the semaphore value as far as possible (i.e.,  to  zero)  and  allowing
224       process termination to proceed immediately.
225
226       In  kernels  2.6.x,  x <= 10, there is a bug that in some circumstances
227       prevents a thread that is waiting for a semaphore value to become  zero
228       from being woken up when the value does actually become zero.  This bug
229       is fixed in kernel 2.6.11.
230

EXAMPLE

232       The following code segment uses semop()  to  atomically  wait  for  the
233       value  of  semaphore 0 to become zero, and then increment the semaphore
234       value by one.
235
236           struct sembuf sops[2];
237           int semid;
238
239           /* Code to set semid omitted */
240
241           sops[0].sem_num = 0;        /* Operate on semaphore 0 */
242           sops[0].sem_op = 0;         /* Wait for value to equal 0 */
243           sops[0].sem_flg = 0;
244
245           sops[1].sem_num = 0;        /* Operate on semaphore 0 */
246           sops[1].sem_op = 1;         /* Increment value by one */
247           sops[1].sem_flg = 0;
248
249           if (semop(semid, sops, 2) == -1) {
250               perror("semop");
251               exit(EXIT_FAILURE);
252           }
253

SEE ALSO

255       clone(2),   semctl(2),   semget(2),   sigaction(2),    capabilities(7),
256       sem_overview(7), svipc(7), time(7)
257

COLOPHON

259       This  page  is  part of release 3.53 of the Linux man-pages project.  A
260       description of the project, and information about reporting  bugs,  can
261       be found at http://www.kernel.org/doc/man-pages/.
262
263
264
265Linux                             2013-04-17                          SEMOP(2)
Impressum