]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/semop.2
"lower-most" --> "lowermost"
[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.
11.\"
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.
19.\"
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
fea681da 30.\"
fe1c5199 31.TH SEMOP 2 2004-11-10 "Linux 2.6.9" "Linux Programmer's Manual"
fea681da
MK
32.SH NAME
33semop, semtimedop \- semaphore operations
34.SH SYNOPSIS
35.nf
36.B
37#include <sys/types.h>
38.B
39#include <sys/ipc.h>
40.B
41#include <sys/sem.h>
42.fi
43.sp
44.BI "int semop(int " semid ,
45.BI "struct sembuf *" sops ,
46.BI "unsigned " nsops );
47.sp
48.BI "int semtimedop(int " semid ,
49.BI "struct sembuf *" sops ,
50.BI "unsigned " nsops ,
51.BI "struct timespec *" timeout );
52.SH DESCRIPTION
fe1c5199 53Each semaphore in a semaphore set has the following associated values:
fea681da
MK
54.sp
55.in +4n
56.nf
57unsigned short semval; /* semaphore value */
58unsigned short semzcnt; /* # waiting for zero */
59unsigned short semncnt; /* # waiting for increase */
60pid_t sempid; /* process that did last op */
61.sp
62.in -4n
63.fi
fe1c5199
MK
64.BR semop ()
65performs operations on selected semaphores in the set indicated by
fea681da
MK
66.IR semid .
67Each of the
68.I nsops
69elements in the array pointed to by
70.I sops
fe1c5199
MK
71specifies an operation to be performed on a single semaphore.
72The elements of this structure are of type
73.BR "struct sembuf" ,
74containing the following members:
fea681da
MK
75.sp
76.in +4n
77.nf
fe1c5199
MK
78unsigned short sem_num; /* semaphore number */
79short sem_op; /* semaphore operation */
80short sem_flg; /* operation flags */
fea681da
MK
81.sp
82.in -4n
83.fi
84Flags recognized in
85.I sem_flg
86are
87.B IPC_NOWAIT
88and
89.BR SEM_UNDO .
fe1c5199 90If an operation specifies
fea681da 91.BR SEM_UNDO ,
fe1c5199 92it will be automatically undone when the process terminates.
fea681da
MK
93.PP
94The set of operations contained in
95.I sops
96is performed
97.IR atomically ,
98that is, the operations are performed at the same time, and only
99if they can all be simultaneously performed.
100The behaviour of the system call if not all operations can be
101performed immediately depends on the presence of the
102.B IPC_NOWAIT
103flag in the individual
104.I sem_flg
105fields, as noted below.
106
107Each operation is performed on the
108.IR sem_num \-th
109semaphore of the semaphore set, where the first semaphore of the set
fe1c5199 110is numbered 0.
fea681da
MK
111There are three types of operation, distinguished by the value of
112.IR sem_op .
113.PP
114If
115.I sem_op
116is a positive integer, the operation adds this value to
117the semaphore value
118.RI ( semval ).
119Furthermore, if
120.B SEM_UNDO
fe1c5199 121is specified for this operation, the system updates the process undo count
fea681da
MK
122.RI ( semadj )
123for this semaphore.
124This operation can always proceed \- it never forces a process to wait.
125The calling process must have alter permission on the semaphore set.
126.PP
127If
128.I sem_op
fe1c5199 129is zero, the process must have read permission on the semaphore
fea681da
MK
130set.
131This is a "wait-for-zero" operation: if
132.I semval
133is zero, the operation can immediately proceed.
134Otherwise, if
135.B IPC_NOWAIT
fe1c5199 136is specified in
fea681da 137.IR sem_flg ,
fe1c5199
MK
138.BR semop ()
139fails with
fea681da
MK
140.I errno
141set to
142.B EAGAIN
143(and none of the operations in
144.I sops
145is performed).
146Otherwise
147.I semzcnt
148(the count of processes waiting until this semaphore's value becomes zero)
149is incremented by one and the process sleeps until
150one of the following occurs:
151.IP \(bu
152.I semval
153becomes 0, at which time the value of
154.I semzcnt
155is decremented.
156.IP \(bu
157The semaphore set
fe1c5199
MK
158is removed:
159.BR semop ()
160fails, with
fea681da
MK
161.I errno
162set to
163.BR EIDRM .
164.IP \(bu
165The calling process catches a signal:
166the value of
167.I semzcnt
fe1c5199
MK
168is decremented and
169.BR semop ()
170fails, with
fea681da
MK
171.I errno
172set to
173.BR EINTR .
174.IP \(bu
175The time limit specified by
176.I timeout
177in a
fe1c5199
MK
178.BR semtimedop ()
179call expires:
180.BR semop ()
181fails, with
fea681da
MK
182.I errno
183set to
184.BR EAGAIN .
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
fe1c5199 201is specified for this operation, the system updates the process undo count
fea681da
MK
202.RI ( semadj )
203for this semaphore.
204If the absolute value of
205.I sem_op
206is greater than
207.IR semval ,
208and
209.B IPC_NOWAIT
fe1c5199 210is specified in
fea681da 211.IR sem_flg ,
fe1c5199
MK
212.BR semop ()
213fails, with
fea681da
MK
214.I errno
215set to
216.B EAGAIN
217(and none of the operations in
218.I sops
219is performed).
220Otherwise
221.I semncnt
222(the counter of processes waiting for this semaphore's value to increase)
223is incremented by one and the process sleeps until
224one of the following occurs:
225.IP \(bu
226.I semval
227becomes greater than or equal to the absolute value of
228.IR sem_op ,
229at which time the value of
230.I semncnt
231is decremented, the absolute value of
232.I sem_op
233is subtracted from
234.I semval
235and, if
236.B SEM_UNDO
fe1c5199 237is specified for this operation, the system updates the process undo count
fea681da
MK
238.RI ( semadj )
239for this semaphore.
240.IP \(bu
fe1c5199
MK
241The semaphore set is removed from the system:
242.BR semop ()
243fails, with
fea681da
MK
244.I errno
245set to
246.BR EIDRM .
247.IP \(bu
248The calling process catches a signal:
249the value of
250.I semncnt
fe1c5199
MK
251is decremented and
252.BR semop ()
253fails, with
fea681da
MK
254.I errno
255set to
256.BR EINTR .
257.IP \(bu
258The time limit specified by
259.I timeout
260in a
fe1c5199 261.BR semtimedop ()
fea681da
MK
262call expires: the system call fails, with
263.I errno
264set to
265.BR EAGAIN .
266.PP
267On successful completion, the
268.I sempid
269value for each semaphore specified in the array pointed to by
270.I sops
271is set to the process ID of the calling process.
272In addition, the
273.I sem_otime
274.\" and
275.\" .I sem_ctime
276is set to the current time.
277.PP
fe1c5199
MK
278.BR semtimedop ()
279behaves identically to
280.BR semop ()
fea681da
MK
281except that in those cases were the calling process would sleep,
282the duration of that sleep is limited by the amount of elapsed
283time specified by the
284.B timespec
285structure whose address is passed in the
286.I timeout
287parameter. If 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
298parameter is
299.BR NULL ,
300then
fe1c5199 301.BR semtimedop ()
fea681da 302behaves exactly like
fe1c5199 303.BR semop ().
fea681da 304.SH "RETURN VALUE"
fe1c5199
MK
305If successful
306.BR semop ()
307and
308.BR semtimedop ()
309return 0;
310otherwise they return -1
fea681da
MK
311with
312.I errno
313indicating the error.
314.SH ERRORS
315On failure,
316.I errno
317is set to one of the following:
318.TP
319.B E2BIG
320The argument
321.I nsops
322is greater than
323.BR SEMOPM ,
324the maximum number of operations allowed per system
325call.
326.TP
327.B EACCES
328The calling process does not have the permissions required
329to perform the specified semaphore operations,
330and does not have the
331.B CAP_IPC_OWNER
332capability.
333.TP
334.B EAGAIN
335An operation could not proceed immediately and either
336.BR IPC_NOWAIT
fe1c5199 337was specified in
fea681da
MK
338.I sem_flg
339or the time limit specified in
340.I timeout
341expired.
342.TP
343.B EFAULT
344An address specified in either the
345.I sops
346or
347.I timeout
348parameters isn't accessible.
349.TP
350.B EFBIG
351For some operation the value of
352.I sem_num
353is less than 0 or greater than or equal to the number
354of semaphores in the set.
355.TP
356.B EIDRM
357The semaphore set was removed.
358.TP
359.B EINTR
360While blocked in this system call, the process caught a signal.
361.TP
362.B EINVAL
363The semaphore set doesn't exist, or
364.I semid
365is less than zero, or
366.I nsops
367has a non-positive value.
368.TP
369.B ENOMEM
370The
371.I sem_flg
fe1c5199 372of some operation specified
fea681da
MK
373.B SEM_UNDO
374and the system does not have enough memory to allocate the undo
375structure.
376.TP
377.B ERANGE
378For some operation
379.I sem_op+semval
380is greater than
381.BR SEMVMX ,
382the implementation dependent maximum value for
383.IR semval .
384.SH NOTES
385The
386.I sem_undo
387structures of a process aren't inherited across a
388.BR fork (2)
fe1c5199 389system call, but they are inherited across an
fea681da
MK
390.BR execve (2)
391system call.
392.PP
fe1c5199 393.BR semop ()
fea681da
MK
394is never automatically restarted after being interrupted by a signal handler,
395regardless of the setting of the
396.B SA_RESTART
fe1c5199 397flag when establishing a signal handler.
fea681da
MK
398.PP
399.I semadj
400is a per\-process integer which is simply the (negative) count
401of all semaphore operations performed specifying the
402.B SEM_UNDO
403flag.
404When a semaphore's value is directly set using the
405.B SETVAL
406or
407.B SETALL
408request to
409.BR semctl (2),
410the corresponding
411.I semadj
412values in all processes are cleared.
413.PP
414The \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, and \fIsemnct\fP values
415for a semaphore can all be retrieved using appropriate
416.BR semctl (2)
417calls.
418.PP
540036b2 419The following limits on semaphore set resources affect the
fe1c5199 420.BR semop ()
fea681da
MK
421call:
422.TP
423.B SEMOPM
424Maximum number of operations allowed for one
fe1c5199
MK
425.BR semop ()
426call (32)
427(on Linux, this limit can be read and modified via the third field of
428.IR /proc/sys/kernel/sem ).
429.\" This /proc file is not available in Linux 2.2 and earlier -- MTK
fea681da
MK
430.TP
431.B SEMVMX
432Maximum allowable value for
433.IR semval :
434implementation dependent (32767).
435.PP
436The implementation has no intrinsic limits for
437the adjust on exit maximum value
438.RB ( SEMAEM ),
439the system wide maximum number of undo structures
440.RB ( SEMMNU )
441and the per\-process maximum number of undo entries system parameters.
442.SH BUGS
443When a process terminates, its set of associated
444.I semadj
445structures is used to undo the effect of all of the
446semaphore operations it performed with the
447.B SEM_UNDO
448flag.
449This raises a difficulty: if one (or more) of these semaphore adjustments
450would result in an attempt to decrease a semaphore's value below zero,
451what should an implementation do?
452One possible approach would be to block until all the semaphore
453adjustments could be performed.
454This is however undesirable since it could force process termination to
455block for arbitrarily long periods.
456Another possibility is that such semaphore adjustments could be ignored
457altogether (somewhat analogously to failing when
458.B IPC_NOWAIT
459is specified for a semaphore operation).
460Linux adopts a third approach: decreasing the semaphore value
461as far as possible (i.e., to zero) and allowing process
462termination to proceed immediately.
463.SH "CONFORMING TO"
464SVr4, SVID. SVr4 documents additional error conditions EINVAL, EFBIG,
465ENOSPC.
466.SH "SEE ALSO"
467.BR semctl (2),
468.BR semget (2),
469.BR sigaction (2),
470.BR ipc (5),
471.BR capabilities (7)