]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/semop.2
strip trailing white space
[thirdparty/man-pages.git] / man2 / semop.2
CommitLineData
fea681da
MK
1.\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
c13182ef 11.\"
fea681da
MK
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
c13182ef 19.\"
fea681da
MK
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
23.\" Modified 1996-10-22, Eric S. Raymond <esr@thyrsus.com>
305a0578 24.\" Modified 2002-01-08, Michael Kerrisk <mtk-manpages@gmx.net>
fea681da 25.\" Modified 2003-04-28, Ernie Petrides <petrides@redhat.com>
305a0578 26.\" Modified 2004-05-27, Michael Kerrisk <mtk-manpages@gmx.net>
fe1c5199
MK
27.\" Modified, 11 Nov 2004, Michael Kerrisk <mtk-manpages@gmx.net>
28.\" Language and formatting clean-ups
29.\" Added notes on /proc files
055fd5fa 30.\" 2005-04-08, mtk, Noted kernel version numbers for semtimedop()
fea681da 31.\"
d9343c5c 32.TH SEMOP 2 2004-11-10 "Linux" "Linux Programmer's Manual"
fea681da
MK
33.SH NAME
34semop, semtimedop \- semaphore operations
35.SH SYNOPSIS
36.nf
37.B
38#include <sys/types.h>
39.B
40#include <sys/ipc.h>
41.B
42#include <sys/sem.h>
fea681da 43.sp
521bf584 44.BI "int semop(int " semid ", struct sembuf *" sops ", unsigned " nsops );
fea681da 45.sp
521bf584
MK
46.BI "int semtimedop(int " semid ", struct sembuf *" sops ", unsigned " nsops ,
47.BI " struct timespec *" timeout );
48.fi
fea681da 49.SH DESCRIPTION
fe1c5199 50Each semaphore in a semaphore set has the following associated values:
fea681da
MK
51.sp
52.in +4n
53.nf
54unsigned short semval; /* semaphore value */
55unsigned short semzcnt; /* # waiting for zero */
56unsigned short semncnt; /* # waiting for increase */
57pid_t sempid; /* process that did last op */
58.sp
59.in -4n
60.fi
fe1c5199
MK
61.BR semop ()
62performs operations on selected semaphores in the set indicated by
fea681da
MK
63.IR semid .
64Each of the
65.I nsops
66elements in the array pointed to by
67.I sops
fe1c5199
MK
68specifies an operation to be performed on a single semaphore.
69The elements of this structure are of type
8478ee02 70.IR "struct sembuf" ,
fe1c5199 71containing the following members:
fea681da
MK
72.sp
73.in +4n
74.nf
fe1c5199
MK
75unsigned short sem_num; /* semaphore number */
76short sem_op; /* semaphore operation */
77short sem_flg; /* operation flags */
fea681da
MK
78.sp
79.in -4n
80.fi
81Flags recognized in
82.I sem_flg
83are
84.B IPC_NOWAIT
85and
86.BR SEM_UNDO .
fe1c5199 87If an operation specifies
fea681da 88.BR SEM_UNDO ,
fe1c5199 89it will be automatically undone when the process terminates.
fea681da
MK
90.PP
91The set of operations contained in
92.I sops
c13182ef
MK
93is performed in
94.IR "array order" ,
95and
fea681da 96.IR atomically ,
87f434df
MK
97that is, the operations are performed either as a complete unit,
98or not at all.
d9bfdb9c 99The behavior of the system call if not all operations can be
fea681da
MK
100performed immediately depends on the presence of the
101.B IPC_NOWAIT
102flag in the individual
103.I sem_flg
104fields, as noted below.
105
106Each operation is performed on the
107.IR sem_num \-th
108semaphore of the semaphore set, where the first semaphore of the set
fe1c5199 109is numbered 0.
fea681da
MK
110There are three types of operation, distinguished by the value of
111.IR sem_op .
112.PP
113If
114.I sem_op
115is a positive integer, the operation adds this value to
116the semaphore value
117.RI ( semval ).
118Furthermore, if
119.B SEM_UNDO
fe1c5199 120is specified for this operation, the system updates the process undo count
fea681da
MK
121.RI ( semadj )
122for this semaphore.
df8a3cac 123This operation can always proceed \(em it never forces a process to wait.
fea681da
MK
124The calling process must have alter permission on the semaphore set.
125.PP
126If
127.I sem_op
fe1c5199 128is zero, the process must have read permission on the semaphore
fea681da
MK
129set.
130This is a "wait-for-zero" operation: if
131.I semval
132is zero, the operation can immediately proceed.
133Otherwise, if
134.B IPC_NOWAIT
fe1c5199 135is specified in
fea681da 136.IR sem_flg ,
fe1c5199
MK
137.BR semop ()
138fails with
fea681da
MK
139.I errno
140set to
141.B EAGAIN
142(and none of the operations in
143.I sops
144is performed).
145Otherwise
146.I semzcnt
147(the count of processes waiting until this semaphore's value becomes zero)
148is incremented by one and the process sleeps until
149one of the following occurs:
150.IP \(bu
151.I semval
152becomes 0, at which time the value of
153.I semzcnt
154is decremented.
155.IP \(bu
156The semaphore set
fe1c5199
MK
157is removed:
158.BR semop ()
159fails, with
fea681da
MK
160.I errno
161set to
162.BR EIDRM .
163.IP \(bu
164The calling process catches a signal:
165the value of
166.I semzcnt
fe1c5199
MK
167is decremented and
168.BR semop ()
169fails, with
fea681da
MK
170.I errno
171set to
172.BR EINTR .
173.IP \(bu
174The time limit specified by
175.I timeout
176in a
fe1c5199
MK
177.BR semtimedop ()
178call expires:
179.BR semop ()
180fails, with
fea681da
MK
181.I errno
182set to
183.BR EAGAIN .
184.PP
185If
186.I sem_op
187is less than zero, the process must have alter permission on the
188semaphore set.
189If
190.I semval
191is greater than or equal to the absolute value of
192.IR sem_op ,
193the operation can proceed immediately:
194the absolute value of
195.I sem_op
196is subtracted from
197.IR semval ,
198and, if
199.B SEM_UNDO
fe1c5199 200is specified for this operation, the system updates the process undo count
fea681da
MK
201.RI ( semadj )
202for this semaphore.
203If the absolute value of
204.I sem_op
205is greater than
206.IR semval ,
207and
208.B IPC_NOWAIT
fe1c5199 209is specified in
fea681da 210.IR sem_flg ,
fe1c5199
MK
211.BR semop ()
212fails, with
fea681da
MK
213.I errno
214set to
215.B EAGAIN
216(and none of the operations in
217.I sops
218is performed).
219Otherwise
220.I semncnt
221(the counter of processes waiting for this semaphore's value to increase)
222is incremented by one and the process sleeps until
223one of the following occurs:
224.IP \(bu
225.I semval
226becomes greater than or equal to the absolute value of
227.IR sem_op ,
228at which time the value of
229.I semncnt
230is decremented, the absolute value of
231.I sem_op
232is subtracted from
233.I semval
234and, if
235.B SEM_UNDO
fe1c5199 236is specified for this operation, the system updates the process undo count
fea681da
MK
237.RI ( semadj )
238for this semaphore.
239.IP \(bu
fe1c5199
MK
240The semaphore set is removed from the system:
241.BR semop ()
242fails, with
fea681da
MK
243.I errno
244set to
245.BR EIDRM .
246.IP \(bu
247The calling process catches a signal:
248the value of
249.I semncnt
c13182ef 250is decremented and
fe1c5199
MK
251.BR semop ()
252fails, with
fea681da
MK
253.I errno
254set to
255.BR EINTR .
256.IP \(bu
257The time limit specified by
258.I timeout
259in a
fe1c5199 260.BR semtimedop ()
fea681da
MK
261call expires: the system call fails, with
262.I errno
263set to
264.BR EAGAIN .
265.PP
266On successful completion, the
267.I sempid
268value for each semaphore specified in the array pointed to by
269.I sops
270is set to the process ID of the calling process.
271In addition, the
272.I sem_otime
273.\" and
274.\" .I sem_ctime
275is set to the current time.
276.PP
fe1c5199 277.BR semtimedop ()
c13182ef 278behaves identically to
fe1c5199 279.BR semop ()
fea681da
MK
280except that in those cases were the calling process would sleep,
281the duration of that sleep is limited by the amount of elapsed
282time specified by the
283.B timespec
284structure whose address is passed in the
285.I timeout
c13182ef
MK
286parameter.
287If the specified time limit has been reached,
fe1c5199
MK
288.BR semtimedop ()
289fails with
fea681da
MK
290.I errno
291set to
292.B EAGAIN
293(and none of the operations in
294.I sops
295is performed).
296If the
297.I timeout
859ae99c 298parameter is NULL,
fea681da 299then
fe1c5199 300.BR semtimedop ()
fea681da 301behaves exactly like
fe1c5199 302.BR semop ().
fea681da 303.SH "RETURN VALUE"
fe1c5199
MK
304If successful
305.BR semop ()
c13182ef 306and
fe1c5199
MK
307.BR semtimedop ()
308return 0;
8729177b 309otherwise they return \-1
fea681da
MK
310with
311.I errno
312indicating the error.
313.SH ERRORS
314On failure,
315.I errno
316is set to one of the following:
317.TP
318.B E2BIG
319The argument
320.I nsops
321is greater than
322.BR SEMOPM ,
323the maximum number of operations allowed per system
324call.
325.TP
326.B EACCES
327The calling process does not have the permissions required
328to perform the specified semaphore operations,
329and does not have the
330.B CAP_IPC_OWNER
331capability.
332.TP
333.B EAGAIN
334An operation could not proceed immediately and either
335.BR IPC_NOWAIT
fe1c5199 336was specified in
fea681da
MK
337.I sem_flg
338or the time limit specified in
339.I timeout
340expired.
341.TP
342.B EFAULT
343An address specified in either the
344.I sops
345or
346.I timeout
347parameters isn't accessible.
348.TP
349.B EFBIG
350For some operation the value of
351.I sem_num
352is less than 0 or greater than or equal to the number
353of semaphores in the set.
354.TP
355.B EIDRM
356The semaphore set was removed.
357.TP
358.B EINTR
359While blocked in this system call, the process caught a signal.
360.TP
361.B EINVAL
362The semaphore set doesn't exist, or
363.I semid
364is less than zero, or
365.I nsops
366has a non-positive value.
367.TP
368.B ENOMEM
369The
370.I sem_flg
fe1c5199 371of some operation specified
fea681da
MK
372.B SEM_UNDO
373and the system does not have enough memory to allocate the undo
374structure.
375.TP
376.B ERANGE
377For some operation
378.I sem_op+semval
379is greater than
380.BR SEMVMX ,
381the implementation dependent maximum value for
382.IR semval .
4f73a46c
MK
383.SH VERSIONS
384.BR semtimedop ()
385first appeared in Linux 2.5.52,
386and was subsequently backported into kernel 2.4.22.
988db661 387Glibc support for
4f73a46c
MK
388.BR semtimedop ()
389first appeared in version 2.3.3.
a1d5f77c
MK
390.SH "CONFORMING TO"
391SVr4, POSIX.1-2001.
392.\" SVr4 documents additional error conditions EINVAL, EFBIG, ENOSPC.
fea681da
MK
393.SH NOTES
394The
395.I sem_undo
396structures of a process aren't inherited across a
397.BR fork (2)
fe1c5199 398system call, but they are inherited across an
fea681da
MK
399.BR execve (2)
400system call.
401.PP
fe1c5199 402.BR semop ()
fea681da
MK
403is never automatically restarted after being interrupted by a signal handler,
404regardless of the setting of the
405.B SA_RESTART
fe1c5199 406flag when establishing a signal handler.
fea681da
MK
407.PP
408.I semadj
409is a per\-process integer which is simply the (negative) count
410of all semaphore operations performed specifying the
411.B SEM_UNDO
412flag.
413When a semaphore's value is directly set using the
414.B SETVAL
415or
416.B SETALL
417request to
418.BR semctl (2),
419the corresponding
420.I semadj
421values in all processes are cleared.
422.PP
423The \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, and \fIsemnct\fP values
424for a semaphore can all be retrieved using appropriate
425.BR semctl (2)
426calls.
427.PP
540036b2 428The following limits on semaphore set resources affect the
fe1c5199 429.BR semop ()
fea681da
MK
430call:
431.TP
432.B SEMOPM
433Maximum number of operations allowed for one
fe1c5199
MK
434.BR semop ()
435call (32)
436(on Linux, this limit can be read and modified via the third field of
437.IR /proc/sys/kernel/sem ).
438.\" This /proc file is not available in Linux 2.2 and earlier -- MTK
fea681da
MK
439.TP
440.B SEMVMX
441Maximum allowable value for
442.IR semval :
443implementation dependent (32767).
444.PP
445The implementation has no intrinsic limits for
446the adjust on exit maximum value
447.RB ( SEMAEM ),
448the system wide maximum number of undo structures
449.RB ( SEMMNU )
450and the per\-process maximum number of undo entries system parameters.
451.SH BUGS
452When a process terminates, its set of associated
453.I semadj
454structures is used to undo the effect of all of the
455semaphore operations it performed with the
456.B SEM_UNDO
457flag.
458This raises a difficulty: if one (or more) of these semaphore adjustments
459would result in an attempt to decrease a semaphore's value below zero,
460what should an implementation do?
461One possible approach would be to block until all the semaphore
462adjustments could be performed.
463This is however undesirable since it could force process termination to
464block for arbitrarily long periods.
465Another possibility is that such semaphore adjustments could be ignored
466altogether (somewhat analogously to failing when
467.B IPC_NOWAIT
468is specified for a semaphore operation).
469Linux adopts a third approach: decreasing the semaphore value
470as far as possible (i.e., to zero) and allowing process
471termination to proceed immediately.
3e4d52e1 472
c13182ef
MK
473In kernels 2.6.x, x <= 10, there is a bug that in some circumstances
474prevents a process that is waiting for a semaphore value to become
3e4d52e1
MK
475zero from being woken up when the value does actually become zero.
476This bug is fixed in kernel 2.6.11.
477.\" The bug report:
478.\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
479.\" the fix:
480.\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
fea681da
MK
481.SH "SEE ALSO"
482.BR semctl (2),
483.BR semget (2),
484.BR sigaction (2),
f675ea37 485.BR capabilities (7),
2c5e151c
MK
486.BR sem_overview (7),
487.BR svipc (7)