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