semop, semtimedop - System V semaphore operations
#include <sys/sem.h>
int semop(int semid, struct sembuf *sops, size_t nsops);
int semtimedop(int semid, struct sembuf *sops, size_t nsops,
const struct timespec *_Nullable timeout);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
Each semaphore in a System V semaphore set has the following associated values:
unsigned short semval; /* semaphore value */
unsigned short semzcnt; /* # waiting for zero */
unsigned short semncnt; /* # waiting for increase */
pid_t sempid; /* PID of process that last
modified the semaphore value */
semop() performs operations on selected semaphores
in the set indicated by semid
. Each of the nsops
elements in the array pointed to by sops
is a structure that
specifies an operation to be performed on a single semaphore. The
elements of this structure are of type struct sembuf
,
containing the following members:
unsigned short sem_num; /* semaphore number */
short sem_op; /* semaphore operation */
short sem_flg; /* operation flags */
Flags recognized in sem_flg
are IPC_NOWAIT
and SEM_UNDO. If an operation specifies
SEM_UNDO, it will be automatically undone when the
process terminates.
The set of operations contained in sops
is performed in
array order
, and atomically
, that is, the operations
are performed either as a complete unit, or not at all. The behavior of
the system call if not all operations can be performed immediately
depends on the presence of the IPC_NOWAIT flag in the
individual sem_flg
fields, as noted below.
Each operation is performed on the sem_num
-th semaphore of
the semaphore set, where the first semaphore of the set is numbered 0.
There are three types of operation, distinguished by the value of
sem_op
.
If sem_op
is a positive integer, the operation adds this
value to the semaphore value (semval
). Furthermore, if
SEM_UNDO is specified for this operation, the system
subtracts the value sem_op
from the semaphore adjustment
(semadj
) value for this semaphore. This operation can always
proceed—it never forces a thread to wait. The calling process must have
alter permission on the semaphore set.
If sem_op
is zero, the process must have read permission on
the semaphore set. This is a "wait-for-zero" operation: if
semval
is zero, the operation can immediately proceed.
Otherwise, if IPC_NOWAIT is specified in
sem_flg
, semop() fails with errno
set
to EAGAIN (and none of the operations in sops
is performed). Otherwise, semzcnt
(the count of threads waiting
until this semaphore's value becomes zero) is incremented by one and the
thread sleeps until one of the following occurs:
semval
becomes 0, at which time the value of
semzcnt
is decremented.
The semaphore set is removed: semop() fails,
with errno
set to EIDRM.
The calling thread catches a signal: the value of
semzcnt
is decremented and semop() fails, with
errno
set to EINTR.
If sem_op
is less than zero, the process must have alter
permission on the semaphore set. If semval
is greater than or
equal to the absolute value of sem_op
, the operation can
proceed immediately: the absolute value of sem_op
is subtracted
from semval
, and, if SEM_UNDO is specified for
this operation, the system adds the absolute value of sem_op
to
the semaphore adjustment (semadj
) value for this semaphore. If
the absolute value of sem_op
is greater than semval
,
and IPC_NOWAIT is specified in sem_flg
,
semop() fails, with errno
set to
EAGAIN (and none of the operations in sops
is
performed). Otherwise, semncnt
(the counter of threads waiting
for this semaphore's value to increase) is incremented by one and the
thread sleeps until one of the following occurs:
semval
becomes greater than or equal to the absolute
value of sem_op
: the operation now proceeds, as described
above.
The semaphore set is removed from the system:
semop() fails, with errno
set to
EIDRM.
The calling thread catches a signal: the value of
semncnt
is decremented and semop() fails, with
errno
set to EINTR.
On successful completion, the sempid
value for each
semaphore specified in the array pointed to by sops
is set to
the caller's process ID. In addition, the sem_otime
is set to
the current time.
semtimedop() behaves identically to
semop() except that in those cases where the calling
thread would sleep, the duration of that sleep is limited by the amount
of elapsed time specified by the timespec
structure whose
address is passed in the timeout
argument. (This sleep interval
will be rounded up to the system clock granularity, and kernel
scheduling delays mean that the interval may overrun by a small amount.)
If the specified time limit has been reached,
semtimedop() fails with errno
set to
EAGAIN (and none of the operations in sops
is
performed). If the timeout
argument is NULL, then
semtimedop() behaves exactly like
semop().
Note that if semtimedop() is interrupted by a
signal, causing the call to fail with the error EINTR,
the contents of timeout
are left unchanged.
On success, semop() and
semtimedop() return 0. On failure, they return -1, and
set errno
to indicate the error.
The following code segment uses semop() to atomically wait for the value of semaphore 0 to become zero, and then increment the semaphore value by one.
struct sembuf sops[2];
int semid;
/* Code to set semid omitted */
sops[0].sem_num = 0; /* Operate on semaphore 0 */
sops[0].sem_op = 0; /* Wait for value to equal 0 */
sops[0].sem_flg = 0;
sops[1].sem_num = 0; /* Operate on semaphore 0 */
sops[1].sem_op = 1; /* Increment value by one */
sops[1].sem_flg = 0;
if (semop(semid, sops, 2) == -1) {
perror("semop");
exit(EXIT_FAILURE);
}
A further example of the use of semop() can be found in shmop(2).
clone(2), semctl(2), semget(2), sigaction(2), capabilities(7), sem_overview(7), sysvipc(7), time(7)