1SETPGID(3P) POSIX Programmer's Manual SETPGID(3P)
2
3
4
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
11
13 setpgid — set process group ID for job control
14
16 #include <unistd.h>
17
18 int setpgid(pid_t pid, pid_t pgid);
19
21 The setpgid() function shall either join an existing process group or
22 create a new process group within the session of the calling process.
23
24 The process group ID of a session leader shall not change.
25
26 Upon successful completion, the process group ID of the process with a
27 process ID that matches pid shall be set to pgid.
28
29 As a special case, if pid is 0, the process ID of the calling process
30 shall be used. Also, if pgid is 0, the process ID of the indicated
31 process shall be used.
32
34 Upon successful completion, setpgid() shall return 0; otherwise, −1
35 shall be returned and errno shall be set to indicate the error.
36
38 The setpgid() function shall fail if:
39
40 EACCES The value of the pid argument matches the process ID of a child
41 process of the calling process and the child process has suc‐
42 cessfully executed one of the exec functions.
43
44 EINVAL The value of the pgid argument is less than 0, or is not a value
45 supported by the implementation.
46
47 EPERM The process indicated by the pid argument is a session leader.
48
49 EPERM The value of the pid argument matches the process ID of a child
50 process of the calling process and the child process is not in
51 the same session as the calling process.
52
53 EPERM The value of the pgid argument is valid but does not match the
54 process ID of the process indicated by the pid argument and
55 there is no process with a process group ID that matches the
56 value of the pgid argument in the same session as the calling
57 process.
58
59 ESRCH The value of the pid argument does not match the process ID of
60 the calling process or of a child process of the calling
61 process.
62
63 The following sections are informative.
64
66 None.
67
69 None.
70
72 The setpgid() function shall group processes together for the purpose
73 of signaling, placement in foreground or background, and other job con‐
74 trol actions.
75
76 The setpgid() function is similar to the setpgrp() function of 4.2 BSD,
77 except that 4.2 BSD allowed the specified new process group to assume
78 any value. This presents certain security problems and is more flexible
79 than necessary to support job control.
80
81 To provide tighter security, setpgid() only allows the calling process
82 to join a process group already in use inside its session or create a
83 new process group whose process group ID was equal to its process ID.
84
85 When a job control shell spawns a new job, the processes in the job
86 must be placed into a new process group via setpgid(). There are two
87 timing constraints involved in this action:
88
89 1. The new process must be placed in the new process group before the
90 appropriate program is launched via one of the exec functions.
91
92 2. The new process must be placed in the new process group before the
93 shell can correctly send signals to the new process group.
94
95 To address these constraints, the following actions are performed. The
96 new processes call setpgid() to alter their own process groups after
97 fork() but before exec. This satisfies the first constraint. Under 4.3
98 BSD, the second constraint is satisfied by the synchronization property
99 of vfork(); that is, the shell is suspended until the child has com‐
100 pleted the exec, thus ensuring that the child has completed the
101 setpgid(). A new version of fork() with this same synchronization
102 property was considered, but it was decided instead to merely allow the
103 parent shell process to adjust the process group of its child processes
104 via setpgid(). Both timing constraints are now satisfied by having
105 both the parent shell and the child attempt to adjust the process group
106 of the child process; it does not matter which succeeds first.
107
108 Since it would be confusing to an application to have its process group
109 change after it began executing (that is, after exec), and because the
110 child process would already have adjusted its process group before
111 this, the [EACCES] error was added to disallow this.
112
113 One non-obvious use of setpgid() is to allow a job control shell to
114 return itself to its original process group (the one in effect when the
115 job control shell was executed). A job control shell does this before
116 returning control back to its parent when it is terminating or suspend‐
117 ing itself as a way of restoring its job control ``state'' back to what
118 its parent would expect. (Note that the original process group of the
119 job control shell typically matches the process group of its parent,
120 but this is not necessarily always the case.)
121
123 None.
124
126 exec, getpgrp(), setsid(), tcsetpgrp()
127
128 The Base Definitions volume of POSIX.1‐2008, <sys_types.h>, <unistd.h>
129
131 Portions of this text are reprinted and reproduced in electronic form
132 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
133 -- Portable Operating System Interface (POSIX), The Open Group Base
134 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
135 cal and Electronics Engineers, Inc and The Open Group. (This is
136 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
137 event of any discrepancy between this version and the original IEEE and
138 The Open Group Standard, the original IEEE and The Open Group Standard
139 is the referee document. The original Standard can be obtained online
140 at http://www.unix.org/online.html .
141
142 Any typographical or formatting errors that appear in this page are
143 most likely to have been introduced during the conversion of the source
144 files to man page format. To report such errors, see https://www.ker‐
145 nel.org/doc/man-pages/reporting_bugs.html .
146
147
148
149IEEE/The Open Group 2013 SETPGID(3P)