]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | .\" Copyright (c) 1983, 1991 Regents of the University of California. |
0786e308 | 2 | .\" and Copyright (C) 2007, Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da MK |
3 | .\" All rights reserved. |
4 | .\" | |
a9cd9cb7 | 5 | .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB) |
fea681da MK |
6 | .\" Redistribution and use in source and binary forms, with or without |
7 | .\" modification, are permitted provided that the following conditions | |
8 | .\" are met: | |
9 | .\" 1. Redistributions of source code must retain the above copyright | |
10 | .\" notice, this list of conditions and the following disclaimer. | |
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
12 | .\" notice, this list of conditions and the following disclaimer in the | |
13 | .\" documentation and/or other materials provided with the distribution. | |
14 | .\" 3. All advertising materials mentioning features or use of this software | |
15 | .\" must display the following acknowledgement: | |
16 | .\" This product includes software developed by the University of | |
17 | .\" California, Berkeley and its contributors. | |
18 | .\" 4. Neither the name of the University nor the names of its contributors | |
19 | .\" may be used to endorse or promote products derived from this software | |
20 | .\" without specific prior written permission. | |
21 | .\" | |
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | .\" SUCH DAMAGE. | |
8c9302dc | 33 | .\" %%%LICENSE_END |
fea681da MK |
34 | .\" |
35 | .\" @(#)getpgrp.2 6.4 (Berkeley) 3/10/91 | |
36 | .\" | |
37 | .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu> | |
38 | .\" Modified 1995-04-15 by Michael Chastain <mec@shell.portal.com>: | |
39 | .\" Added 'getpgid'. | |
40 | .\" Modified 1996-07-21 by Andries Brouwer <aeb@cwi.nl> | |
41 | .\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com> | |
42 | .\" Modified 1999-09-02 by Michael Haardt <michael@moria.de> | |
c11b1abf | 43 | .\" Modified 2002-01-18 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 44 | .\" Modified 2003-01-20 by Andries Brouwer <aeb@cwi.nl> |
0bbb92a3 MK |
45 | .\" 2007-07-25, mtk, fairly substantial rewrites and rearrangements |
46 | .\" of text. | |
fea681da | 47 | .\" |
4b8c67d9 | 48 | .TH SETPGID 2 2017-09-15 "Linux" "Linux Programmer's Manual" |
fea681da MK |
49 | .SH NAME |
50 | setpgid, getpgid, setpgrp, getpgrp \- set/get process group | |
51 | .SH SYNOPSIS | |
cc94f6aa AF |
52 | .B #include <sys/types.h> |
53 | .br | |
fea681da | 54 | .B #include <unistd.h> |
68e4db0a | 55 | .PP |
fea681da MK |
56 | .BI "int setpgid(pid_t " pid ", pid_t " pgid ); |
57 | .br | |
58 | .BI "pid_t getpgid(pid_t " pid ); | |
68e4db0a | 59 | .PP |
72ef5c5c | 60 | .BR "pid_t getpgrp(void);" " /* POSIX.1 version */" |
fea681da | 61 | .br |
afdfa65b | 62 | .BI "pid_t getpgrp(pid_t " pid ");\fR /* BSD version */" |
68e4db0a | 63 | .PP |
72ef5c5c | 64 | .BR "int setpgrp(void);" " /* System V version */" |
fea681da | 65 | .br |
afdfa65b | 66 | .BI "int setpgrp(pid_t " pid ", pid_t " pgid ");\fR /* BSD version */" |
68e4db0a | 67 | .PP |
0bbb92a3 MK |
68 | .in -4n |
69 | Feature Test Macro Requirements for glibc (see | |
70 | .BR feature_test_macros (7)): | |
71 | .in | |
68e4db0a | 72 | .PP |
cc4615cc | 73 | .ad l |
0bbb92a3 | 74 | .BR getpgid (): |
fc541b7a | 75 | .RS 4 |
cf7fa0a1 MK |
76 | _XOPEN_SOURCE\ >=\ 500 |
77 | .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED | |
3ba63d80 | 78 | .br |
cf7fa0a1 | 79 | || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L |
fc541b7a | 80 | .RE |
68e4db0a | 81 | .PP |
fc541b7a MK |
82 | .BR setpgrp "() (POSIX.1):" |
83 | .nf | |
f8619b6a | 84 | _XOPEN_SOURCE\ >=\ 500 |
cf7fa0a1 | 85 | .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED |
f8619b6a MK |
86 | || /* Since glibc 2.19: */ _DEFAULT_SOURCE |
87 | || /* Glibc versions <= 2.19: */ _SVID_SOURCE | |
fc541b7a | 88 | .fi |
68e4db0a | 89 | .PP |
0bbb92a3 | 90 | .BR setpgrp "()\ (BSD)," |
f8619b6a | 91 | .BR getpgrp "()\ (BSD):" |
fc541b7a | 92 | .nf |
f8619b6a | 93 | [These are available only before glibc 2.19] |
fc541b7a MK |
94 | _BSD_SOURCE && |
95 | !\ (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE || | |
52d1e2b2 | 96 | _GNU_SOURCE || _SVID_SOURCE) |
fc541b7a | 97 | .fi |
02e92562 | 98 | .ad |
fea681da | 99 | .SH DESCRIPTION |
0bbb92a3 MK |
100 | All of these interfaces are available on Linux, |
101 | and are used for getting and setting the | |
102 | process group ID (PGID) of a process. | |
103 | The preferred, POSIX.1-specified ways of doing this are: | |
104 | .BR getpgrp (void), | |
105 | for retrieving the calling process's PGID; and | |
106 | .BR setpgid (), | |
107 | for setting a process's PGID. | |
efeece04 | 108 | .PP |
e511ffb6 | 109 | .BR setpgid () |
0bbb92a3 | 110 | sets the PGID of the process specified by |
fea681da MK |
111 | .I pid |
112 | to | |
113 | .IR pgid . | |
114 | If | |
115 | .I pid | |
17c69289 | 116 | is zero, then the process ID of the calling process is used. |
c13182ef | 117 | If |
fea681da | 118 | .I pgid |
17c69289 | 119 | is zero, then the PGID of the process specified by |
fea681da | 120 | .I pid |
17c69289 | 121 | is made the same as its process ID. |
60a90ecd MK |
122 | If |
123 | .BR setpgid () | |
124 | is used to move a process from one process | |
fea681da | 125 | group to another (as is done by some shells when creating pipelines), |
0bbb92a3 MK |
126 | both process groups must be part of the same session (see |
127 | .BR setsid (2) | |
e0bf9127 | 128 | and |
0bbb92a3 | 129 | .BR credentials (7)). |
c13182ef | 130 | In this case, |
fea681da MK |
131 | the \fIpgid\fP specifies an existing process group to be joined and the |
132 | session ID of that group must match the session ID of the joining process. | |
efeece04 | 133 | .PP |
0bbb92a3 MK |
134 | The POSIX.1 version of |
135 | .BR getpgrp (), | |
136 | which takes no arguments, | |
137 | returns the PGID of the calling process. | |
efeece04 | 138 | .PP |
e511ffb6 | 139 | .BR getpgid () |
0bbb92a3 | 140 | returns the PGID of the process specified by |
fea681da MK |
141 | .IR pid . |
142 | If | |
143 | .I pid | |
a1ffe9f5 | 144 | is zero, the process ID of the calling process is used. |
0bbb92a3 MK |
145 | (Retrieving the PGID of a process other than the caller is rarely |
146 | necessary, and the POSIX.1 | |
147 | .BR getpgrp () | |
148 | is preferred for that task.) | |
efeece04 | 149 | .PP |
efbfd7ec | 150 | The System\ V-style |
aadd7709 MK |
151 | .BR setpgrp (), |
152 | which takes no arguments, is equivalent to | |
ab7b478b | 153 | .IR "setpgid(0,\ 0)" . |
efeece04 | 154 | .PP |
0bbb92a3 MK |
155 | The BSD-specific |
156 | .BR setpgrp () | |
157 | call, which takes arguments | |
158 | .I pid | |
fea681da | 159 | and |
0bbb92a3 | 160 | .IR pgid , |
8e6ee8f3 | 161 | is a wrapper function that calls |
efeece04 | 162 | .PP |
ded44f9b | 163 | setpgid(pid, pgid) |
efeece04 | 164 | .PP |
0bbb92a3 MK |
165 | .\" The true BSD setpgrp() system call differs in allowing the PGID |
166 | .\" to be set to arbitrary values, rather than being restricted to | |
167 | .\" PGIDs in the same session. | |
ded44f9b MK |
168 | Since glibc 2.19, the BSD-specific |
169 | .BR setpgrp () | |
170 | function is no longer exposed by | |
171 | .IR <unistd.h> ; | |
172 | calls should be replaced with the | |
173 | .BR setpgid () | |
174 | call shown above. | |
efeece04 | 175 | .PP |
0bbb92a3 MK |
176 | The BSD-specific |
177 | .BR getpgrp () | |
178 | call, which takes a single | |
179 | .I pid | |
ded44f9b | 180 | argument, is a wrapper function that calls |
efeece04 | 181 | .PP |
ded44f9b | 182 | getpgid(pid) |
efeece04 | 183 | .PP |
ded44f9b MK |
184 | Since glibc 2.19, the BSD-specific |
185 | .BR getpgrp () | |
186 | function is no longer exposed by | |
187 | .IR <unistd.h> ; | |
188 | calls should be replaced with calls to the POSIX.1 | |
189 | .BR getpgrp () | |
190 | which takes no arguments (if the intent is to obtain the caller's PGID), | |
191 | or with the | |
192 | .BR getpgid () | |
193 | call shown above. | |
47297adb | 194 | .SH RETURN VALUE |
fea681da | 195 | On success, |
ae050d9a | 196 | .BR setpgid () |
c13182ef | 197 | and |
ae050d9a | 198 | .BR setpgrp () |
c13182ef MK |
199 | return zero. |
200 | On error, \-1 is returned, and | |
fea681da MK |
201 | .I errno |
202 | is set appropriately. | |
efeece04 | 203 | .PP |
0bbb92a3 MK |
204 | The POSIX.1 |
205 | .BR getpgrp () | |
aadd7709 | 206 | always returns the PGID of the caller. |
efeece04 | 207 | .PP |
0bbb92a3 MK |
208 | .BR getpgid (), |
209 | and the BSD-specific | |
210 | .BR getpgrp () | |
211 | return a process group on success. | |
fea681da MK |
212 | On error, \-1 is returned, and |
213 | .I errno | |
214 | is set appropriately. | |
fea681da MK |
215 | .SH ERRORS |
216 | .TP | |
217 | .B EACCES | |
218 | An attempt was made to change the process group ID | |
219 | of one of the children of the calling process and the child had | |
60a90ecd MK |
220 | already performed an |
221 | .BR execve (2) | |
222 | .RB ( setpgid (), | |
223 | .BR setpgrp ()). | |
fea681da MK |
224 | .TP |
225 | .B EINVAL | |
226 | .I pgid | |
227 | is less than 0 | |
60a90ecd MK |
228 | .RB ( setpgid (), |
229 | .BR setpgrp ()). | |
fea681da MK |
230 | .TP |
231 | .B EPERM | |
c13182ef MK |
232 | An attempt was made to move a process into a process group in a |
233 | different session, or to change the process | |
234 | group ID of one of the children of the calling process and the | |
fea681da MK |
235 | child was in a different session, or to change the process group ID of |
236 | a session leader | |
60a90ecd MK |
237 | .RB ( setpgid (), |
238 | .BR setpgrp ()). | |
fea681da MK |
239 | .TP |
240 | .B ESRCH | |
241 | For | |
e511ffb6 | 242 | .BR getpgid (): |
fea681da MK |
243 | .I pid |
244 | does not match any process. | |
245 | For | |
e511ffb6 | 246 | .BR setpgid (): |
fea681da | 247 | .I pid |
a1ffe9f5 | 248 | is not the calling process and not a child of the calling process. |
47297adb | 249 | .SH CONFORMING TO |
e511ffb6 | 250 | .BR setpgid () |
0bbb92a3 | 251 | and the version of |
e511ffb6 | 252 | .BR getpgrp () |
d68b2713 | 253 | with no arguments |
97c1eac8 | 254 | conform to POSIX.1-2001. |
efeece04 | 255 | .PP |
0bbb92a3 | 256 | POSIX.1-2001 also specifies |
e511ffb6 | 257 | .BR getpgid () |
0bbb92a3 MK |
258 | and the version of |
259 | .BR setpgrp () | |
260 | that takes no arguments. | |
55cfc574 MK |
261 | (POSIX.1-2008 marks this |
262 | .BR setpgrp () | |
263 | specification as obsolete.) | |
efeece04 | 264 | .PP |
0bbb92a3 MK |
265 | The version of |
266 | .BR getpgrp () | |
267 | with one argument and the version of | |
268 | .BR setpgrp () | |
269 | that takes two arguments derive from 4.2BSD, | |
270 | and are not specified by POSIX.1. | |
fea681da | 271 | .SH NOTES |
c13182ef | 272 | A child created via |
73ac0666 MK |
273 | .BR fork (2) |
274 | inherits its parent's process group ID. | |
aadd7709 | 275 | The PGID is preserved across an |
73ac0666 | 276 | .BR execve (2). |
efeece04 | 277 | .PP |
0bbb92a3 MK |
278 | Each process group is a member of a session and each process is a |
279 | member of the session of which its process group is a member. | |
1a765504 MK |
280 | (See |
281 | .BR credentials (7).) | |
efeece04 | 282 | .PP |
0bbb92a3 MK |
283 | A session can have a controlling terminal. |
284 | At any time, one (and only one) of the process groups | |
285 | in the session can be the foreground process group | |
286 | for the terminal; | |
287 | the remaining process groups are in the background. | |
288 | If a signal is generated from the terminal (e.g., typing the | |
289 | interrupt key to generate | |
290 | .BR SIGINT ), | |
291 | that signal is sent to the foreground process group. | |
292 | (See | |
293 | .BR termios (3) | |
294 | for a description of the characters that generate signals.) | |
aadd7709 MK |
295 | Only the foreground process group may |
296 | .BR read (2) | |
297 | from the terminal; | |
298 | if a background process group tries to | |
299 | .BR read (2) | |
15b17877 | 300 | from the terminal, then the group is sent a |
07bfe93f | 301 | .B SIGTTIN |
0bbb92a3 MK |
302 | signal, which suspends it. |
303 | The | |
304 | .BR tcgetpgrp (3) | |
305 | and | |
306 | .BR tcsetpgrp (3) | |
307 | functions are used to get/set the foreground | |
308 | process group of the controlling terminal. | |
efeece04 | 309 | .PP |
0bbb92a3 | 310 | The |
e511ffb6 | 311 | .BR setpgid () |
c3dfd2c8 | 312 | and |
0bbb92a3 MK |
313 | .BR getpgrp () |
314 | calls are used by programs such as | |
315 | .BR bash (1) | |
316 | to create process groups in order to implement shell job control. | |
efeece04 | 317 | .PP |
343313da | 318 | If the termination of a process causes a process group to become orphaned, |
0bbb92a3 MK |
319 | and if any member of the newly orphaned process group is stopped, then a |
320 | .B SIGHUP | |
321 | signal followed by a | |
322 | .B SIGCONT | |
323 | signal will be sent to each process | |
324 | in the newly orphaned process group. | |
a38dc43a MK |
325 | .\" exit.3 refers to the following text: |
326 | An orphaned process group is one in which the parent of | |
327 | every member of process group is either itself also a member | |
328 | of the process group or is a member of a process group | |
329 | in a different session (see also | |
330 | .BR credentials (7)). | |
47297adb | 331 | .SH SEE ALSO |
fea681da MK |
332 | .BR getuid (2), |
333 | .BR setsid (2), | |
334 | .BR tcgetpgrp (3), | |
335 | .BR tcsetpgrp (3), | |
0786e308 MK |
336 | .BR termios (3), |
337 | .BR credentials (7) |