SEMGET(3)                  POSIX Programmer's Manual                 SEMGET(3)



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


NNAAMMEE
       semget -- get set of XSI semaphores

SSYYNNOOPPSSIISS
       #include <sys/sem.h>

       int semget(key_t _k_e_y, int _n_s_e_m_s, int _s_e_m_f_l_g);

DDEESSCCRRIIPPTTIIOONN
       The _s_e_m_g_e_t() function operates on XSI semaphores (see the Base
       Definitions volume of POSIX.1-2008, _S_e_c_t_i_o_n _4_._1_6, _S_e_m_a_p_h_o_r_e).  It is
       unspecified whether this function interoperates with the realtime
       interprocess communication facilities defined in _S_e_c_t_i_o_n _2_._8, _R_e_a_l_t_i_m_e.

       The _s_e_m_g_e_t() function shall return the semaphore identifier associated
       with _k_e_y.

       A semaphore identifier with its associated sseemmiidd__ddss data structure and
       its associated set of _n_s_e_m_s semaphores (see _<_s_y_s_/_s_e_m_._h_>) is created for
       _k_e_y if one of the following is true:

        *  The argument _k_e_y is equal to IPC_PRIVATE.

        *  The argument _k_e_y does not already have a semaphore identifier
           associated with it and (_s_e_m_f_l_g &IPC_CREAT) is non-zero.

       Upon creation, the sseemmiidd__ddss data structure associated with the new
       semaphore identifier is initialized as follows:

        *  In the operation permissions structure _s_e_m___p_e_r_m_._c_u_i_d, _s_e_m___p_e_r_m_._u_i_d,
           _s_e_m___p_e_r_m_._c_g_i_d, and _s_e_m___p_e_r_m_._g_i_d shall be set to the effective user
           ID and effective group ID, respectively, of the calling process.

        *  The low-order 9 bits of _s_e_m___p_e_r_m_._m_o_d_e shall be set to the low-order
           9 bits of _s_e_m_f_l_g.

        *  The variable _s_e_m___n_s_e_m_s shall be set to the value of _n_s_e_m_s.

        *  The variable _s_e_m___o_t_i_m_e shall be set to 0 and _s_e_m___c_t_i_m_e shall be set
           to the current time, as described in _S_e_c_t_i_o_n _2_._7_._1, _I_P_C _G_e_n_e_r_a_l
           _D_e_s_c_r_i_p_t_i_o_n.

        *  The data structure associated with each semaphore in the set need
           not be initialized. The _s_e_m_c_t_l() function with the command SETVAL
           or SETALL can be used to initialize each semaphore.

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

EERRRROORRSS
       The _s_e_m_g_e_t() function shall fail if:

       EEAACCCCEESS A semaphore identifier exists for _k_e_y, but operation permission
              as specified by the low-order 9 bits of _s_e_m_f_l_g would not be
              granted; see _S_e_c_t_i_o_n _2_._7, _X_S_I _I_n_t_e_r_p_r_o_c_e_s_s _C_o_m_m_u_n_i_c_a_t_i_o_n.

       EEEEXXIISSTT A semaphore identifier exists for the argument _k_e_y but ((_s_e_m_f_l_g
              &IPC_CREAT) &&(_s_e_m_f_l_g &IPC_EXCL)) is non-zero.

       EEIINNVVAALL The value of _n_s_e_m_s is either less than or equal to 0 or greater
              than the system-imposed limit, or a semaphore identifier exists
              for the argument _k_e_y, but the number of semaphores in the set
              associated with it is less than _n_s_e_m_s and _n_s_e_m_s is not equal to
              0.

       EENNOOEENNTT A semaphore identifier does not exist for the argument _k_e_y and
              (_s_e_m_f_l_g &IPC_CREAT) is equal to 0.

       EENNOOSSPPCC A semaphore identifier is to be created but the system-imposed
              limit on the maximum number of allowed semaphores system-wide
              would be exceeded.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       Refer to _s_e_m_o_p().

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       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 _S_e_c_t_i_o_n _2_._7, _X_S_I _I_n_t_e_r_p_r_o_c_e_s_s _C_o_m_m_u_n_i_c_a_t_i_o_n can be easily
       modified to use the alternative interfaces.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       A future version may require that the value of the _s_e_m_v_a_l, _s_e_m_p_i_d,
       _s_e_m_n_c_n_t, and _s_e_m_z_c_n_t members of all semaphores in a semaphore set be
       initialized to zero when a call to _s_e_m_g_e_t() creates a semaphore set.
       Many semaphore implementations already do this and it greatly
       simplifies what an application must do to initialize a semaphore set.

SSEEEE AALLSSOO
       _S_e_c_t_i_o_n _2_._7, _X_S_I _I_n_t_e_r_p_r_o_c_e_s_s _C_o_m_m_u_n_i_c_a_t_i_o_n, _S_e_c_t_i_o_n _2_._8, _R_e_a_l_t_i_m_e,
       _f_t_o_k(), _s_e_m_c_t_l(), _s_e_m_o_p(), _s_e_m___c_l_o_s_e(), _s_e_m___d_e_s_t_r_o_y(), _s_e_m___g_e_t_v_a_l_u_e(),
       _s_e_m___i_n_i_t(), _s_e_m___o_p_e_n(), _s_e_m___p_o_s_t(), _s_e_m___t_r_y_w_a_i_t(), _s_e_m___u_n_l_i_n_k()

       The Base Definitions volume of POSIX.1-2008, _S_e_c_t_i_o_n _4_._1_6, _S_e_m_a_p_h_o_r_e,
       <<ssyyss__sseemm..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                            SEMGET(3)
