SEM_INIT(3)                POSIX Programmer's Manual               SEM_INIT(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
       sem_init -- initialize an unnamed semaphore

SSYYNNOOPPSSIISS
       #include <semaphore.h>

       int sem_init(sem_t *_s_e_m, int _p_s_h_a_r_e_d, unsigned _v_a_l_u_e);

DDEESSCCRRIIPPTTIIOONN
       The _s_e_m___i_n_i_t() function shall initialize the unnamed semaphore referred
       to by _s_e_m.  The value of the initialized semaphore shall be _v_a_l_u_e.
       Following a successful call to _s_e_m___i_n_i_t(), the semaphore may be used in
       subsequent calls to _s_e_m___w_a_i_t(), _s_e_m___t_i_m_e_d_w_a_i_t(), _s_e_m___t_r_y_w_a_i_t(),
       _s_e_m___p_o_s_t(), and _s_e_m___d_e_s_t_r_o_y().  This semaphore shall remain usable
       until the semaphore is destroyed.

       If the _p_s_h_a_r_e_d argument has a non-zero value, then the semaphore is
       shared between processes; in this case, any process that can access the
       semaphore _s_e_m can use _s_e_m for performing _s_e_m___w_a_i_t(), _s_e_m___t_i_m_e_d_w_a_i_t(),
       _s_e_m___t_r_y_w_a_i_t(), _s_e_m___p_o_s_t(), and _s_e_m___d_e_s_t_r_o_y() operations.

       Only _s_e_m itself may be used for performing synchronization. The result
       of referring to copies of _s_e_m in calls to _s_e_m___w_a_i_t(), _s_e_m___t_i_m_e_d_w_a_i_t(),
       _s_e_m___t_r_y_w_a_i_t(), _s_e_m___p_o_s_t(), and _s_e_m___d_e_s_t_r_o_y() is undefined.

       If the _p_s_h_a_r_e_d argument is zero, then the semaphore is shared between
       threads of the process; any thread in this process can use _s_e_m for
       performing _s_e_m___w_a_i_t(), _s_e_m___t_i_m_e_d_w_a_i_t(), _s_e_m___t_r_y_w_a_i_t(), _s_e_m___p_o_s_t(), and
       _s_e_m___d_e_s_t_r_o_y() operations. The use of the semaphore by threads other
       than those created in the same process is undefined.

       Attempting to initialize an already initialized semaphore results in
       undefined behavior.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, the _s_e_m___i_n_i_t() function shall initialize
       the semaphore in _s_e_m and return 0. Otherwise, it shall return -1 and
       set _e_r_r_n_o to indicate the error.

EERRRROORRSS
       The _s_e_m___i_n_i_t() function shall fail if:

       EEIINNVVAALL The _v_a_l_u_e argument exceeds {SEM_VALUE_MAX}.

       EENNOOSSPPCC A resource required to initialize the semaphore has been
              exhausted, or the limit on semaphores ({SEM_NSEMS_MAX}) has been
              reached.

       EEPPEERRMM  The process lacks appropriate privileges to initialize the
              semaphore.

       _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
       None.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       None.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _s_e_m___d_e_s_t_r_o_y(), _s_e_m___p_o_s_t(), _s_e_m___t_i_m_e_d_w_a_i_t(), _s_e_m___t_r_y_w_a_i_t()

       The Base Definitions volume of POSIX.1-2008, <<sseemmaapphhoorree..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                          SEM_INIT(3)
