1SEMOP(2) Linux Programmer's Manual SEMOP(2)
2
3
4
6 semop, semtimedop - System V semaphore operations
7
9 #include <sys/sem.h>
10
11 int semop(int semid, struct sembuf *sops, size_t nsops);
12 int semtimedop(int semid, struct sembuf *sops, size_t nsops,
13 const struct timespec *timeout);
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 semtimedop():
18 _GNU_SOURCE
19
21 Each semaphore in a System V semaphore set has the following associated
22 values:
23
24 unsigned short semval; /* semaphore value */
25 unsigned short semzcnt; /* # waiting for zero */
26 unsigned short semncnt; /* # waiting for increase */
27 pid_t sempid; /* PID of process that last
28
29 semop() performs operations on selected semaphores in the set indicated
30 by semid. Each of the nsops elements in the array pointed to by sops
31 is a structure that specifies an operation to be performed on a single
32 semaphore. The elements of this structure are of type struct sembuf,
33 containing the following members:
34
35 unsigned short sem_num; /* semaphore number */
36 short sem_op; /* semaphore operation */
37 short sem_flg; /* operation flags */
38
39 Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO. If an opera‐
40 tion specifies SEM_UNDO, it will be automatically undone when the
41 process terminates.
42
43 The set of operations contained in sops is performed in array order,
44 and atomically, that is, the operations are performed either as a com‐
45 plete unit, or not at all. The behavior of the system call if not all
46 operations can be performed immediately depends on the presence of the
47 IPC_NOWAIT flag in the individual sem_flg fields, as noted below.
48
49 Each operation is performed on the sem_num-th semaphore of the sema‐
50 phore set, where the first semaphore of the set is numbered 0. There
51 are three types of operation, distinguished by the value of sem_op.
52
53 If sem_op is a positive integer, the operation adds this value to the
54 semaphore value (semval). Furthermore, if SEM_UNDO is specified for
55 this operation, the system subtracts the value sem_op from the sema‐
56 phore adjustment (semadj) value for this semaphore. This operation can
57 always proceed—it never forces a thread to wait. The calling process
58 must have alter permission on the semaphore set.
59
60 If sem_op is zero, the process must have read permission on the sema‐
61 phore set. This is a "wait-for-zero" operation: if semval is zero, the
62 operation can immediately proceed. Otherwise, if IPC_NOWAIT is speci‐
63 fied in sem_flg, semop() fails with errno set to EAGAIN (and none of
64 the operations in sops is performed). Otherwise, semzcnt (the count of
65 threads waiting until this semaphore's value becomes zero) is incre‐
66 mented by one and the thread sleeps until one of the following occurs:
67
68 • semval becomes 0, at which time the value of semzcnt is decremented.
69
70 • The semaphore set is removed: semop() fails, with errno set to EIDRM.
71
72 • The calling thread catches a signal: the value of semzcnt is decre‐
73 mented and semop() fails, with errno set to EINTR.
74
75 If sem_op is less than zero, the process must have alter permission on
76 the semaphore set. If semval is greater than or equal to the absolute
77 value of sem_op, the operation can proceed immediately: the absolute
78 value of sem_op is subtracted from semval, and, if SEM_UNDO is speci‐
79 fied for this operation, the system adds the absolute value of sem_op
80 to the semaphore adjustment (semadj) value for this semaphore. If the
81 absolute value of sem_op is greater than semval, and IPC_NOWAIT is
82 specified in sem_flg, semop() fails, with errno set to EAGAIN (and none
83 of the operations in sops is performed). Otherwise, semncnt (the
84 counter of threads waiting for this semaphore's value to increase) is
85 incremented by one and the thread sleeps until one of the following oc‐
86 curs:
87
88 • semval becomes greater than or equal to the absolute value of sem_op:
89 the operation now proceeds, as described above.
90
91 • The semaphore set is removed from the system: semop() fails, with er‐
92 rno set to EIDRM.
93
94 • The calling thread catches a signal: the value of semncnt is decre‐
95 mented and semop() fails, with errno set to EINTR.
96
97 On successful completion, the sempid value for each semaphore specified
98 in the array pointed to by sops is set to the caller's process ID. In
99 addition, the sem_otime is set to the current time.
100
101 semtimedop()
102 semtimedop() behaves identically to semop() except that in those cases
103 where the calling thread would sleep, the duration of that sleep is
104 limited by the amount of elapsed time specified by the timespec struc‐
105 ture whose address is passed in the timeout argument. (This sleep in‐
106 terval will be rounded up to the system clock granularity, and kernel
107 scheduling delays mean that the interval may overrun by a small
108 amount.) If the specified time limit has been reached, semtimedop()
109 fails with errno set to EAGAIN (and none of the operations in sops is
110 performed). If the timeout argument is NULL, then semtimedop() behaves
111 exactly like semop().
112
113 Note that if semtimedop() is interrupted by a signal, causing the call
114 to fail with the error EINTR, the contents of timeout are left un‐
115 changed.
116
118 On success, semop() and semtimedop() return 0. On failure, they return
119 -1, and set errno to indicate the error.
120
122 E2BIG The argument nsops is greater than SEMOPM, the maximum number of
123 operations allowed per system call.
124
125 EACCES The calling process does not have the permissions required to
126 perform the specified semaphore operations, and does not have
127 the CAP_IPC_OWNER capability in the user namespace that governs
128 its IPC namespace.
129
130 EAGAIN An operation could not proceed immediately and either IPC_NOWAIT
131 was specified in sem_flg or the time limit specified in timeout
132 expired.
133
134 EFAULT An address specified in either the sops or the timeout argument
135 isn't accessible.
136
137 EFBIG For some operation the value of sem_num is less than 0 or
138 greater than or equal to the number of semaphores in the set.
139
140 EIDRM The semaphore set was removed.
141
142 EINTR While blocked in this system call, the thread caught a signal;
143 see signal(7).
144
145 EINVAL The semaphore set doesn't exist, or semid is less than zero, or
146 nsops has a nonpositive value.
147
148 ENOMEM The sem_flg of some operation specified SEM_UNDO and the system
149 does not have enough memory to allocate the undo structure.
150
151 ERANGE For some operation sem_op+semval is greater than SEMVMX, the im‐
152 plementation dependent maximum value for semval.
153
155 semtimedop() first appeared in Linux 2.5.52, and was subsequently back‐
156 ported into kernel 2.4.22. Glibc support for semtimedop() first ap‐
157 peared in version 2.3.3.
158
160 POSIX.1-2001, POSIX.1-2008, SVr4.
161
163 The sem_undo structures of a process aren't inherited by the child pro‐
164 duced by fork(2), but they are inherited across an execve(2) system
165 call.
166
167 semop() is never automatically restarted after being interrupted by a
168 signal handler, regardless of the setting of the SA_RESTART flag when
169 establishing a signal handler.
170
171 A semaphore adjustment (semadj) value is a per-process, per-semaphore
172 integer that is the negated sum of all operations performed on a sema‐
173 phore specifying the SEM_UNDO flag. Each process has a list of semadj
174 values—one value for each semaphore on which it has operated using
175 SEM_UNDO. When a process terminates, each of its per-semaphore semadj
176 values is added to the corresponding semaphore, thus undoing the effect
177 of that process's operations on the semaphore (but see BUGS below).
178 When a semaphore's value is directly set using the SETVAL or SETALL re‐
179 quest to semctl(2), the corresponding semadj values in all processes
180 are cleared. The clone(2) CLONE_SYSVSEM flag allows more than one
181 process to share a semadj list; see clone(2) for details.
182
183 The semval, sempid, semzcnt, and semnct values for a semaphore can all
184 be retrieved using appropriate semctl(2) calls.
185
186 Semaphore limits
187 The following limits on semaphore set resources affect the semop()
188 call:
189
190 SEMOPM Maximum number of operations allowed for one semop() call. Be‐
191 fore Linux 3.19, the default value for this limit was 32. Since
192 Linux 3.19, the default value is 500. On Linux, this limit can
193 be read and modified via the third field of /proc/sys/ker‐
194 nel/sem. Note: this limit should not be raised above 1000, be‐
195 cause of the risk of that semop() fails due to kernel memory
196 fragmentation when allocating memory to copy the sops array.
197
198 SEMVMX Maximum allowable value for semval: implementation dependent
199 (32767).
200
201 The implementation has no intrinsic limits for the adjust on exit maxi‐
202 mum value (SEMAEM), the system wide maximum number of undo structures
203 (SEMMNU) and the per-process maximum number of undo entries system pa‐
204 rameters.
205
207 When a process terminates, its set of associated semadj structures is
208 used to undo the effect of all of the semaphore operations it performed
209 with the SEM_UNDO flag. This raises a difficulty: if one (or more) of
210 these semaphore adjustments would result in an attempt to decrease a
211 semaphore's value below zero, what should an implementation do? One
212 possible approach would be to block until all the semaphore adjustments
213 could be performed. This is however undesirable since it could force
214 process termination to block for arbitrarily long periods. Another
215 possibility is that such semaphore adjustments could be ignored alto‐
216 gether (somewhat analogously to failing when IPC_NOWAIT is specified
217 for a semaphore operation). Linux adopts a third approach: decreasing
218 the semaphore value as far as possible (i.e., to zero) and allowing
219 process termination to proceed immediately.
220
221 In kernels 2.6.x, x <= 10, there is a bug that in some circumstances
222 prevents a thread that is waiting for a semaphore value to become zero
223 from being woken up when the value does actually become zero. This bug
224 is fixed in kernel 2.6.11.
225
227 The following code segment uses semop() to atomically wait for the
228 value of semaphore 0 to become zero, and then increment the semaphore
229 value by one.
230
231 struct sembuf sops[2];
232 int semid;
233
234 /* Code to set semid omitted */
235
236 sops[0].sem_num = 0; /* Operate on semaphore 0 */
237 sops[0].sem_op = 0; /* Wait for value to equal 0 */
238 sops[0].sem_flg = 0;
239
240 sops[1].sem_num = 0; /* Operate on semaphore 0 */
241 sops[1].sem_op = 1; /* Increment value by one */
242 sops[1].sem_flg = 0;
243
244 if (semop(semid, sops, 2) == -1) {
245 perror("semop");
246 exit(EXIT_FAILURE);
247 }
248
249 A further example of the use of semop() can be found in shmop(2).
250
252 clone(2), semctl(2), semget(2), sigaction(2), capabilities(7),
253 sem_overview(7), sysvipc(7), time(7)
254
256 This page is part of release 5.13 of the Linux man-pages project. A
257 description of the project, information about reporting bugs, and the
258 latest version of this page, can be found at
259 https://www.kernel.org/doc/man-pages/.
260
261
262
263Linux 2021-03-22 SEMOP(2)