SEM_OPEN(3)                POSIX Programmer's Manual               SEM_OPEN(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_open -- initialize and open a named semaphore

SSYYNNOOPPSSIISS
       #include <semaphore.h>

       sem_t *sem_open(const char *_n_a_m_e, int _o_f_l_a_g, ...);

DDEESSCCRRIIPPTTIIOONN
       The _s_e_m___o_p_e_n() function shall establish a connection between a named
       semaphore and a process. Following a call to _s_e_m___o_p_e_n() with semaphore
       name _n_a_m_e, the process may reference the semaphore associated with _n_a_m_e
       using the address returned from the call. This 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___c_l_o_s_e().  The semaphore remains usable by this
       process until the semaphore is closed by a successful call to
       _s_e_m___c_l_o_s_e(), ___e_x_i_t(), or one of the _e_x_e_c functions.

       The _o_f_l_a_g argument controls whether the semaphore is created or merely
       accessed by the call to _s_e_m___o_p_e_n().  The following flag bits may be set
       in _o_f_l_a_g:

       O_CREAT   This flag is used to create a semaphore if it does not
                 already exist.  If O_CREAT is set and the semaphore already
                 exists, then O_CREAT has no effect, except as noted under
                 O_EXCL. Otherwise, _s_e_m___o_p_e_n() creates a named semaphore. The
                 O_CREAT flag requires a third and a fourth argument: _m_o_d_e,
                 which is of type mmooddee__tt, and _v_a_l_u_e, which is of type
                 uunnssiiggnneedd.  The semaphore is created with an initial value of
                 _v_a_l_u_e.  Valid initial values for semaphores are less than or
                 equal to {SEM_VALUE_MAX}.

                 The user ID of the semaphore shall be set to the effective
                 user ID of the process. The group ID of the semaphore shall
                 be set to the effective group ID of the process; however, if
                 the _n_a_m_e argument is visible in the file system, the group ID
                 may be set to the group ID of the containing directory. The
                 permission bits of the semaphore are set to the value of the
                 _m_o_d_e argument except those set in the file mode creation mask
                 of the process. When bits in _m_o_d_e other than the file
                 permission bits are specified, the effect is unspecified.

                 After the semaphore named _n_a_m_e has been created by _s_e_m___o_p_e_n()
                 with the O_CREAT flag, other processes can connect to the
                 semaphore by calling _s_e_m___o_p_e_n() with the same value of _n_a_m_e.

       O_EXCL    If O_EXCL and O_CREAT are set, _s_e_m___o_p_e_n() fails if the
                 semaphore _n_a_m_e exists. The check for the existence of the
                 semaphore and the creation of the semaphore if it does not
                 exist are atomic with respect to other processes executing
                 _s_e_m___o_p_e_n() with O_EXCL and O_CREAT set. If O_EXCL is set and
                 O_CREAT is not set, the effect is undefined.

                 If flags other than O_CREAT and O_EXCL are specified in the
                 _o_f_l_a_g parameter, the effect is unspecified.

       The _n_a_m_e argument points to a string naming a semaphore object. It is
       unspecified whether the name appears in the file system and is visible
       to functions that take pathnames as arguments. The _n_a_m_e argument
       conforms to the construction rules for a pathname, except that the
       interpretation of <slash> characters other than the leading <slash>
       character in _n_a_m_e is implementation-defined, and that the length limits
       for the _n_a_m_e argument are implementation-defined and need not be the
       same as the pathname limits {PATH_MAX} and {NAME_MAX}.  If _n_a_m_e begins
       with the <slash> character, then processes calling _s_e_m___o_p_e_n() with the
       same value of _n_a_m_e shall refer to the same semaphore object, as long as
       that name has not been removed. If _n_a_m_e does not begin with the <slash>
       character, the effect is implementation-defined.

       If a process makes multiple successful calls to _s_e_m___o_p_e_n() with the
       same value for _n_a_m_e, the same semaphore address shall be returned for
       each such successful call, provided that there have been no calls to
       _s_e_m___u_n_l_i_n_k() for this semaphore, and at least one previous successful
       _s_e_m___o_p_e_n() call for this semaphore has not been matched with a
       _s_e_m___c_l_o_s_e() call.

       References to copies of the semaphore produce undefined results.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, the _s_e_m___o_p_e_n() function shall return the
       address of the semaphore. Otherwise, it shall return a value of
       SEM_FAILED and set _e_r_r_n_o to indicate the error. The symbol SEM_FAILED
       is defined in the _<_s_e_m_a_p_h_o_r_e_._h_> header. No successful return from
       _s_e_m___o_p_e_n() shall return the value SEM_FAILED.

EERRRROORRSS
       If any of the following conditions occur, the _s_e_m___o_p_e_n() function shall
       return SEM_FAILED and set _e_r_r_n_o to the corresponding value:

       EEAACCCCEESS The named semaphore exists and the permissions specified by
              _o_f_l_a_g are denied, or the named semaphore does not exist and
              permission to create the named semaphore is denied.

       EEEEXXIISSTT O_CREAT and O_EXCL are set and the named semaphore already
              exists.

       EEIINNTTRR  The _s_e_m___o_p_e_n() operation was interrupted by a signal.

       EEIINNVVAALL The _s_e_m___o_p_e_n() operation is not supported for the given name, or
              O_CREAT was specified in _o_f_l_a_g and _v_a_l_u_e was greater than
              {SEM_VALUE_MAX}.

       EEMMFFIILLEE Too many semaphore descriptors or file descriptors are currently
              in use by this process.

       EENNFFIILLEE Too many semaphores are currently open in the system.

       EENNOOEENNTT O_CREAT is not set and the named semaphore does not exist.

       EENNOOMMEEMM There is insufficient memory for the creation of the new named
              semaphore.

       EENNOOSSPPCC There is insufficient space on a storage device for the creation
              of the new named semaphore.

       If any of the following conditions occur, the _s_e_m___o_p_e_n() function may
       return SEM_FAILED and set _e_r_r_n_o to the corresponding value:

       EENNAAMMEETTOOOOLLOONNGG
              The length of the _n_a_m_e argument exceeds {_POSIX_PATH_MAX} on
              systems that do not support the XSI option or exceeds
              {_XOPEN_PATH_MAX} on XSI systems, or has a pathname component
              that is longer than {_POSIX_NAME_MAX} on systems that do not
              support the XSI option or longer than {_XOPEN_NAME_MAX} on XSI
              systems.

       _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
       Early drafts required an error return value of -1 with the type sseemm__tt **
       for the _s_e_m___o_p_e_n() function, which is not guaranteed to be portable
       across implementations. The revised text provides the symbolic error
       code SEM_FAILED to eliminate the type conflict.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       A future version might require the _s_e_m___o_p_e_n() and _s_e_m___u_n_l_i_n_k()
       functions to have semantics similar to normal file system operations.

SSEEEE AALLSSOO
       _s_e_m_c_t_l(), _s_e_m_g_e_t(), _s_e_m_o_p(), _s_e_m___c_l_o_s_e(), _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(), _s_e_m___u_n_l_i_n_k()

       The Base Definitions volume of POSIX.1-2017, <<sseemmaapphhoorree..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  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.opengroup.org/unix/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                  2017                          SEM_OPEN(3)
