SETPGID(3)                 POSIX Programmer's Manual                SETPGID(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
       setpgid -- set process group ID for job control

SSYYNNOOPPSSIISS
       #include <unistd.h>

       int setpgid(pid_t _p_i_d, pid_t _p_g_i_d);

DDEESSCCRRIIPPTTIIOONN
       The _s_e_t_p_g_i_d() function shall either join an existing process group or
       create a new process group within the session of the calling process.

       The process group ID of a session leader shall not change.

       Upon successful completion, the process group ID of the process with a
       process ID that matches _p_i_d shall be set to _p_g_i_d.

       As a special case, if _p_i_d is 0, the process ID of the calling process
       shall be used. Also, if _p_g_i_d is 0, the process ID of the indicated
       process shall be used.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, _s_e_t_p_g_i_d() shall return 0; otherwise, -1
       shall be returned and _e_r_r_n_o shall be set to indicate the error.

EERRRROORRSS
       The _s_e_t_p_g_i_d() function shall fail if:

       EEAACCCCEESS The value of the _p_i_d argument matches the process ID of a child
              process of the calling process and the child process has
              successfully executed one of the _e_x_e_c functions.

       EEIINNVVAALL The value of the _p_g_i_d argument is less than 0, or is not a value
              supported by the implementation.

       EEPPEERRMM  The process indicated by the _p_i_d argument is a session leader.

       EEPPEERRMM  The value of the _p_i_d argument matches the process ID of a child
              process of the calling process and the child process is not in
              the same session as the calling process.

       EEPPEERRMM  The value of the _p_g_i_d argument is valid but does not match the
              process ID of the process indicated by the _p_i_d argument and
              there is no process with a process group ID that matches the
              value of the _p_g_i_d argument in the same session as the calling
              process.

       EESSRRCCHH  The value of the _p_i_d argument does not match the process ID of
              the calling process or of a child process of the calling
              process.

       _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
       The _s_e_t_p_g_i_d() function shall group processes together for the purpose
       of signaling, placement in foreground or background, and other job
       control actions.

       The _s_e_t_p_g_i_d() function is similar to the _s_e_t_p_g_r_p() function of 4.2 BSD,
       except that 4.2 BSD allowed the specified new process group to assume
       any value. This presents certain security problems and is more flexible
       than necessary to support job control.

       To provide tighter security, _s_e_t_p_g_i_d() only allows the calling process
       to join a process group already in use inside its session or create a
       new process group whose process group ID was equal to its process ID.

       When a job control shell spawns a new job, the processes in the job
       must be placed into a new process group via _s_e_t_p_g_i_d().  There are two
       timing constraints involved in this action:

        1. The new process must be placed in the new process group before the
           appropriate program is launched via one of the _e_x_e_c functions.

        2. The new process must be placed in the new process group before the
           shell can correctly send signals to the new process group.

       To address these constraints, the following actions are performed. The
       new processes call _s_e_t_p_g_i_d() to alter their own process groups after
       _f_o_r_k() but before _e_x_e_c.  This satisfies the first constraint. Under 4.3
       BSD, the second constraint is satisfied by the synchronization property
       of _v_f_o_r_k(); that is, the shell is suspended until the child has
       completed the _e_x_e_c, thus ensuring that the child has completed the
       _s_e_t_p_g_i_d().  A new version of _f_o_r_k() with this same synchronization
       property was considered, but it was decided instead to merely allow the
       parent shell process to adjust the process group of its child processes
       via _s_e_t_p_g_i_d().  Both timing constraints are now satisfied by having
       both the parent shell and the child attempt to adjust the process group
       of the child process; it does not matter which succeeds first.

       Since it would be confusing to an application to have its process group
       change after it began executing (that is, after _e_x_e_c), and because the
       child process would already have adjusted its process group before
       this, the [[EEAACCCCEESS]] error was added to disallow this.

       One non-obvious use of _s_e_t_p_g_i_d() is to allow a job control shell to
       return itself to its original process group (the one in effect when the
       job control shell was executed). A job control shell does this before
       returning control back to its parent when it is terminating or
       suspending itself as a way of restoring its job control ``state'' back
       to what its parent would expect. (Note that the original process group
       of the job control shell typically matches the process group of its
       parent, but this is not necessarily always the case.)

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _e_x_e_c, _g_e_t_p_g_r_p(), _s_e_t_s_i_d(), _t_c_s_e_t_p_g_r_p()

       The Base Definitions volume of POSIX.1-2017, <<ssyyss__ttyyppeess..hh>>, <<uunniissttdd..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                           SETPGID(3)
