]>
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 | .\" | |
47009d5e | 5 | .\" SPDX-License-Identifier: BSD-4-Clause-UC |
fea681da MK |
6 | .\" |
7 | .\" @(#)getpgrp.2 6.4 (Berkeley) 3/10/91 | |
8 | .\" | |
9 | .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu> | |
10 | .\" Modified 1995-04-15 by Michael Chastain <mec@shell.portal.com>: | |
11 | .\" Added 'getpgid'. | |
12 | .\" Modified 1996-07-21 by Andries Brouwer <aeb@cwi.nl> | |
13 | .\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com> | |
14 | .\" Modified 1999-09-02 by Michael Haardt <michael@moria.de> | |
c11b1abf | 15 | .\" Modified 2002-01-18 by Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 16 | .\" Modified 2003-01-20 by Andries Brouwer <aeb@cwi.nl> |
0bbb92a3 MK |
17 | .\" 2007-07-25, mtk, fairly substantial rewrites and rearrangements |
18 | .\" of text. | |
fea681da | 19 | .\" |
1d767b55 | 20 | .TH SETPGID 2 2021-03-22 "Linux" "Linux Programmer's Manual" |
fea681da MK |
21 | .SH NAME |
22 | setpgid, getpgid, setpgrp, getpgrp \- set/get process group | |
f25c8611 AC |
23 | .SH LIBRARY |
24 | Standard C library | |
8fc3b2cf | 25 | .RI ( libc ", " \-lc ) |
fea681da | 26 | .SH SYNOPSIS |
4653ec6e | 27 | .nf |
fea681da | 28 | .B #include <unistd.h> |
68e4db0a | 29 | .PP |
fea681da | 30 | .BI "int setpgid(pid_t " pid ", pid_t " pgid ); |
fea681da | 31 | .BI "pid_t getpgid(pid_t " pid ); |
68e4db0a | 32 | .PP |
72ef5c5c | 33 | .BR "pid_t getpgrp(void);" " /* POSIX.1 version */" |
afdfa65b | 34 | .BI "pid_t getpgrp(pid_t " pid ");\fR /* BSD version */" |
68e4db0a | 35 | .PP |
72ef5c5c | 36 | .BR "int setpgrp(void);" " /* System V version */" |
afdfa65b | 37 | .BI "int setpgrp(pid_t " pid ", pid_t " pgid ");\fR /* BSD version */" |
4653ec6e | 38 | .fi |
68e4db0a | 39 | .PP |
d39ad78f | 40 | .RS -4 |
0bbb92a3 MK |
41 | Feature Test Macro Requirements for glibc (see |
42 | .BR feature_test_macros (7)): | |
d39ad78f | 43 | .RE |
68e4db0a | 44 | .PP |
0bbb92a3 | 45 | .BR getpgid (): |
9d2adbae | 46 | .nf |
5c10d2c5 MK |
47 | _XOPEN_SOURCE >= 500 |
48 | .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED | |
49 | || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L | |
9d2adbae | 50 | .fi |
68e4db0a | 51 | .PP |
fc541b7a MK |
52 | .BR setpgrp "() (POSIX.1):" |
53 | .nf | |
5c10d2c5 MK |
54 | _XOPEN_SOURCE >= 500 |
55 | .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED | |
f8619b6a | 56 | || /* Since glibc 2.19: */ _DEFAULT_SOURCE |
7d8d2f09 | 57 | || /* Glibc <= 2.19: */ _SVID_SOURCE |
fc541b7a | 58 | .fi |
68e4db0a | 59 | .PP |
2c916a1d MK |
60 | .BR setpgrp "() (BSD)," |
61 | .BR getpgrp "() (BSD):" | |
fc541b7a | 62 | .nf |
f8619b6a | 63 | [These are available only before glibc 2.19] |
fc541b7a | 64 | _BSD_SOURCE && |
5c10d2c5 | 65 | ! (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE |
58a6844d | 66 | || _GNU_SOURCE || _SVID_SOURCE) |
fc541b7a | 67 | .fi |
fea681da | 68 | .SH DESCRIPTION |
0bbb92a3 MK |
69 | All of these interfaces are available on Linux, |
70 | and are used for getting and setting the | |
71 | process group ID (PGID) of a process. | |
72 | The preferred, POSIX.1-specified ways of doing this are: | |
73 | .BR getpgrp (void), | |
74 | for retrieving the calling process's PGID; and | |
75 | .BR setpgid (), | |
76 | for setting a process's PGID. | |
efeece04 | 77 | .PP |
e511ffb6 | 78 | .BR setpgid () |
0bbb92a3 | 79 | sets the PGID of the process specified by |
fea681da MK |
80 | .I pid |
81 | to | |
82 | .IR pgid . | |
83 | If | |
84 | .I pid | |
17c69289 | 85 | is zero, then the process ID of the calling process is used. |
c13182ef | 86 | If |
fea681da | 87 | .I pgid |
17c69289 | 88 | is zero, then the PGID of the process specified by |
fea681da | 89 | .I pid |
17c69289 | 90 | is made the same as its process ID. |
60a90ecd MK |
91 | If |
92 | .BR setpgid () | |
93 | is used to move a process from one process | |
fea681da | 94 | group to another (as is done by some shells when creating pipelines), |
0bbb92a3 MK |
95 | both process groups must be part of the same session (see |
96 | .BR setsid (2) | |
e0bf9127 | 97 | and |
0bbb92a3 | 98 | .BR credentials (7)). |
c13182ef | 99 | In this case, |
fea681da MK |
100 | the \fIpgid\fP specifies an existing process group to be joined and the |
101 | session ID of that group must match the session ID of the joining process. | |
efeece04 | 102 | .PP |
0bbb92a3 MK |
103 | The POSIX.1 version of |
104 | .BR getpgrp (), | |
105 | which takes no arguments, | |
106 | returns the PGID of the calling process. | |
efeece04 | 107 | .PP |
e511ffb6 | 108 | .BR getpgid () |
0bbb92a3 | 109 | returns the PGID of the process specified by |
fea681da MK |
110 | .IR pid . |
111 | If | |
112 | .I pid | |
a1ffe9f5 | 113 | is zero, the process ID of the calling process is used. |
0bbb92a3 MK |
114 | (Retrieving the PGID of a process other than the caller is rarely |
115 | necessary, and the POSIX.1 | |
116 | .BR getpgrp () | |
117 | is preferred for that task.) | |
efeece04 | 118 | .PP |
efbfd7ec | 119 | The System\ V-style |
aadd7709 MK |
120 | .BR setpgrp (), |
121 | which takes no arguments, is equivalent to | |
ab7b478b | 122 | .IR "setpgid(0,\ 0)" . |
efeece04 | 123 | .PP |
0bbb92a3 MK |
124 | The BSD-specific |
125 | .BR setpgrp () | |
126 | call, which takes arguments | |
127 | .I pid | |
fea681da | 128 | and |
0bbb92a3 | 129 | .IR pgid , |
8e6ee8f3 | 130 | is a wrapper function that calls |
efeece04 | 131 | .PP |
1ae6b2c7 AC |
132 | .in +4n |
133 | .EX | |
134 | setpgid(pid, pgid) | |
135 | .EE | |
136 | .in | |
efeece04 | 137 | .PP |
0bbb92a3 MK |
138 | .\" The true BSD setpgrp() system call differs in allowing the PGID |
139 | .\" to be set to arbitrary values, rather than being restricted to | |
140 | .\" PGIDs in the same session. | |
ded44f9b MK |
141 | Since glibc 2.19, the BSD-specific |
142 | .BR setpgrp () | |
143 | function is no longer exposed by | |
144 | .IR <unistd.h> ; | |
145 | calls should be replaced with the | |
146 | .BR setpgid () | |
147 | call shown above. | |
efeece04 | 148 | .PP |
0bbb92a3 MK |
149 | The BSD-specific |
150 | .BR getpgrp () | |
151 | call, which takes a single | |
152 | .I pid | |
ded44f9b | 153 | argument, is a wrapper function that calls |
efeece04 | 154 | .PP |
1ae6b2c7 AC |
155 | .in +4n |
156 | .EX | |
157 | getpgid(pid) | |
158 | .EE | |
159 | .in | |
efeece04 | 160 | .PP |
ded44f9b MK |
161 | Since glibc 2.19, the BSD-specific |
162 | .BR getpgrp () | |
163 | function is no longer exposed by | |
164 | .IR <unistd.h> ; | |
165 | calls should be replaced with calls to the POSIX.1 | |
166 | .BR getpgrp () | |
167 | which takes no arguments (if the intent is to obtain the caller's PGID), | |
168 | or with the | |
169 | .BR getpgid () | |
170 | call shown above. | |
47297adb | 171 | .SH RETURN VALUE |
fea681da | 172 | On success, |
ae050d9a | 173 | .BR setpgid () |
c13182ef | 174 | and |
ae050d9a | 175 | .BR setpgrp () |
c13182ef MK |
176 | return zero. |
177 | On error, \-1 is returned, and | |
fea681da | 178 | .I errno |
f6a4078b | 179 | is set to indicate the error. |
efeece04 | 180 | .PP |
0bbb92a3 MK |
181 | The POSIX.1 |
182 | .BR getpgrp () | |
aadd7709 | 183 | always returns the PGID of the caller. |
efeece04 | 184 | .PP |
0bbb92a3 MK |
185 | .BR getpgid (), |
186 | and the BSD-specific | |
187 | .BR getpgrp () | |
188 | return a process group on success. | |
fea681da MK |
189 | On error, \-1 is returned, and |
190 | .I errno | |
f6a4078b | 191 | is set to indicate the error. |
fea681da MK |
192 | .SH ERRORS |
193 | .TP | |
194 | .B EACCES | |
195 | An attempt was made to change the process group ID | |
196 | of one of the children of the calling process and the child had | |
60a90ecd MK |
197 | already performed an |
198 | .BR execve (2) | |
199 | .RB ( setpgid (), | |
200 | .BR setpgrp ()). | |
fea681da MK |
201 | .TP |
202 | .B EINVAL | |
203 | .I pgid | |
204 | is less than 0 | |
60a90ecd MK |
205 | .RB ( setpgid (), |
206 | .BR setpgrp ()). | |
fea681da MK |
207 | .TP |
208 | .B EPERM | |
c13182ef MK |
209 | An attempt was made to move a process into a process group in a |
210 | different session, or to change the process | |
211 | group ID of one of the children of the calling process and the | |
fea681da MK |
212 | child was in a different session, or to change the process group ID of |
213 | a session leader | |
60a90ecd MK |
214 | .RB ( setpgid (), |
215 | .BR setpgrp ()). | |
fea681da MK |
216 | .TP |
217 | .B ESRCH | |
218 | For | |
e511ffb6 | 219 | .BR getpgid (): |
fea681da MK |
220 | .I pid |
221 | does not match any process. | |
222 | For | |
e511ffb6 | 223 | .BR setpgid (): |
fea681da | 224 | .I pid |
a1ffe9f5 | 225 | is not the calling process and not a child of the calling process. |
47297adb | 226 | .SH CONFORMING TO |
e511ffb6 | 227 | .BR setpgid () |
0bbb92a3 | 228 | and the version of |
e511ffb6 | 229 | .BR getpgrp () |
d68b2713 | 230 | with no arguments |
97c1eac8 | 231 | conform to POSIX.1-2001. |
efeece04 | 232 | .PP |
0bbb92a3 | 233 | POSIX.1-2001 also specifies |
e511ffb6 | 234 | .BR getpgid () |
0bbb92a3 MK |
235 | and the version of |
236 | .BR setpgrp () | |
237 | that takes no arguments. | |
55cfc574 MK |
238 | (POSIX.1-2008 marks this |
239 | .BR setpgrp () | |
240 | specification as obsolete.) | |
efeece04 | 241 | .PP |
0bbb92a3 MK |
242 | The version of |
243 | .BR getpgrp () | |
244 | with one argument and the version of | |
245 | .BR setpgrp () | |
246 | that takes two arguments derive from 4.2BSD, | |
247 | and are not specified by POSIX.1. | |
fea681da | 248 | .SH NOTES |
c13182ef | 249 | A child created via |
73ac0666 MK |
250 | .BR fork (2) |
251 | inherits its parent's process group ID. | |
aadd7709 | 252 | The PGID is preserved across an |
73ac0666 | 253 | .BR execve (2). |
efeece04 | 254 | .PP |
0bbb92a3 MK |
255 | Each process group is a member of a session and each process is a |
256 | member of the session of which its process group is a member. | |
1a765504 MK |
257 | (See |
258 | .BR credentials (7).) | |
efeece04 | 259 | .PP |
0bbb92a3 MK |
260 | A session can have a controlling terminal. |
261 | At any time, one (and only one) of the process groups | |
262 | in the session can be the foreground process group | |
263 | for the terminal; | |
264 | the remaining process groups are in the background. | |
265 | If a signal is generated from the terminal (e.g., typing the | |
266 | interrupt key to generate | |
267 | .BR SIGINT ), | |
268 | that signal is sent to the foreground process group. | |
269 | (See | |
270 | .BR termios (3) | |
271 | for a description of the characters that generate signals.) | |
aadd7709 MK |
272 | Only the foreground process group may |
273 | .BR read (2) | |
274 | from the terminal; | |
275 | if a background process group tries to | |
276 | .BR read (2) | |
15b17877 | 277 | from the terminal, then the group is sent a |
07bfe93f | 278 | .B SIGTTIN |
0bbb92a3 MK |
279 | signal, which suspends it. |
280 | The | |
281 | .BR tcgetpgrp (3) | |
282 | and | |
283 | .BR tcsetpgrp (3) | |
284 | functions are used to get/set the foreground | |
285 | process group of the controlling terminal. | |
efeece04 | 286 | .PP |
0bbb92a3 | 287 | The |
e511ffb6 | 288 | .BR setpgid () |
c3dfd2c8 | 289 | and |
0bbb92a3 MK |
290 | .BR getpgrp () |
291 | calls are used by programs such as | |
292 | .BR bash (1) | |
293 | to create process groups in order to implement shell job control. | |
efeece04 | 294 | .PP |
343313da | 295 | If the termination of a process causes a process group to become orphaned, |
0bbb92a3 MK |
296 | and if any member of the newly orphaned process group is stopped, then a |
297 | .B SIGHUP | |
298 | signal followed by a | |
299 | .B SIGCONT | |
300 | signal will be sent to each process | |
301 | in the newly orphaned process group. | |
a38dc43a MK |
302 | .\" exit.3 refers to the following text: |
303 | An orphaned process group is one in which the parent of | |
304 | every member of process group is either itself also a member | |
305 | of the process group or is a member of a process group | |
306 | in a different session (see also | |
307 | .BR credentials (7)). | |
47297adb | 308 | .SH SEE ALSO |
fea681da MK |
309 | .BR getuid (2), |
310 | .BR setsid (2), | |
311 | .BR tcgetpgrp (3), | |
312 | .BR tcsetpgrp (3), | |
0786e308 MK |
313 | .BR termios (3), |
314 | .BR credentials (7) |