SEMCTL(3)                  POSIX Programmer's Manual                 SEMCTL(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
       semctl -- XSI semaphore control operations

SSYYNNOOPPSSIISS
       #include <sys/sem.h>

       int semctl(int _s_e_m_i_d, int _s_e_m_n_u_m, int _c_m_d, ...);

DDEESSCCRRIIPPTTIIOONN
       The _s_e_m_c_t_l() function operates on XSI semaphores (see the Base
       Definitions volume of POSIX.1-2017, _S_e_c_t_i_o_n _4_._1_7, _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_c_t_l() function provides a variety of semaphore control
       operations as specified by _c_m_d.  The fourth argument is optional and
       depends upon the operation requested. If required, it is of type uunniioonn
       sseemmuunn, which the application shall explicitly declare:


           union semun {
               int val;
               struct semid_ds *buf;
               unsigned short  *array;
           } arg;

       Each operation shall be performed atomically.

       The following semaphore control operations as specified by _c_m_d are
       executed with respect to the semaphore specified by _s_e_m_i_d and _s_e_m_n_u_m.
       The level of permission required for each operation is shown with each
       command; 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.  The symbolic
       names for the values of _c_m_d are defined in the _<_s_y_s_/_s_e_m_._h_> header:

       GETVAL      Return the value of _s_e_m_v_a_l; see _<_s_y_s_/_s_e_m_._h_>.  Requires read
                   permission.

       SETVAL      Set the value of _s_e_m_v_a_l to _a_r_g_._v_a_l, where _a_r_g is the value
                   of the fourth argument to _s_e_m_c_t_l().  When this command is
                   successfully executed, the _s_e_m_a_d_j value corresponding to
                   the specified semaphore in all processes is cleared. Also,
                   the _s_e_m___c_t_i_m_e timestamp 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.
                   Requires alter permission; 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.

       GETPID      Return the value of _s_e_m_p_i_d.  Requires read permission.

       GETNCNT     Return the value of _s_e_m_n_c_n_t.  Requires read permission.

       GETZCNT     Return the value of _s_e_m_z_c_n_t.  Requires read permission.

       The following values of _c_m_d operate on each _s_e_m_v_a_l in the set of
       semaphores:

       GETALL      Return the value of _s_e_m_v_a_l for each semaphore in the
                   semaphore set and place into the array pointed to by
                   _a_r_g_._a_r_r_a_y, where _a_r_g is the fourth argument to _s_e_m_c_t_l().
                   Requires read permission.

       SETALL      Set the value of _s_e_m_v_a_l for each semaphore in the semaphore
                   set according to the array pointed to by _a_r_g_._a_r_r_a_y, where
                   _a_r_g is the fourth argument to _s_e_m_c_t_l().  When this command
                   is successfully executed, the _s_e_m_a_d_j values corresponding
                   to each specified semaphore in all processes are cleared.
                   Also, the _s_e_m___c_t_i_m_e timestamp 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.  Requires alter permission.

       The following values of _c_m_d are also available:

       IPC_STAT    Place the current value of each member of the sseemmiidd__ddss data
                   structure associated with _s_e_m_i_d into the structure pointed
                   to by _a_r_g_._b_u_f, where _a_r_g is the fourth argument to
                   _s_e_m_c_t_l().  The contents of this structure are defined in
                   _<_s_y_s_/_s_e_m_._h_>.  Requires read permission.

       IPC_SET     Set the value of the following members of the sseemmiidd__ddss data
                   structure associated with _s_e_m_i_d to the corresponding value
                   found in the structure pointed to by _a_r_g_._b_u_f, where _a_r_g is
                   the fourth argument to _s_e_m_c_t_l():


                       sem_perm.uid
                       sem_perm.gid
                       sem_perm.mode

                   The mode bits specified 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 are copied into the corresponding bits of the
                   _s_e_m___p_e_r_m_._m_o_d_e associated with _s_e_m_i_d.  The stored values of
                   any other bits are unspecified. The _s_e_m___c_t_i_m_e timestamp
                   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.

                   This command can only be executed by a process that has an
                   effective user ID equal to either that of a process with
                   appropriate privileges or to the value of _s_e_m___p_e_r_m_._c_u_i_d or
                   _s_e_m___p_e_r_m_._u_i_d in the sseemmiidd__ddss data structure associated with
                   _s_e_m_i_d.

       IPC_RMID    Remove the semaphore identifier specified by _s_e_m_i_d from the
                   system and destroy the set of semaphores and sseemmiidd__ddss data
                   structure associated with it. This command can only be
                   executed by a process that has an effective user ID equal
                   to either that of a process with appropriate privileges or
                   to the value of _s_e_m___p_e_r_m_._c_u_i_d or _s_e_m___p_e_r_m_._u_i_d in the
                   sseemmiidd__ddss data structure associated with _s_e_m_i_d.

RREETTUURRNN VVAALLUUEE
       If successful, the value returned by _s_e_m_c_t_l() depends on _c_m_d as
       follows:

       GETVAL      The value of _s_e_m_v_a_l.

       GETPID      The value of _s_e_m_p_i_d.

       GETNCNT     The value of _s_e_m_n_c_n_t.

       GETZCNT     The value of _s_e_m_z_c_n_t.

       All others  0.

       Otherwise, _s_e_m_c_t_l() shall return -1 and set _e_r_r_n_o to indicate the
       error.

EERRRROORRSS
       The _s_e_m_c_t_l() function shall fail if:

       EEAACCCCEESS Operation permission is denied to the calling process; 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.

       EEIINNVVAALL The value of _s_e_m_i_d is not a valid semaphore identifier, or the
              value of _s_e_m_n_u_m is less than 0 or greater than or equal to
              _s_e_m___n_s_e_m_s, or the value of _c_m_d is not a valid command.

       EEPPEERRMM  The argument _c_m_d is equal to IPC_RMID or IPC_SET and the
              effective user ID of the calling process is not equal to that of
              a process with appropriate privileges and it is not equal to the
              value of _s_e_m___p_e_r_m_._c_u_i_d or _s_e_m___p_e_r_m_._u_i_d in the data structure
              associated with _s_e_m_i_d.

       EERRAANNGGEE The argument _c_m_d is equal to SETVAL or SETALL and the value to
              which _s_e_m_v_a_l is to be set is greater than the system-imposed
              maximum.

       _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 fourth parameter in the SYNOPSIS section is now specified as ""......""
       in order to avoid a clash with the ISO C standard when referring to the
       union _s_e_m_u_n (as defined in Issue 3) and for backwards-compatibility.

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

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,
       _s_e_m_g_e_t(), _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-2017, _S_e_c_t_i_o_n _4_._1_7, _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-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                            SEMCTL(3)
