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