| Linux / Unix Command: semop |
NAME
semop - semaphore operationsSYNOPSIS
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h>
int semop(int semid, struct sembuf *sops, unsigned nsops);
DESCRIPTION
A semaphore is represented by an anonymous structure including the following members:
unsigned short semval; /* semaphore value */ unsigned short semzcnt; /* # waiting for zero */ unsigned short semncnt; /* # waiting for increase */ pid_t sempid; /* process that did last op */The function semop performs operations on selected members of the semaphore set indicated by semid. Each of the nsops elements in the array pointed to by sops specifies an operation to be performed on a semaphore by a struct sembuf including 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 asserts SEM_UNDO, it will be undone when the process exits.
The set of operations contained in sops is performed atomically, that is, the operations are performed at the same time, and only if they can all be simultaneously performed. The behaviour 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 semaphore 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 asserted for this operation, the system updates the process undo count (semadj) for this semaphore. This operation can always proceed - it never forces a process to wait. The calling process must have alter permission on the semaphore set.
If sem_op is zero, the process must have read access 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 asserted in sem_flg, the system call fails with errno set to EAGAIN (and none of the operations in sops is performed). Otherwise semzcnt (the count of processes waiting until this semaphore's value becomes zero) is incremented by one and the process sleeps until one of the following occurs:
- *
- semval becomes 0, at which time the value of semzcnt is decremented.
- *
- The semaphore set is removed: the system call fails, with errno set to EIDRM.
- *
- The calling process catches a signal: the value of semzcnt is decremented and the system call 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 asserted for this operation, the system updates the process undo count (semadj) for this semaphore. If the absolute value of sem_op is greater than semval, and IPC_NOWAIT is asserted in sem_flg, the system call fails, with errno set to EAGAIN (and none of the operations in sops is performed). Otherwise semncnt (the counter of processes waiting for this semaphore's value to increase) is incremented by one and the process sleeps until one of the following occurs:
- *
- semval becomes greater than or equal to the absolute value of sem_op, at which time the value of semncnt is decremented, the absolute value of sem_op is subtracted from semval and, if SEM_UNDO is asserted for this operation, the system updates the process undo count (semadj) for this semaphore.
- *
- The semaphore set is removed from the system: the system call fails with errno set to EIDRM.
- *
- The calling process catches a signal: the value of semncnt is decremented and the system call 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 process ID of the calling process. In addition, the sem_otime is set to the current time.
RETURN VALUE
If successful the system call returns 0, otherwise it returns -1 with errno indicating the error.ERRORS
On failure, errno is set to one of the following:- E2BIG
- The argument nsops is greater than SEMOPM, the maximum number of operations allowed per system call.
- EACCES
- The calling process has no access permissions on the semaphore set as required by one of the specified operations.
- EAGAIN
- An operation could not proceed immediately and IPC_NOWAIT was asserted in its sem_flg.
- EFAULT
- The address pointed to by sops isn't accessible.
- EFBIG
- For some operation the value of sem_num is less than 0 or greater than or equal to the number of semaphores in the set.
- EIDRM
- The semaphore set was removed.
- EINTR
- While blocked in this system call, the process caught a signal.
- EINVAL
- The semaphore set doesn't exist, or semid is less than zero, or nsops has a non-positive value.
- ENOMEM
- The sem_flg of some operation asserted SEM_UNDO and the system does not have enough memory to allocate the undo structure.
- ERANGE
- For some operation sem_op+semval is greater than SEMVMX, the implementation dependent maximum value for semval.
SEE ALSO
ipc(5), semctl(2), semget(2), sigaction(2)
Important: Use the man command (% man) to see how a command is used on your particular computer.

