1SETPGID(3P)                POSIX Programmer's Manual               SETPGID(3P)
2
3
4

PROLOG

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

NAME

12       setpgid — set process group ID for job control
13

SYNOPSIS

15       #include <unistd.h>
16
17       int setpgid(pid_t pid, pid_t pgid);
18

DESCRIPTION

20       The setpgid() function shall either join an existing process  group  or
21       create a new process group within the session of the calling process.
22
23       The process group ID of a session leader shall not change.
24
25       Upon  successful completion, the process group ID of the process with a
26       process ID that matches pid shall be set to pgid.
27
28       As a special case, if pid is 0, the process ID of the  calling  process
29       shall  be  used.  Also,  if  pgid is 0, the process ID of the indicated
30       process shall be used.
31

RETURN VALUE

33       Upon successful completion, setpgid() shall  return  0;  otherwise,  -1
34       shall be returned and errno shall be set to indicate the error.
35

ERRORS

37       The setpgid() function shall fail if:
38
39       EACCES The  value of the pid argument matches the process ID of a child
40              process of the calling process and the child  process  has  suc‐
41              cessfully executed one of the exec functions.
42
43       EINVAL The value of the pgid argument is less than 0, or is not a value
44              supported by the implementation.
45
46       EPERM  The process indicated by the pid argument is a session leader.
47
48       EPERM  The value of the pid argument matches the process ID of a  child
49              process  of  the calling process and the child process is not in
50              the same session as the calling process.
51
52       EPERM  The value of the pgid argument is valid but does not  match  the
53              process  ID  of  the  process  indicated by the pid argument and
54              there is no process with a process group  ID  that  matches  the
55              value  of  the  pgid argument in the same session as the calling
56              process.
57
58       ESRCH  The value of the pid argument does not match the process  ID  of
59              the  calling  process  or  of  a  child  process  of the calling
60              process.
61
62       The following sections are informative.
63

EXAMPLES

65       None.
66

APPLICATION USAGE

68       None.
69

RATIONALE

71       The setpgid() function shall group processes together for  the  purpose
72       of signaling, placement in foreground or background, and other job con‐
73       trol actions.
74
75       The setpgid() function is similar to the setpgrp() function of 4.2 BSD,
76       except  that  4.2 BSD allowed the specified new process group to assume
77       any value. This presents certain security problems and is more flexible
78       than necessary to support job control.
79
80       To  provide tighter security, setpgid() only allows the calling process
81       to join a process group already in use inside its session or  create  a
82       new process group whose process group ID was equal to its process ID.
83
84       When  a  job  control  shell spawns a new job, the processes in the job
85       must be placed into a new process group via setpgid().  There  are  two
86       timing constraints involved in this action:
87
88        1. The  new process must be placed in the new process group before the
89           appropriate program is launched via one of the exec functions.
90
91        2. The new process must be placed in the new process group before  the
92           shell can correctly send signals to the new process group.
93
94       To  address these constraints, the following actions are performed. The
95       new processes call setpgid() to alter their own  process  groups  after
96       fork() but before exec.  This satisfies the first constraint. Under 4.3
97       BSD, the second constraint is satisfied by the synchronization property
98       of  vfork();  that  is, the shell is suspended until the child has com‐
99       pleted the exec,  thus  ensuring  that  the  child  has  completed  the
100       setpgid().   A  new  version  of  fork() with this same synchronization
101       property was considered, but it was decided instead to merely allow the
102       parent shell process to adjust the process group of its child processes
103       via setpgid().  Both timing constraints are  now  satisfied  by  having
104       both the parent shell and the child attempt to adjust the process group
105       of the child process; it does not matter which succeeds first.
106
107       Since it would be confusing to an application to have its process group
108       change  after it began executing (that is, after exec), and because the
109       child process would already have  adjusted  its  process  group  before
110       this, the [EACCES] error was added to disallow this.
111
112       One  non-obvious  use  of  setpgid() is to allow a job control shell to
113       return itself to its original process group (the one in effect when the
114       job  control  shell was executed). A job control shell does this before
115       returning control back to its parent when it is terminating or suspend‐
116       ing itself as a way of restoring its job control ``state'' back to what
117       its parent would expect. (Note that the original process group  of  the
118       job  control  shell  typically matches the process group of its parent,
119       but this is not necessarily always the case.)
120

FUTURE DIRECTIONS

122       None.
123

SEE ALSO

125       exec, getpgrp(), setsid(), tcsetpgrp()
126
127       The Base Definitions volume of POSIX.1‐2017, <sys_types.h>, <unistd.h>
128
130       Portions of this text are reprinted and reproduced in  electronic  form
131       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
132       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
133       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
134       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
135       event of any discrepancy between this version and the original IEEE and
136       The Open Group Standard, the original IEEE and The Open Group  Standard
137       is  the  referee document. The original Standard can be obtained online
138       at http://www.opengroup.org/unix/online.html .
139
140       Any typographical or formatting errors that appear  in  this  page  are
141       most likely to have been introduced during the conversion of the source
142       files to man page format. To report such errors,  see  https://www.ker
143       nel.org/doc/man-pages/reporting_bugs.html .
144
145
146
147IEEE/The Open Group                  2017                          SETPGID(3P)
Impressum