int semop ( int semid, struct sembuf *sops, unsigned nsops )
short sem_num; /* semaphore number: 0 = first */
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 system call semantic assures that the operations will be performed if and only if all of them will succeed. Each operation is performed on the sem_num-th semaphore of the semaphore set - where the first semaphore of the set is semaphore 0 - and is one among the following three.
If sem_op is a positive integer, the operation adds this value to semval. Furthermore, if SEM_UNDO is asserted for this operation, the system updates the process undo count for this semaphore. The operation always goes through, so no process sleeping can happen. The calling process must have alter permissions on the semaphore set.
If sem_op is zero, the process must have read access permissions on the semaphore set. If semval is zero, the operation goes through. Otherwise, if IPC_NOWAIT is asserted in sem_flg, the system call fails (undoing all previous actions performed) with errno set to EAGAIN. Otherwise semzcnt 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 receives a signal that has to be caught:
- 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 permissions on the semaphore set. If semval is greater than or equal to the absolute value of sem_op, the absolute value of sem_op is subtracted by semval. Furthermore, if SEM_UNDO is asserted for this operation, the system updates the process undo count for this semaphore. Then the operation goes through. Otherwise, if IPC_NOWAIT is asserted in sem_flg, the system call fails (undoing all previous actions performed) with errno set to EAGAIN. Otherwise semncnt is incremented by one and the process sleeps until one of the following occurs:
- semval
- becomes greater 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 for this semaphore.
- The semaphore set is removed from the system: the system call fails with
- errno set to EIDRM.
- The calling process receives a signal that has to be caught:
- the value of semncnt is decremented and the system call fails with errno set to EINTR.
In case of success, the sempid member of the structure sem for each semaphore specified in the array pointed to by sops is set to the process-ID of the calling process. Furthermore both sem_otime and sem_ctime are set to the current time.
The followings are limits on semaphore set resources affecting a semop call:
The implementation has no intrinsic limits for the adjust on exit maximum value (SEMAEM), the system wide maximum number of undo structures (SEMMNU) and the per process maximum number of undo entries system parameters.