semop()

NAME

semop() - operate on a set of semaphores

SYNOPSIS

#include <sys/sem.h>

int semop(int semid, struct sembuf *sops, size_t nsops)

DESCRIPTION

The semop(2) function performs atomic actions on the set of semaphores associated with the semaphore associated with semid. The operations are specified in sops, a user-defined array of semaphore operation structures (sembuf structures). The argument nsops specifies the number of structures in the array.

The structure sembuf contains the following members:

struct sembuf {
   unsigned short int sem_num   /* semaphore number */
   short int sem_op			 /* semaphore operation */
   short int sem_flg			/* operation flags */
};

There are really only three semaphore operations (specified by sem_op although the conditions under which each occurs can be difficult to understand. The three semaphore operations are:

The actual operation performed depends upon the permissions of the calling process, the value of sem_op the value of semval (in the sembuf structure), and the value of sem_flg

  1. semop is negative and the calling process has alter permission
  2. Semop is positive and the calling process has alter permission
  3. Semop is zero and the calling process has read permission

Some operations adjust the value of semadj a per-process variable stored in the process table. This happens if (semflg & SEM_UNDO) is non-zero.

The header file <sys/sem.h> includes <sys/ipc.h>.

RETURN VALUE

On success, semop(2) returns 0. for each semaphore in the array pointed to by sops, the value of sempid is set to the process ID of the calling process.

On failure, semop(2) returns -1 and sets errno to indicate the error.

ERRORS

The semop(2) call can fail for any of these reasons:

[E2BIG]
The value of nsops is too large.
[EACCES]
The calling process did not have permission to perform the operation. Although a semaphore identifier exists for key, its permissions were not compatible with the low-order 9 bits of semflg.
[EAGAIN]
Although the call would suspend the calling process, the value of (sem_flg & IPC_NOWAIT) was non-zero
[EFBIG]
The value of sem_num was outside the range 0 to (number of semaphores in the set associated with semid-1).
[EIDRM]
The semaphore identifier semid was removed from the system.
[EINTR]
An interrupt occurred.
[EINVAL]
Semid was not a valid semaphore identifier.
[EINVAL]
The number of semaphores for which the process requested a SEM_UNDO exceeded the system maximum.
[ENOSPC]
The number of semaphores for which the process requested a SEM_UNDO exceeded the process maximum.
[ERANGE]
The operation would cause a semval to overflow the system limit, or the operation would cause a semadj to overflow the system limit.

SEE ALSO

exec(1)

fork(2)

semctl(2)

semget(2)