Seth Woolley's Man Viewer

semget(2) - semget, semget - get a semaphore set identifier - man 2 semget

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

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



NAME
       semget - get a semaphore set(7,n,1 builtins) identifier

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

       int semget(key_t key, int nsems, int semflg);

DESCRIPTION
       This  function returns the semaphore set(7,n,1 builtins) identifier associated with the
       argument key.  A new set(7,n,1 builtins) of nsems semaphores is created if(3,n) key has  the
       value  IPC_PRIVATE or if(3,n) no existing semaphore set(7,n,1 builtins) is associated to key
       and IPC_CREAT is asserted in(1,8) semflg (i.e.   semflg  &  IPC_CREAT  isn't
       zero).

       The  presence  in(1,8) semflg of the fields IPC_CREAT and IPC_EXCL plays the
       same role, with respect to the existence of the semaphore set(7,n,1 builtins),  as  the
       presence of O_CREAT and O_EXCL in(1,8) the mode argument of the open(2,3,n)(2) sys-
       tem call: i.e.  the  semget  function  fails  if(3,n)  semflg  asserts  both
       IPC_CREAT and IPC_EXCL and a semaphore set(7,n,1 builtins) already exists for key.

       Upon  creation,  the low-order 9 bits of the argument semflg define the
       access(2,5) permissions (for owner, group and others) for the semaphore set.
       These  bits  have  the  same  format, and the same meaning, as the mode
       argument in(1,8) the open(2,3,n)(2) or creat(2) system calls  (though  the  execute
       permissions  are  not  meaningful for semaphores, and write(1,2) permissions
       mean permission to alter semaphore values).

       When creating a new semaphore set(7,n,1 builtins),  semget  initializes  the  semaphore
       set(7,n,1 builtins)'s associated data structure semid_ds as follows:

              sem_perm.cuid  and sem_perm.uid are set(7,n,1 builtins) to the effective user-ID
              of the calling process.

              sem_perm.cgid and sem_perm.gid are set(7,n,1 builtins) to the effective group-ID
              of the calling process.

              The low-order 9 bits of sem_perm.mode are set(7,n,1 builtins) to the low-order 9
              bits of semflg.

              sem_nsems is set(7,n,1 builtins) to the value of nsems.

              sem_otime is set(7,n,1 builtins) to 0.

              sem_ctime is set(7,n,1 builtins) to the current time.

       The argument nsems can be 0 (a don't care) when a semaphore set(7,n,1 builtins) is  not
       being created.  Otherwise nsems must be greater than 0 and less(1,3) than or
       equal to the maximum number of semaphores per semaphore set(7,n,1 builtins) (SEMMSL).

       If the semaphore set(7,n,1 builtins) already exists, the access(2,5) permissions  are  veri-
       fied.

RETURN VALUE
       If successful, the return value will be the semaphore set(7,n,1 builtins) identifier (a
       nonnegative integer), otherwise -1 is returned, with  errno  indicating
       the error.

ERRORS
       On failure errno will be set(7,n,1 builtins) to one of the following:

       EACCES     A semaphore set(7,n,1 builtins) exists for key, but the calling process does
                  not have permission to access(2,5) the set(7,n,1 builtins), and does not have the
                  CAP_IPC_OWNER capability.

       EEXIST     A semaphore set(7,n,1 builtins) exists for key and semflg was asserting both
                  IPC_CREAT and IPC_EXCL.

       EINVAL     nsems is less(1,3) than 0 or greater than the limit on the number
                  of semaphores per semaphore set(7,n,1 builtins) (SEMMSL), or a semaphore set(7,n,1 builtins)
                  corresponding to key already exists,  and  nsems  is  larger
                  than the number of semaphores in(1,8) that set.

       ENOENT     No  semaphore set(7,n,1 builtins) exists for key and semflg wasn't asserting
                  IPC_CREAT.

       ENOMEM     A semaphore set(7,n,1 builtins) has to be created but  the  system  has  not
                  enough memory for the new data structure.

       ENOSPC     A  semaphore  set(7,n,1 builtins) has to be created but the system limit for
                  the maximum number of semaphore sets (SEMMNI), or the system
                  wide   maximum  number  of  semaphores  (SEMMNS),  would  be
                  exceeded.

NOTES
       IPC_PRIVATE isn't a flag field but a key_t type.  If this special value
       is used for key, the system call ignores everything but the low-order 9
       bits of semflg and creates a new semaphore set(7,n,1 builtins) (on success).

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

       SEMMNI     System  wide maximum number of semaphore sets: policy depen-
                  dent.

       SEMMSL     Maximum  number  of  semaphores  per  semid:  implementation
                  dependent (500 currently).

       SEMMNS     System  wide maximum number of semaphores: policy dependent.
                  Values greater than SEMMSL * SEMMNI makes it irrelevant.

BUGS
       The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more
       clearly show its function.

       The data structure associated with each semaphore in(1,8) the set(7,n,1 builtins) isn't ini-
       tialized by the system call.  In order to initialize those data  struc-
       tures,  one  has to execute a subsequent call to semctl(2) to perform a
       SETVAL or a SETALL command on the semaphore set.

CONFORMING TO
       SVr4, SVID.  SVr4 documents additional error(8,n) conditions  EFBIG,  E2BIG,
       EAGAIN, ERANGE, EFAULT.

SEE ALSO
       semctl(2), semop(2), ftok(3), ipc(2,5)(5), capabilities(7)



Linux 2.6.6                       2004-05-27                         SEMGET(2)

References for this manual (incoming links)