1SEMOP(2)                   Linux Programmer's Manual                  SEMOP(2)
2
3
4

NAME

6       semop, semtimedop - semaphore operations
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/ipc.h>
11       #include <sys/sem.h>
12
13       int semop(int semid, struct sembuf *sops, unsigned nsops);
14
15       int  semtimedop(int  semid, struct sembuf *sops, unsigned nsops, struct
16       timespec *timeout);
17

DESCRIPTION

19       Each semaphore in a semaphore set has the following associated values:
20
21           unsigned short  semval;   /* semaphore value */
22           unsigned short  semzcnt;  /* # waiting for zero */
23           unsigned short  semncnt;  /* # waiting for increase */
24           pid_t           sempid;   /* process that did last op */
25
26       semop() performs operations on selected semaphores in the set indicated
27       by  semid.   Each of the nsops elements in the array pointed to by sops
28       specifies an operation to be performed on a single semaphore.  The ele‐
29       ments  of this structure are of type struct sembuf, containing the fol‐
30       lowing members:
31
32           unsigned short sem_num;  /* semaphore number */
33           short          sem_op;   /* semaphore operation */
34           short          sem_flg;  /* operation flags */
35
36       Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO.  If an  opera‐
37       tion  specifies  SEM_UNDO,  it  will  be  automatically undone when the
38       process terminates.
39
40       The set of operations contained in sops is performed  in  array  order,
41       and  atomically, that is, the operations are performed either as a com‐
42       plete unit, or not at all.  The behaviour of the system call if not all
43       operations  can be performed immediately depends on the presence of the
44       IPC_NOWAIT flag in the individual sem_flg fields, as noted below.
45
46       Each operation is performed on the sem_num-th semaphore  of  the  sema‐
47       phore  set,  where the first semaphore of the set is numbered 0.  There
48       are three types of operation, distinguished by the value of sem_op.
49
50       If sem_op is a positive integer, the operation adds this value  to  the
51       semaphore  value  (semval).   Furthermore, if SEM_UNDO is specified for
52       this operation, the system updates the process undo count (semadj)  for
53       this  semaphore.  This operation can always proceed — it never forces a
54       process to wait.  The calling process must have alter permission on the
55       semaphore set.
56
57       If  sem_op  is zero, the process must have read permission on the sema‐
58       phore set.  This is a "wait-for-zero" operation: if semval is zero, the
59       operation  can immediately proceed.  Otherwise, if IPC_NOWAIT is speci‐
60       fied in sem_flg, semop() fails with errno set to EAGAIN  (and  none  of
61       the  operations in sops is performed).  Otherwise semzcnt (the count of
62       processes waiting until this semaphore's value becomes zero) is  incre‐
63       mented by one and the process sleeps until one of the following occurs:
64
65       ·      semval  becomes  0, at which time the value of semzcnt is decre‐
66              mented.
67
68       ·      The semaphore set is removed: semop() fails, with errno  set  to
69              EIDRM.
70
71       ·      The  calling  process  catches a signal: the value of semzcnt is
72              decremented and semop() fails, with errno set to EINTR.
73
74       ·      The time limit specified  by  timeout  in  a  semtimedop()  call
75              expires: semop() fails, with errno set to EAGAIN.
76
77       If  sem_op is less than zero, the process must have alter permission on
78       the semaphore set.  If semval is greater than or equal to the  absolute
79       value  of  sem_op,  the operation can proceed immediately: the absolute
80       value of sem_op is subtracted from semval, and, if SEM_UNDO  is  speci‐
81       fied  for  this  operation,  the  system updates the process undo count
82       (semadj) for this semaphore.   If  the  absolute  value  of  sem_op  is
83       greater  than  semval,  and IPC_NOWAIT is specified in sem_flg, semop()
84       fails, with errno set to EAGAIN (and none of the operations in sops  is
85       performed).   Otherwise  semncnt  (the counter of processes waiting for
86       this semaphore's value to increase)  is  incremented  by  one  and  the
87       process sleeps until one of the following occurs:
88
89       ·      semval  becomes  greater  than or equal to the absolute value of
90              sem_op, at which time the value of semncnt is  decremented,  the
91              absolute  value  of  sem_op  is  subtracted  from semval and, if
92              SEM_UNDO is specified for this operation, the system updates the
93              process undo count (semadj) for this semaphore.
94
95       ·      The  semaphore  set  is  removed from the system: semop() fails,
96              with errno set to EIDRM.
97
98       ·      The calling process catches a signal: the value  of  semncnt  is
99              decremented and semop() fails, with errno set to EINTR.
100
101       ·      The  time  limit  specified  by  timeout  in a semtimedop() call
102              expires: the system call fails, with errno set to EAGAIN.
103
104       On successful completion, the sempid value for each semaphore specified
105       in the array pointed to by sops is set to the process ID of the calling
106       process.  In addition, the sem_otime is set to the current time.
107
108       semtimedop() behaves identically to semop() except that in those  cases
109       were  the  calling  process  would sleep, the duration of that sleep is
110       limited by the amount of elapsed time specified by the timespec  struc‐
111       ture  whose  address is passed in the timeout parameter.  If the speci‐
112       fied time limit has been reached, semtimedop() fails with errno set  to
113       EAGAIN (and none of the operations in sops is performed).  If the time‐
114       out parameter is NULL, then semtimedop() behaves exactly like semop().
115

RETURN VALUE

117       If successful semop() and semtimedop() return 0; otherwise they  return
118       -1 with errno indicating the error.
119

ERRORS

121       On failure, errno is set to one of the following:
122
123       E2BIG  The argument nsops is greater than SEMOPM, the maximum number of
124              operations allowed per system call.
125
126       EACCES The calling process does not have the  permissions  required  to
127              perform  the  specified  semaphore operations, and does not have
128              the CAP_IPC_OWNER capability.
129
130       EAGAIN An operation could not proceed immediately and either IPC_NOWAIT
131              was  specified in sem_flg or the time limit specified in timeout
132              expired.
133
134       EFAULT An address specified in either the sops  or  timeout  parameters
135              isn't accessible.
136
137       EFBIG  For  some  operation  the  value  of  sem_num  is less than 0 or
138              greater than or equal to the number of semaphores in the set.
139
140       EIDRM  The semaphore set was removed.
141
142       EINTR  While blocked in this system call, the process caught a signal.
143
144       EINVAL The semaphore set doesn't exist, or semid is less than zero,  or
145              nsops has a non-positive value.
146
147       ENOMEM The  sem_flg of some operation specified SEM_UNDO and the system
148              does not have enough memory to allocate the undo structure.
149
150       ERANGE For some operation sem_op+semval is  greater  than  SEMVMX,  the
151              implementation dependent maximum value for semval.
152

NOTES

154       The  sem_undo structures of a process aren't inherited across a fork(2)
155       system call, but they are inherited across an execve(2) system call.
156
157       semop() is never automatically restarted after being interrupted  by  a
158       signal  handler,  regardless of the setting of the SA_RESTART flag when
159       establishing a signal handler.
160
161       semadj is a per-process integer which is simply the (negative) count of
162       all  semaphore operations performed specifying the SEM_UNDO flag.  When
163       a semaphore's value is directly set using the SETVAL or SETALL  request
164       to  semctl(2),  the  corresponding  semadj  values in all processes are
165       cleared.
166
167       The semval, sempid, semzcnt, and semnct values for a semaphore can  all
168       be retrieved using appropriate semctl(2) calls.
169
170       The  following  limits  on  semaphore  set resources affect the semop()
171       call:
172
173       SEMOPM Maximum number of operations allowed for one semop()  call  (32)
174              (on  Linux,  this  limit  can be read and modified via the third
175              field of /proc/sys/kernel/sem).
176
177       SEMVMX Maximum allowable value  for  semval:  implementation  dependent
178              (32767).
179
180       The implementation has no intrinsic limits for the adjust on exit maxi‐
181       mum value (SEMAEM), the system wide maximum number of  undo  structures
182       (SEMMNU)  and  the  per-process  maximum  number of undo entries system
183       parameters.
184
185       semtimedop() first appeared in Linux 2.5.52, and was subsequently back‐
186       ported into kernel 2.4.22.
187

BUGS

189       When  a  process terminates, its set of associated semadj structures is
190       used to undo the effect of all of the semaphore operations it performed
191       with  the SEM_UNDO flag.  This raises a difficulty: if one (or more) of
192       these semaphore adjustments would result in an attempt  to  decrease  a
193       semaphore's  value  below  zero, what should an implementation do?  One
194       possible approach would be to block until all the semaphore adjustments
195       could  be  performed.  This is however undesirable since it could force
196       process termination to block for  arbitrarily  long  periods.   Another
197       possibility  is  that such semaphore adjustments could be ignored alto‐
198       gether (somewhat analogously to failing when  IPC_NOWAIT  is  specified
199       for  a semaphore operation).  Linux adopts a third approach: decreasing
200       the semaphore value as far as possible (i.e.,  to  zero)  and  allowing
201       process termination to proceed immediately.
202
203       In  kernels  2.6.x,  x <= 10, there is a bug that in some circumstances
204       prevents a process that is waiting for a semaphore value to become zero
205       from being woken up when the value does actually become zero.  This bug
206       is fixed in kernel 2.6.11.
207

CONFORMING TO

209       SVr4, POSIX.1-2001.
210

SEE ALSO

212       semctl(2), semget(2), sigaction(2),  capabilities(7),  sem_overview(7),
213       svipc(7)
214
215
216
217Linux 2.6.9                       2004-11-10                          SEMOP(2)
Impressum