SEMOP


This function performs operations on selected members of the semaphore set indicated by ecx.
Each semaphore in a semaphore set has the following associated values:

semval  - Semaphore value [16bit]
semzcnt - Number waiting for zero [16bit]
semncnt - Number waiting for increase [16bit]
sempid  - Process that did last operation
[32bit]

Arguments

ecx Semaphore set ID.
edx Pointer to an array of sembuf structures specifying the operations to be performed on a semaphore set:

struc sembuf
{
sem_num rw 1
sem_op  rw 1
sem_flg rw 1
}

sembuf members:

sem_num

The number of the semaphore you wish to deal with.
sem_op

The operation (positive, negative, or zero) to perform on each semaphore in the specified set:

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 -EAGAIN (and none of the operations 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 -EIDRM.
  • The calling process catches a signal: the value of semzcnt is decremented and the system call fails, with -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 -EAGAIN (and none of the operations 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 -EIDRM.
  • The calling process catches a signal: the value of semncnt is decremented and the system call fails with -EINTR.

On successful completion, the sempid value for each semaphore specified in the array pointed to by edx is set to the process ID of the calling process. In addition, the sem_otime is set to the current time.

sem_flg
Operational flags:

IPC_NOWAIT - Return error on wait.
SEM_UNDO   - Undo the operation on exit.
esi Number of operations in the array specified by esi.

Return values

If the function succeeds the return value is 0.
If the function fails the return value is one of the following errno values:

-E2BIG The argument in esi 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 edx 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 semaphore set ID is less than zero, or esi 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.

Remarks

SEMOP is never automatically restarted after being interrupted by a signal handler, regardless of the setting of the SA_RESTART flags when establishing a signal handler.

semadj is a per-process integer which is simply the (negative) count of all semaphore operations performed specifying the SEM_UNDO flag. When a semaphore's value is directly set using the SETVAL or SETALL request to SEMCTL, the corresponding semadj values in all processes are cleared.

The semval, sempid, semzcnt, and semnct values for a semaphore can all be retrieved using appropriate SEMCTL calls.

The followings are limits on semaphore set resources affecting a SEMOP call:

  • SEMOPM - Maximum number of operations allowed for one SEMOP call (32).
  • SEMVMX - Maximum allowable value for semval ( 32767).

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.

When a process terminates, its set of associated semadj structures is used to undo the effect of all of the semaphore operations it performed with the SEM_UNDO flag. This raises a difficulty: if one (or more) of these semaphore adjustments would result in an attempt to decrease a semaphore's value below zero, what should an implementation do? One possible approach would be to block until all the semaphore adjustments could be performed. This is however undesirable since it could force process termination to block for arbitrarily long periods. Another possibility is that such semaphore adjustments could be ignored altogether (somewhat analogously to failing when IPC_NOWAIT is specified for a semaphore operation). Linux adopts a third approach: decreasing the semaphore value as far as possible (i.e., to zero) and allowing process termination to proceed immediately.

In kernels prior to 2.6.11 there is a bug that in some circumstances prevents a process that is waiting for a semaphore value to become zero from being woken up when the value does actually become zero. This bug is fixed in kernel 2.6.11.