# shmget

## 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



           shmget — get an XSI shared memory segment
    


## SYNOPSIS



           #include <sys/shm.h>
    
           int shmget(key_t _key_ , size_t _size_ , int _shmflg_);
    


## DESCRIPTION



           The _shmget_() function operates on XSI shared memory (see the Base
           Definitions volume of POSIX.1‐2017, _Section 3.346_ , _Shared Memory_
           _Object_).  It is unspecified whether this function interoperates
           with the realtime interprocess communication facilities defined in
           _Section 2.8_ , _Realtime_.
    
           The _shmget_() function shall return the shared memory identifier
           associated with _key_.
    
           A shared memory identifier, associated data structure, and shared
           memory segment of at least _size_ bytes (see _< sys/shm.h>_) are
           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 shared memory
               identifier associated with it and (_shmflg_ &IPC_CREAT) is non-
               zero.
    
           Upon creation, the data structure associated with the new shared
           memory identifier shall be initialized as follows:
    
            *  The values of _shm_perm.cuid_ , _shm_perm.uid_ , _shm_perm.cgid_ , and
               _shm_perm.gid_ are set to the effective user ID and effective
               group ID, respectively, of the calling process.
    
            *  The low-order nine bits of _shm_perm.mode_ are set to the low-
               order nine bits of _shmflg_.
    
            *  The value of _shm_segsz_ is set to the value of _size_.
    
            *  The values of _shm_lpid_ , _shm_nattch_ , _shm_atime_ , and _shm_dtime_
               are set to 0.
    
            *  The value of _shm_ctime_ is set to the current time, as
               described in _Section 2.7.1_ , _IPC General Description_.
    
           When the shared memory segment is created, it shall be initialized
           with all zero values.
    


## RETURN VALUE



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


## ERRORS



           The _shmget_() function shall fail if:
    
           **EACCES** A shared memory identifier exists for _key_ but operation
                  permission as specified by the low-order nine bits of
                  _shmflg_ would not be granted; see _Section 2.7_ , _XSI_
                  _Interprocess Communication_.
    
           **EEXIST** A shared memory identifier exists for the argument _key_ but
                  (_shmflg_ &IPC_CREAT) &&(_shmflg_ &IPC_EXCL) is non-zero.
    
           **EINVAL** A shared memory segment is to be created and the value of
                  size is less than the system-imposed minimum or greater
                  than the system-imposed maximum.
    
           **EINVAL** No shared memory segment is to be created and a shared
                  memory segment exists for _key_ but the size of the segment
                  associated with it is less than _size_.
    
           **ENOENT** A shared memory identifier does not exist for the argument
                  _key_ and (_shmflg_ &IPC_CREAT) is 0.
    
           **ENOMEM** A shared memory identifier and associated shared memory
                  segment are to be created, but the amount of available
                  physical memory is not sufficient to fill the request.
    
           **ENOSPC** A shared memory identifier is to be created, but the
                  system-imposed limit on the maximum number of allowed
                  shared memory identifiers system-wide would be exceeded.
    
           _The following sections are informative._
    


## EXAMPLES



           None.
    


## 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



           None.
    


* * *