Seth Woolley's Man Viewer

semop(2) - semop, semtimedop, semop, semtimedop - semaphore operations - man 2 semop

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

SEMOP(2)                   Linux Programmer's Manual                  SEMOP(2)



NAME
       semop, semtimedop - semaphore operations

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(1,3x,3x cbreak));

DESCRIPTION
       A semaphore is represented by an anonymous structure including the fol-
       lowing 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 sema-
       phore set(7,n,1 builtins) indicated by semid.  Each of the nsops elements in(1,8) the  array
       pointed  to  by  sops specifies an operation to be performed on a sema-
       phore 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(1,8) sem_flg are IPC_NOWAIT and SEM_UNDO.  If an  opera-
       tion asserts SEM_UNDO, it will be undone when the process exits.

       The  set(7,n,1 builtins)  of operations contained in(1,8) sops is performed atomically, that
       is, the operations are performed at the same time(1,2,n), and only if(3,n) they can
       all  be  simultaneously performed.  The behaviour of the system call if(3,n)
       not all operations can be performed immediately depends on the presence
       of  the  IPC_NOWAIT  flag  in(1,8)  the  individual sem_flg fields, as noted
       below.

       Each operation is performed on the sem_num-th semaphore  of  the  sema-
       phore  set(7,n,1 builtins), where the first semaphore of the set(7,n,1 builtins) 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(3,n) 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(2,n,1 builtins) access(2,5) permission on the
       semaphore set.  This is a "wait-for-zero" operation: if(3,n) semval is zero,
       the  operation  can  immediately  proceed.  Otherwise, if(3,n) IPC_NOWAIT is
       asserted in(1,8) sem_flg, the system call fails with  errno  set(7,n,1 builtins)  to  EAGAIN
       (and  none  of the operations in(1,8) 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(1,2,n) the value of semzcnt  is  decre-
              mented.

             The  semaphore set(7,n,1 builtins) is removed: the system call fails, with errno
              set(7,n,1 builtins) to EIDRM.

             The calling process catches a signal: the value  of  semzcnt  is
              decremented  and the system call fails, with errno set(7,n,1 builtins) to EINTR.

             The time(1,2,n)  limit  specified  by  timeout(1,3x,3x cbreak)  in(1,8)  a  semtimedop  call
              expires: the system call fails, with errno set(7,n,1 builtins) to EAGAIN.

       If  sem_op is less(1,3) 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(3,n) 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(1,8) sem_flg, the system call fails,
       with errno set(7,n,1 builtins) to EAGAIN (and none of the operations in(1,8)  sops  is  per-
       formed).   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(1,2,n) the value of semncnt is  decremented,  the
              absolute  value  of  sem_op  is  subtracted  from semval and, if(3,n)
              SEM_UNDO is asserted for this operation, the system updates  the
              process undo count (semadj) for this semaphore.

             The  semaphore  set(7,n,1 builtins)  is removed from the system: the system call
              fails with errno set(7,n,1 builtins) to EIDRM.

             The calling process catches a signal: the value  of  semncnt  is
              decremented and the system call fails with errno set(7,n,1 builtins) to EINTR.

             The  time(1,2,n)  limit  specified  by  timeout(1,3x,3x cbreak)  in(1,8)  a  semtimedop call
              expires: the system call fails, with errno set(7,n,1 builtins) to EAGAIN.

       On successful completion, the sempid value for each semaphore specified
       in(1,8) the array pointed to by sops is set(7,n,1 builtins) to the process ID of the calling
       process.  In addition, the sem_otime is set(7,n,1 builtins) to the current time.

       The function semtimedop  behaves  identically  to  the  function  semop
       except  that  in(1,8)  those cases were the calling process would sleep(1,3), the
       duration of that sleep(1,3) is limited by the amount of elapsed time(1,2,n)  speci-
       fied  by  the timespec structure whose address is passed in(1,8) the timeout(1,3x,3x cbreak)
       parameter.  If the specified time(1,2,n) limit has been  reached,  the  system
       call fails with errno set(7,n,1 builtins) to EAGAIN (and none of the operations in(1,8) sops
       is performed).  If the  timeout(1,3x,3x cbreak)  parameter  is  NULL,  then  semtimedop
       behaves exactly like semop.

RETURN VALUE
       If  successful  the system call returns 0, otherwise it returns -1 with
       errno indicating the error.

ERRORS
       On failure, errno is set(7,n,1 builtins) 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  does not have the permissions required to
              perform the specified semaphore operations, and  does  not  have
              the CAP_IPC_OWNER capability.

       EAGAIN An operation could not proceed immediately and either IPC_NOWAIT
              was asserted in(1,8) its sem_flg or the time(1,2,n) limit specified in(1,8) time-
              out expired.

       EFAULT An  address  specified  in(1,8) either the sops or timeout(1,3x,3x cbreak) parameters
              isn't accessible.

       EFBIG  For some operation the value  of  sem_num  is  less(1,3)  than  0  or
              greater than or equal to the number of semaphores in(1,8) the set.

       EIDRM  The semaphore set(7,n,1 builtins) was removed.

       EINTR  While  blocked in(1,8) this system call, the process caught a signal.

       EINVAL The semaphore set(7,n,1 builtins) doesn't exist, or semid is less(1,3) 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.

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

       semop is never automatically restarted after  being  interrupted  by  a
       signal(2,7)  handler, regardless of the setting of the SA_RESTART flags when
       establishing a signal(2,7) 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(7,n,1 builtins) using the SETVAL or SETALL  request
       to  semctl(2),  the  corresponding  semadj  values in(1,8) all processes are
       cleared.

       The semval, sempid, semzcnt, and semnct values for a semaphore can  all
       be retrieved using appropriate semctl(2) calls.

       The  followings are limits on semaphore set(7,n,1 builtins) resources affecting a semop
       call:

       SEMOPM Maximum number of operations allowed for one semop call (32).

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

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

BUGS
       When a process terminates, its set(7,n,1 builtins) 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(3,n) one (or more)  of
       these  semaphore  adjustments  would result in(1,8) 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  alto-
       gether  (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.

CONFORMING TO
       SVr4, SVID.  SVr4 documents additional error(8,n) conditions EINVAL,  EFBIG,
       ENOSPC.

SEE ALSO
       semctl(2), semget(2), sigaction(2), ipc(2,5)(5), capabilities(7)



Linux 2.4                         2003-04-28                          SEMOP(2)

References for this manual (incoming links)