1semop(2)                      System Calls Manual                     semop(2)
2
3
4

NAME

6       semop, semtimedop - System V semaphore operations
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

123       On success, semop() and semtimedop() return 0.  On failure, they return
124       -1, and set errno to indicate the error.
125

ERRORS

127       E2BIG  The argument nsops is greater than SEMOPM, the maximum number of
128              operations allowed per system call.
129
130       EACCES The calling process does not have the  permissions  required  to
131              perform  the  specified  semaphore operations, and does not have
132              the CAP_IPC_OWNER capability in the user namespace that  governs
133              its IPC namespace.
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 im‐
157              plementation dependent maximum value for semval.
158

STANDARDS

160       POSIX.1-2008.
161

VERSIONS

163       Linux   2.5.52   (backported   into   Linux   2.4.22),   glibc   2.3.3.
164       POSIX.1-2001, SVr4.
165

NOTES

167       The sem_undo structures of a process aren't inherited by the child pro‐
168       duced  by  fork(2),  but  they are inherited across an execve(2) system
169       call.
170
171       semop() is never automatically restarted after being interrupted  by  a
172       signal  handler,  regardless of the setting of the SA_RESTART flag when
173       establishing a signal handler.
174
175       A semaphore adjustment (semadj) value is a  per-process,  per-semaphore
176       integer  that is the negated sum of all operations performed on a sema‐
177       phore specifying the SEM_UNDO flag.  Each process has a list of  semadj
178       values—one  value  for  each  semaphore  on which it has operated using
179       SEM_UNDO.  When a process terminates, each of its per-semaphore  semadj
180       values is added to the corresponding semaphore, thus undoing the effect
181       of that process's operations on the semaphore  (but  see  BUGS  below).
182       When a semaphore's value is directly set using the SETVAL or SETALL re‐
183       quest to semctl(2), the corresponding semadj values  in  all  processes
184       are  cleared.   The  clone(2)  CLONE_SYSVSEM  flag allows more than one
185       process to share a semadj list; see clone(2) for details.
186
187       The semval, sempid, semzcnt, and semnct values for a semaphore can  all
188       be retrieved using appropriate semctl(2) calls.
189
190   Semaphore limits
191       The  following  limits  on  semaphore  set resources affect the semop()
192       call:
193
194       SEMOPM Maximum number of operations allowed for one semop() call.   Be‐
195              fore Linux 3.19, the default value for this limit was 32.  Since
196              Linux 3.19, the default value is 500.  On Linux, this limit  can
197              be  read  and  modified  via  the  third field of /proc/sys/ker‐
198              nel/sem.  Note: this limit should not be raised above 1000,  be‐
199              cause  of  the  risk  of that semop() fails due to kernel memory
200              fragmentation when allocating memory to copy the sops array.
201
202       SEMVMX Maximum allowable value  for  semval:  implementation  dependent
203              (32767).
204
205       The implementation has no intrinsic limits for the adjust on exit maxi‐
206       mum value (SEMAEM), the system wide maximum number of  undo  structures
207       (SEMMNU)  and the per-process maximum number of undo entries system pa‐
208       rameters.
209

BUGS

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

EXAMPLES

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

SEE ALSO

256       clone(2),    semctl(2),   semget(2),   sigaction(2),   capabilities(7),
257       sem_overview(7), sysvipc(7), time(7)
258
259
260
261Linux man-pages 6.04              2023-03-30                          semop(2)
Impressum