Home : Computers : Linux : Man Pages : semop

semop


SYNOPSIS

       #include <sys/types.h>
       #include <sys/ipc.h>
       #include <sys/sem.h>

       int semop(int semid, struct sembuf *sops, unsigned nsops);

       int  semtimedop(int  semid, struct sembuf *sops, unsigned nsops, struct
       timespec *timeout);


DESCRIPTION

       Each semaphore in a semaphore set has the following associated values:

           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 */

       semop() performs operations on selected semaphores in the set indicated
       by  semid.   Each of the nsops elements in the array pointed to by sops
       specifies an operation to be performed on a single semaphore.  The ele-
       ments  of this structure are of type struct sembuf, containing the fol-
       lowing 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  opera-
       tion  specifies SEM_UNDO, it will be automatically undone when the pro-
       cess terminates.

       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  numbered  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 specified 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 permission on the
       ?      The calling process catches a signal: the value  of  semzcnt  is
              decremented and semop() fails, with errno set to EINTR.

       ?      The  time  limit  specified  by  timeout  in a semtimedop() call
              expires: semop() fails, with errno set to EAGAIN.

       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 speci-
       fied 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 specified  in  sem_flg,  semop()
       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 pro-
       cess 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 specified for this operation, the system updates the
              process undo count (semadj) for this semaphore.

       ?      The semaphore set is removed from  the  system:  semop()  fails,
              with errno set to EIDRM.

       ?      The  calling  process  catches a signal: the value of semncnt is
              decremented and semop() fails, with errno set to EINTR.

       ?      The time limit specified  by  timeout  in  a  semtimedop()  call
              expires: the system call fails, with errno set to EAGAIN.

       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.

       semtimedop()  behaves identically to semop() except that in those cases
       were the calling process would sleep, the duration  of  that  sleep  is
       limited  by the amount of elapsed time specified by the timespec struc-
       ture whose address is passed in the timeout parameter.  If  the  speci-
       fied  time limit has been reached, semtimedop() fails with errno set to
       EAGAIN (and none of the operations in sops is performed).  If the time-
       out  parameter is NULL, then semtimedop() behaves exactly like semop().


RETURN VALUE

       If successful semop() and semtimedop() return 0; otherwise they  return
       -1 with errno indicating the error.


ERRORS

       On failure, errno is set to one of the following:

       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 specified 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.


NOTES

       The sem_undo structures of a process aren't inherited across a  fork(2)
       system call, but they are inherited across an execve(2) system call.

       semop()  is  never automatically restarted after being interrupted by a
       signal handler, regardless of the setting of the SA_RESTART  flag  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(2), 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(2) calls.

       The following limits on semaphore  set  resources  affect  the  semop()
       call:

       SEMOPM Maximum  number  of operations allowed for one semop() call (32)
              (on Linux, this limit can be read and  modified  via  the  third
              field of /proc/sys/kernel/sem).

       SEMVMX Maximum  allowable  value  for  semval: implementation dependent
              (32767).

       The implementation has no intrinsic limits for the adjust on exit maxi-
       mum  value  (SEMAEM), the system wide maximum number of undo structures
       (SEMMNU) and the per-process maximum  number  of  undo  entries  system
       parameters.

       semtimedop() first appeared in Linux 2.5.52, and was subsequently back-
       ported into kernel 2.4.22.


BUGS

       In kernels 2.6.x, x <= 10, 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.


CONFORMING TO

       SVr4, POSIX.1-2001.


SEE ALSO

       semctl(2),  semget(2),  sigaction(2), capabilities(7), sem_overview(7),
       svipc(7)


Linux 2.6.9 2004-11-10 SEMOP(2)



Subscribe to us on YouTube