# semget

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           semget — get set of XSI semaphores
    


## SYNOPSIS



           #include <sys/sem.h>
    
           int semget(key_t _key_ , int _nsems_ , int _semflg_);
    


## DESCRIPTION



           The _semget_() function operates on XSI semaphores (see the Base
           Definitions volume of POSIX.1‐2017, _Section 4.17_ , _Semaphore_).  It
           is unspecified whether this function interoperates with the
           realtime interprocess communication facilities defined in _Section_
           _2.8_ , _Realtime_.
    
           The _semget_() function shall return the semaphore identifier
           associated with _key_.
    
           A semaphore identifier with its associated **semid_ds** data structure
           and its associated set of _nsems_ semaphores (see _< sys/sem.h>_) is
           created for _key_ if one of the following is true:
    
            *  The argument _key_ is equal to IPC_PRIVATE.
    
            *  The argument _key_ does not already have a semaphore identifier
               associated with it and (_semflg_ &IPC_CREAT) is non-zero.
    
           Upon creation, the **semid_ds** data structure associated with the new
           semaphore identifier is initialized as follows:
    
            *  In the operation permissions structure _sem_perm.cuid_ ,
               _sem_perm.uid_ , _sem_perm.cgid_ , and _sem_perm.gid_ shall be set to
               the effective user ID and effective group ID, respectively, of
               the calling process.
    
            *  The low-order 9 bits of _sem_perm.mode_ shall be set to the low-
               order 9 bits of _semflg_.
    
            *  The variable _sem_nsems_ shall be set to the value of _nsems_.
    
            *  The variable _sem_otime_ shall be set to 0 and _sem_ctime_ shall
               be set to the current time, as described in _Section 2.7.1_ , _IPC_
               _General Description_.
    
            *  The data structure associated with each semaphore in the set
               need not be initialized. The _semctl_() function with the
               command SETVAL or SETALL can be used to initialize each
               semaphore.
    


## RETURN VALUE



           Upon successful completion, _semget_() shall return a non-negative
           integer, namely a semaphore identifier; otherwise, it shall return
           -1 and set _errno_ to indicate the error.
    


## ERRORS



           The _semget_() function shall fail if:
    
           **EACCES** A semaphore identifier exists for _key_ , but operation
                  permission as specified by the low-order 9 bits of _semflg_
                  would not be granted; see _Section 2.7_ , _XSI Interprocess_
                  _Communication_.
    
           **EEXIST** A semaphore identifier exists for the argument _key_ but
                  ((_semflg_ &IPC_CREAT) &&(_semflg_ &IPC_EXCL)) is non-zero.
    
           **EINVAL** The value of _nsems_ is either less than or equal to 0 or
                  greater than the system-imposed limit, or a semaphore
                  identifier exists for the argument _key_ , but the number of
                  semaphores in the set associated with it is less than _nsems_
                  and _nsems_ is not equal to 0.
    
           **ENOENT** A semaphore identifier does not exist for the argument _key_
                  and (_semflg_ &IPC_CREAT) is equal to 0.
    
           **ENOSPC** A semaphore identifier is to be created but the system-
                  imposed limit on the maximum number of allowed semaphores
                  system-wide would be exceeded.
    
           _The following sections are informative._
    


## EXAMPLES



           Refer to semop(3p).
    


## APPLICATION USAGE



           The POSIX Realtime Extension defines alternative interfaces for
           interprocess communication. Application developers who need to use
           IPC should design their applications so that modules using the IPC
           routines described in _Section 2.7_ , _XSI Interprocess Communication_
           can be easily modified to use the alternative interfaces.
    


## RATIONALE



           None.
    


## FUTURE DIRECTIONS



           A future version may require that the value of the _semval_ , _sempid_ ,
           _semncnt_ , and _semzcnt_ members of all semaphores in a semaphore set
           be initialized to zero when a call to _semget_() creates a semaphore
           set. Many semaphore implementations already do this and it greatly
           simplifies what an application must do to initialize a semaphore
           set.
    


* * *