SEMOP(2)

NAME
semop - atomic array of operations on a semaphore set

LIBRARY
libc

SYNOPSIS
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int
semop(int semid, struct sembuf array[], unsigned nops);

DESCRIPTION
Semop() atomically performs the array of operations indicated by array on the semaphore set indicated by semid. The length of array is indicated by nops. Each operation is encoded in a struct sembuf, which is defined as follows:

    struct sembuf {
            u_short sem_num;        /* semaphore # */
            short   sem_op;         /* semaphore operation */
            short   sem_flg;        /* operation flags */
    };

For each element in array, sem_op and sem_flg determine an operation to be performed on semaphore number sem_num in the set. The values SEM_UNDO and IPC_NOWAIT may be OR'ed into the sem_flg member in order to modify the behavior of the given operation.

The operation performed depends as follows on the value of sem_op:

•   When sem_op is positive, the semaphore's value is incremented by sem_op's value. If SEM_UNDO is specified, the semaphore's adjust on exit value is decremented by sem_op's value. A positive value for sem_op generally corresponds to a process releasing a resource associated with the semaphore.

•   The behavior when sem_op is negative depends on the current value of the semaphore:

    •   If the current value of the semaphore is greater than or equal to the absolute value of sem_op, then the value is decremented by the absolute value of sem_op. If SEM_UNDO is specified, the semaphore's adjust on exit value is incremented by the absolute value of sem_op.

    •   If the current value of the semaphore is less than sem_op's value, one of the following happens:

        •   If IPC_NOWAIT was specified, then semop() returns immediately with a return value of EAGAIN.

        •   If some other process has removed the semaphore with the IPC_RMID option of semctl(), then semop() returns immediately with a return value of EINVAL.

        •   Otherwise, the calling process is put to sleep until the semaphore's value is greater than or equal to the absolute value of sem_op. When this condition becomes true, the semaphore's value is decremented by the absolute value of sem_op, and the semaphore's adjust on exit value is incremented by the absolute value of sem_op.

        A negative value for sem_op generally means that a process is waiting for a resource to become available.

•   When sem_op is zero, the process waits for the semaphore's value to become zero. If it is already zero, the call to semop() can return immediately. Otherwise, the calling process is put to sleep until the semaphore's value becomes zero.

For each semaphore a process has in use, the kernel maintains an `adjust on exit' value, as alluded to earlier. When a process exits, either voluntarily or involuntarily, the adjust on exit value for each semaphore is added to the semaphore's value. This can be used to insure that a resource is released if a process terminates unexpectedly.

RETURN VALUES

ENVIRONMENT
The XSI Interprocess Communication family of functions is also available as an implementation in userspace. To use it, the sysvipcd(8) daemon has to be running.

If the USR_SYSVIPC variable is set in a process' environment, the process and its children will use the userspace implementation.

ERRORS
Semop() will fail if:

[EINVAL]
    No semaphore set corresponds to semid.

[EACCES]
    Permission denied due to mismatch between operation and mode of semaphore set.

[EAGAIN]
    The semaphore's value was less than sem_op, and IPC_NOWAIT was specified.

[E2BIG]
    Too many operations were specified.

[EFBIG]
    sem_num was not in the range of valid semaphores for the set.

SEE ALSO
semctl(2), semget(2)

AUTHORS
-nosplit The DragonFly specific userspace implementation (see ENVIRONMENT) was written by Larisa Grigore.