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, size_t nsops);
14
15       int semtimedop(int semid, struct sembuf *sops, size_t nsops,
16                      const 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;   /* PID of process that last
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       is a structure that specifies an operation to be performed on a  single
34       semaphore.   The  elements of this structure are of type struct sembuf,
35       containing the following 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
70semval becomes 0, at which time the value of semzcnt is decremented.
71
72       • The semaphore set is removed: semop() fails, with errno set to EIDRM.
73
74       • The  calling  thread catches a signal: the value of semzcnt is decre‐
75         mented and semop() fails, with errno set to EINTR.
76
77       If sem_op is less than zero, the process must have alter permission  on
78       the  semaphore set.  If semval is greater than or equal to the absolute
79       value of sem_op, the operation can proceed  immediately:  the  absolute
80       value  of  sem_op is subtracted from semval, and, if SEM_UNDO is speci‐
81       fied for this operation, the system adds the absolute value  of  sem_op
82       to  the semaphore adjustment (semadj) value for this semaphore.  If the
83       absolute value of sem_op is greater  than  semval,  and  IPC_NOWAIT  is
84       specified in sem_flg, semop() fails, with errno set to EAGAIN (and none
85       of the operations in  sops  is  performed).   Otherwise,  semncnt  (the
86       counter  of  threads waiting for this semaphore's value to increase) is
87       incremented by one and the thread sleeps until one of the following oc‐
88       curs:
89
90semval becomes greater than or equal to the absolute value of sem_op:
91         the operation now proceeds, as described above.
92
93       • The semaphore set is removed from the system: semop() fails, with er‐
94         rno set to EIDRM.
95
96       • The  calling  thread catches a signal: the value of semncnt is decre‐
97         mented and semop() fails, with errno set to EINTR.
98
99       On successful completion, the sempid value for each semaphore specified
100       in  the array pointed to by sops is set to the caller's process ID.  In
101       addition, the sem_otime is set to the current time.
102
103   semtimedop()
104       semtimedop() behaves identically to semop() except that in those  cases
105       where  the  calling  thread  would sleep, the duration of that sleep is
106       limited by the amount of elapsed time specified by the timespec  struc‐
107       ture  whose address is passed in the timeout argument.  (This sleep in‐
108       terval will be rounded up to the system clock granularity,  and  kernel
109       scheduling  delays  mean  that  the  interval  may  overrun  by a small
110       amount.)  If the specified time limit has  been  reached,  semtimedop()
111       fails  with  errno set to EAGAIN (and none of the operations in sops is
112       performed).  If the timeout argument is NULL, then semtimedop() behaves
113       exactly like semop().
114
115       Note  that if semtimedop() is interrupted by a signal, causing the call
116       to fail with the error EINTR, the contents  of  timeout  are  left  un‐
117       changed.
118

RETURN VALUE

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

ERRORS

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

VERSIONS

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

CONFORMING TO

164       POSIX.1-2001, POSIX.1-2008, SVr4.
165

NOTES

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

BUGS

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

EXAMPLES

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

SEE ALSO

262       clone(2),    semctl(2),   semget(2),   sigaction(2),   capabilities(7),
263       sem_overview(7), sysvipc(7), time(7)
264

COLOPHON

266       This page is part of release 5.10 of the Linux  man-pages  project.   A
267       description  of  the project, information about reporting bugs, and the
268       latest    version    of    this    page,    can     be     found     at
269       https://www.kernel.org/doc/man-pages/.
270
271
272
273Linux                             2020-04-11                          SEMOP(2)
Impressum