]> git.ipfire.org Git - thirdparty/man-pages.git/blob
fe2906be4
[thirdparty/man-pages.git] /
1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .\" Modified 1996-10-22, Eric S. Raymond <esr@thyrsus.com>
6 .\" Modified 2002-01-08, Michael Kerrisk <mtk.manpages@gmail.com>
7 .\" Modified 2003-04-28, Ernie Petrides <petrides@redhat.com>
8 .\" Modified 2004-05-27, Michael Kerrisk <mtk.manpages@gmail.com>
9 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
10 .\" Language and formatting clean-ups
11 .\" Added notes on /proc files
12 .\" 2005-04-08, mtk, Noted kernel version numbers for semtimedop()
13 .\" 2007-07-09, mtk, Added an EXAMPLE code segment.
14 .\"
15 .TH semop 2 (date) "Linux man-pages (unreleased)"
16 .SH NAME
17 semop, semtimedop \- System V semaphore operations
18 .SH LIBRARY
19 Standard C library
20 .RI ( libc ", " \-lc )
21 .SH SYNOPSIS
22 .nf
23 .B #include <sys/sem.h>
24 .PP
25 .BI "int semop(int " semid ", struct sembuf *" sops ", size_t " nsops );
26 .BI "int semtimedop(int " semid ", struct sembuf *" sops ", size_t " nsops ,
27 .BI " const struct timespec *_Nullable " timeout );
28 .fi
29 .PP
30 .RS -4
31 Feature Test Macro Requirements for glibc (see
32 .BR feature_test_macros (7)):
33 .RE
34 .PP
35 .BR semtimedop ():
36 .nf
37 _GNU_SOURCE
38 .fi
39 .SH DESCRIPTION
40 Each semaphore in a System\ V semaphore set
41 has the following associated values:
42 .PP
43 .in +4n
44 .EX
45 unsigned short semval; /* semaphore value */
46 unsigned short semzcnt; /* # waiting for zero */
47 unsigned short semncnt; /* # waiting for increase */
48 pid_t sempid; /* PID of process that last
49 modified the semaphore value */
50 .EE
51 .in
52 .PP
53 .BR semop ()
54 performs operations on selected semaphores in the set indicated by
55 .IR semid .
56 Each of the
57 .I nsops
58 elements in the array pointed to by
59 .I sops
60 is a structure that
61 specifies an operation to be performed on a single semaphore.
62 The elements of this structure are of type
63 .IR "struct sembuf" ,
64 containing the following members:
65 .PP
66 .in +4n
67 .EX
68 unsigned short sem_num; /* semaphore number */
69 short sem_op; /* semaphore operation */
70 short sem_flg; /* operation flags */
71 .EE
72 .in
73 .PP
74 Flags recognized in
75 .I sem_flg
76 are
77 .B IPC_NOWAIT
78 and
79 .BR SEM_UNDO .
80 If an operation specifies
81 .BR SEM_UNDO ,
82 it will be automatically undone when the process terminates.
83 .PP
84 The set of operations contained in
85 .I sops
86 is performed in
87 .IR "array order" ,
88 and
89 .IR atomically ,
90 that is, the operations are performed either as a complete unit,
91 or not at all.
92 The behavior of the system call if not all operations can be
93 performed immediately depends on the presence of the
94 .B IPC_NOWAIT
95 flag in the individual
96 .I sem_flg
97 fields, as noted below.
98 .PP
99 Each operation is performed on the
100 .IR sem_num \-th
101 semaphore of the semaphore set, where the first semaphore of the set
102 is numbered 0.
103 There are three types of operation, distinguished by the value of
104 .IR sem_op .
105 .PP
106 If
107 .I sem_op
108 is a positive integer, the operation adds this value to
109 the semaphore value
110 .RI ( semval ).
111 Furthermore, if
112 .B SEM_UNDO
113 is specified for this operation, the system subtracts the value
114 .I sem_op
115 from the semaphore adjustment
116 .RI ( semadj )
117 value for this semaphore.
118 This operation can always proceed\[em]it never forces a thread to wait.
119 The calling process must have alter permission on the semaphore set.
120 .PP
121 If
122 .I sem_op
123 is zero, the process must have read permission on the semaphore
124 set.
125 This is a "wait-for-zero" operation: if
126 .I semval
127 is zero, the operation can immediately proceed.
128 Otherwise, if
129 .B IPC_NOWAIT
130 is specified in
131 .IR sem_flg ,
132 .BR semop ()
133 fails with
134 .I errno
135 set to
136 .B EAGAIN
137 (and none of the operations in
138 .I sops
139 is performed).
140 Otherwise,
141 .I semzcnt
142 (the count of threads waiting until this semaphore's value becomes zero)
143 is incremented by one and the thread sleeps until
144 one of the following occurs:
145 .IP \[bu] 3
146 .I semval
147 becomes 0, at which time the value of
148 .I semzcnt
149 is decremented.
150 .IP \[bu]
151 The semaphore set
152 is removed:
153 .BR semop ()
154 fails, with
155 .I errno
156 set to
157 .BR EIDRM .
158 .IP \[bu]
159 The calling thread catches a signal:
160 the value of
161 .I semzcnt
162 is decremented and
163 .BR semop ()
164 fails, with
165 .I errno
166 set to
167 .BR EINTR .
168 .PP
169 If
170 .I sem_op
171 is less than zero, the process must have alter permission on the
172 semaphore set.
173 If
174 .I semval
175 is greater than or equal to the absolute value of
176 .IR sem_op ,
177 the operation can proceed immediately:
178 the absolute value of
179 .I sem_op
180 is subtracted from
181 .IR semval ,
182 and, if
183 .B SEM_UNDO
184 is specified for this operation, the system adds the absolute value of
185 .I sem_op
186 to the semaphore adjustment
187 .RI ( semadj )
188 value for this semaphore.
189 If the absolute value of
190 .I sem_op
191 is greater than
192 .IR semval ,
193 and
194 .B IPC_NOWAIT
195 is specified in
196 .IR sem_flg ,
197 .BR semop ()
198 fails, with
199 .I errno
200 set to
201 .B EAGAIN
202 (and none of the operations in
203 .I sops
204 is performed).
205 Otherwise,
206 .I semncnt
207 (the counter of threads waiting for this semaphore's value to increase)
208 is incremented by one and the thread sleeps until
209 one of the following occurs:
210 .IP \[bu] 3
211 .I semval
212 becomes greater than or equal to the absolute value of
213 .IR sem_op :
214 the operation now proceeds, as described above.
215 .IP \[bu]
216 The semaphore set is removed from the system:
217 .BR semop ()
218 fails, with
219 .I errno
220 set to
221 .BR EIDRM .
222 .IP \[bu]
223 The calling thread catches a signal:
224 the value of
225 .I semncnt
226 is decremented and
227 .BR semop ()
228 fails, with
229 .I errno
230 set to
231 .BR EINTR .
232 .PP
233 On successful completion, the
234 .I sempid
235 value for each semaphore specified in the array pointed to by
236 .I sops
237 is set to the caller's process ID.
238 In addition, the
239 .I sem_otime
240 .\" and
241 .\" .I sem_ctime
242 is set to the current time.
243 .SS semtimedop()
244 .BR semtimedop ()
245 behaves identically to
246 .BR semop ()
247 except that in those cases where the calling thread would sleep,
248 the duration of that sleep is limited by the amount of elapsed
249 time specified by the
250 .I timespec
251 structure whose address is passed in the
252 .I timeout
253 argument.
254 (This sleep interval will be rounded up to the system clock granularity,
255 and kernel scheduling delays mean that the interval
256 may overrun by a small amount.)
257 If the specified time limit has been reached,
258 .BR semtimedop ()
259 fails with
260 .I errno
261 set to
262 .B EAGAIN
263 (and none of the operations in
264 .I sops
265 is performed).
266 If the
267 .I timeout
268 argument is NULL,
269 then
270 .BR semtimedop ()
271 behaves exactly like
272 .BR semop ().
273 .PP
274 Note that if
275 .BR semtimedop ()
276 is interrupted by a signal, causing the call to fail with the error
277 .BR EINTR ,
278 the contents of
279 .I timeout
280 are left unchanged.
281 .SH RETURN VALUE
282 On success,
283 .BR semop ()
284 and
285 .BR semtimedop ()
286 return 0.
287 On failure, they return \-1, and set
288 .I errno
289 to indicate the error.
290 .SH ERRORS
291 .TP
292 .B E2BIG
293 The argument
294 .I nsops
295 is greater than
296 .BR SEMOPM ,
297 the maximum number of operations allowed per system
298 call.
299 .TP
300 .B EACCES
301 The calling process does not have the permissions required
302 to perform the specified semaphore operations,
303 and does not have the
304 .B CAP_IPC_OWNER
305 capability in the user namespace that governs its IPC namespace.
306 .TP
307 .B EAGAIN
308 An operation could not proceed immediately and either
309 .B IPC_NOWAIT
310 was specified in
311 .I sem_flg
312 or the time limit specified in
313 .I timeout
314 expired.
315 .TP
316 .B EFAULT
317 An address specified in either the
318 .I sops
319 or the
320 .I timeout
321 argument isn't accessible.
322 .TP
323 .B EFBIG
324 For some operation the value of
325 .I sem_num
326 is less than 0 or greater than or equal to the number
327 of semaphores in the set.
328 .TP
329 .B EIDRM
330 The semaphore set was removed.
331 .TP
332 .B EINTR
333 While blocked in this system call, the thread caught a signal; see
334 .BR signal (7).
335 .TP
336 .B EINVAL
337 The semaphore set doesn't exist, or
338 .I semid
339 is less than zero, or
340 .I nsops
341 has a nonpositive value.
342 .TP
343 .B ENOMEM
344 The
345 .I sem_flg
346 of some operation specified
347 .B SEM_UNDO
348 and the system does not have enough memory to allocate the undo
349 structure.
350 .TP
351 .B ERANGE
352 For some operation
353 .I sem_op+semval
354 is greater than
355 .BR SEMVMX ,
356 the implementation dependent maximum value for
357 .IR semval .
358 .SH STANDARDS
359 POSIX.1-2008.
360 .SH VERSIONS
361 Linux 2.5.52 (backported into Linux 2.4.22),
362 glibc 2.3.3.
363 POSIX.1-2001, SVr4.
364 .\" SVr4 documents additional error conditions EINVAL, EFBIG, ENOSPC.
365 .SH NOTES
366 The
367 .I sem_undo
368 structures of a process aren't inherited by the child produced by
369 .BR fork (2),
370 but they are inherited across an
371 .BR execve (2)
372 system call.
373 .PP
374 .BR semop ()
375 is never automatically restarted after being interrupted by a signal handler,
376 regardless of the setting of the
377 .B SA_RESTART
378 flag when establishing a signal handler.
379 .PP
380 A semaphore adjustment
381 .RI ( semadj )
382 value is a per-process, per-semaphore integer that is the negated sum
383 of all operations performed on a semaphore specifying the
384 .B SEM_UNDO
385 flag.
386 Each process has a list of
387 .I semadj
388 values\[em]one value for each semaphore on which it has operated using
389 .BR SEM_UNDO .
390 When a process terminates, each of its per-semaphore
391 .I semadj
392 values is added to the corresponding semaphore,
393 thus undoing the effect of that process's operations on the semaphore
394 (but see BUGS below).
395 When a semaphore's value is directly set using the
396 .B SETVAL
397 or
398 .B SETALL
399 request to
400 .BR semctl (2),
401 the corresponding
402 .I semadj
403 values in all processes are cleared.
404 The
405 .BR clone (2)
406 .B CLONE_SYSVSEM
407 flag allows more than one process to share a
408 .I semadj
409 list; see
410 .BR clone (2)
411 for details.
412 .PP
413 The \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, and \fIsemnct\fP values
414 for a semaphore can all be retrieved using appropriate
415 .BR semctl (2)
416 calls.
417 .SS Semaphore limits
418 The following limits on semaphore set resources affect the
419 .BR semop ()
420 call:
421 .TP
422 .B SEMOPM
423 Maximum number of operations allowed for one
424 .BR semop ()
425 call.
426 Before Linux 3.19,
427 .\" commit e843e7d2c88b7db107a86bd2c7145dc715c058f4
428 the default value for this limit was 32.
429 Since Linux 3.19, the default value is 500.
430 On Linux, this limit can be read and modified via the third field of
431 .IR /proc/sys/kernel/sem .
432 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
433 .IR Note :
434 this limit should not be raised above 1000,
435 .\" See comment in Linux 3.19 source file include/uapi/linux/sem.h
436 because of the risk of that
437 .BR semop ()
438 fails due to kernel memory fragmentation when allocating memory to copy the
439 .I sops
440 array.
441 .TP
442 .B SEMVMX
443 Maximum allowable value for
444 .IR semval :
445 implementation dependent (32767).
446 .PP
447 The implementation has no intrinsic limits for
448 the adjust on exit maximum value
449 .RB ( SEMAEM ),
450 the system wide maximum number of undo structures
451 .RB ( SEMMNU )
452 and the per-process maximum number of undo entries system parameters.
453 .SH BUGS
454 When a process terminates, its set of associated
455 .I semadj
456 structures is used to undo the effect of all of the
457 semaphore operations it performed with the
458 .B SEM_UNDO
459 flag.
460 This raises a difficulty: if one (or more) of these semaphore adjustments
461 would result in an attempt to decrease a semaphore's value below zero,
462 what should an implementation do?
463 One possible approach would be to block until all the semaphore
464 adjustments could be performed.
465 This is however undesirable since it could force process termination to
466 block for arbitrarily long periods.
467 Another possibility is that such semaphore adjustments could be ignored
468 altogether (somewhat analogously to failing when
469 .B IPC_NOWAIT
470 is specified for a semaphore operation).
471 Linux adopts a third approach: decreasing the semaphore value
472 as far as possible (i.e., to zero) and allowing process
473 termination to proceed immediately.
474 .PP
475 In Linux 2.6.x, x <= 10, there is a bug that in some circumstances
476 prevents a thread that is waiting for a semaphore value to become
477 zero from being woken up when the value does actually become zero.
478 This bug is fixed in Linux 2.6.11.
479 .\" The bug report:
480 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
481 .\" the fix:
482 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
483 .SH EXAMPLES
484 The following code segment uses
485 .BR semop ()
486 to atomically wait for the value of semaphore 0 to become zero,
487 and then increment the semaphore value by one.
488 .PP
489 .in +4n
490 .EX
491 struct sembuf sops[2];
492 int semid;
493
494 /* Code to set \fIsemid\fP omitted */
495
496 sops[0].sem_num = 0; /* Operate on semaphore 0 */
497 sops[0].sem_op = 0; /* Wait for value to equal 0 */
498 sops[0].sem_flg = 0;
499
500 sops[1].sem_num = 0; /* Operate on semaphore 0 */
501 sops[1].sem_op = 1; /* Increment value by one */
502 sops[1].sem_flg = 0;
503
504 if (semop(semid, sops, 2) == \-1) {
505 perror("semop");
506 exit(EXIT_FAILURE);
507 }
508 .EE
509 .in
510 .PP
511 A further example of the use of
512 .BR semop ()
513 can be found in
514 .BR shmop (2).
515 .SH SEE ALSO
516 .BR clone (2),
517 .BR semctl (2),
518 .BR semget (2),
519 .BR sigaction (2),
520 .BR capabilities (7),
521 .BR sem_overview (7),
522 .BR sysvipc (7),
523 .BR time (7)