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