ioctl_tty.2: Update DTR example
[man-pages.git] / man2 / setpgid.2
blobf3be0eef4f2f6380ab7b11314fc75061c6be8abe
1 .\" Copyright (c) 1983, 1991 Regents of the University of California.
2 .\" and Copyright (C) 2007, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" All rights reserved.
4 .\"
5 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
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.
33 .\" %%%LICENSE_END
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>
43 .\" Modified 2002-01-18 by Michael Kerrisk <mtk.manpages@gmail.com>
44 .\" Modified 2003-01-20 by Andries Brouwer <aeb@cwi.nl>
45 .\" 2007-07-25, mtk, fairly substantial rewrites and rearrangements
46 .\" of text.
47 .\"
48 .TH SETPGID 2 2021-03-22 "Linux" "Linux Programmer's Manual"
49 .SH NAME
50 setpgid, getpgid, setpgrp, getpgrp \- set/get process group
51 .SH SYNOPSIS
52 .nf
53 .B #include <unistd.h>
54 .PP
55 .BI "int setpgid(pid_t " pid ", pid_t " pgid );
56 .BI "pid_t getpgid(pid_t " pid );
57 .PP
58 .BR "pid_t getpgrp(void);" "                 /* POSIX.1 version */"
59 .BI "pid_t getpgrp(pid_t " pid ");\fR            /* BSD version */"
60 .PP
61 .BR "int setpgrp(void);" "                   /* System V version */"
62 .BI "int setpgrp(pid_t " pid ", pid_t " pgid ");\fR  /* BSD version */"
63 .fi
64 .PP
65 .RS -4
66 Feature Test Macro Requirements for glibc (see
67 .BR feature_test_macros (7)):
68 .RE
69 .PP
70 .BR getpgid ():
71 .nf
72     _XOPEN_SOURCE >= 500
73 .\"    || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
74         || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
75 .fi
76 .PP
77 .BR setpgrp "() (POSIX.1):"
78 .nf
79     _XOPEN_SOURCE >= 500
80 .\"    || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
81         || /* Since glibc 2.19: */ _DEFAULT_SOURCE
82         || /* Glibc <= 2.19: */ _SVID_SOURCE
83 .fi
84 .PP
85 .BR setpgrp "() (BSD),"
86 .BR getpgrp "() (BSD):"
87 .nf
88     [These are available only before glibc 2.19]
89     _BSD_SOURCE &&
90         ! (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE
91             || _GNU_SOURCE || _SVID_SOURCE)
92 .fi
93 .SH DESCRIPTION
94 All of these interfaces are available on Linux,
95 and are used for getting and setting the
96 process group ID (PGID) of a process.
97 The preferred, POSIX.1-specified ways of doing this are:
98 .BR getpgrp (void),
99 for retrieving the calling process's PGID; and
100 .BR setpgid (),
101 for setting a process's PGID.
103 .BR setpgid ()
104 sets the PGID of the process specified by
105 .I pid
107 .IR pgid .
109 .I pid
110 is zero, then the process ID of the calling process is used.
112 .I pgid
113 is zero, then the PGID of the process specified by
114 .I pid
115 is made the same as its process ID.
117 .BR setpgid ()
118 is used to move a process from one process
119 group to another (as is done by some shells when creating pipelines),
120 both process groups must be part of the same session (see
121 .BR setsid (2)
123 .BR credentials (7)).
124 In this case,
125 the \fIpgid\fP specifies an existing process group to be joined and the
126 session ID of that group must match the session ID of the joining process.
128 The POSIX.1 version of
129 .BR getpgrp (),
130 which takes no arguments,
131 returns the PGID of the calling process.
133 .BR getpgid ()
134 returns the PGID of the process specified by
135 .IR pid .
137 .I pid
138 is zero, the process ID of the calling process is used.
139 (Retrieving the PGID of a process other than the caller is rarely
140 necessary, and the POSIX.1
141 .BR getpgrp ()
142 is preferred for that task.)
144 The System\ V-style
145 .BR setpgrp (),
146 which takes no arguments, is equivalent to
147 .IR "setpgid(0,\ 0)" .
149 The BSD-specific
150 .BR setpgrp ()
151 call, which takes arguments
152 .I pid
154 .IR pgid ,
155 is a wrapper function that calls
157     setpgid(pid, pgid)
159 .\" The true BSD setpgrp() system call differs in allowing the PGID
160 .\" to be set to arbitrary values, rather than being restricted to
161 .\" PGIDs in the same session.
162 Since glibc 2.19, the BSD-specific
163 .BR setpgrp ()
164 function is no longer exposed by
165 .IR <unistd.h> ;
166 calls should be replaced with the
167 .BR setpgid ()
168 call shown above.
170 The BSD-specific
171 .BR getpgrp ()
172 call, which takes a single
173 .I pid
174 argument, is a wrapper function that calls
176     getpgid(pid)
178 Since glibc 2.19, the BSD-specific
179 .BR getpgrp ()
180 function is no longer exposed by
181 .IR <unistd.h> ;
182 calls should be replaced with calls to the POSIX.1
183 .BR getpgrp ()
184 which takes no arguments (if the intent is to obtain the caller's PGID),
185 or with the
186 .BR getpgid ()
187 call shown above.
188 .SH RETURN VALUE
189 On success,
190 .BR setpgid ()
192 .BR setpgrp ()
193 return zero.
194 On error, \-1 is returned, and
195 .I errno
196 is set to indicate the error.
198 The POSIX.1
199 .BR getpgrp ()
200 always returns the PGID of the caller.
202 .BR getpgid (),
203 and the BSD-specific
204 .BR getpgrp ()
205 return a process group on success.
206 On error, \-1 is returned, and
207 .I errno
208 is set to indicate the error.
209 .SH ERRORS
211 .B EACCES
212 An attempt was made to change the process group ID
213 of one of the children of the calling process and the child had
214 already performed an
215 .BR execve (2)
216 .RB ( setpgid (),
217 .BR setpgrp ()).
219 .B EINVAL
220 .I pgid
221 is less than 0
222 .RB ( setpgid (),
223 .BR setpgrp ()).
225 .B EPERM
226 An attempt was made to move a process into a process group in a
227 different session, or to change the process
228 group ID of one of the children of the calling process and the
229 child was in a different session, or to change the process group ID of
230 a session leader
231 .RB ( setpgid (),
232 .BR setpgrp ()).
234 .B ESRCH
236 .BR getpgid ():
237 .I pid
238 does not match any process.
240 .BR setpgid ():
241 .I pid
242 is not the calling process and not a child of the calling process.
243 .SH CONFORMING TO
244 .BR setpgid ()
245 and the version of
246 .BR getpgrp ()
247 with no arguments
248 conform to POSIX.1-2001.
250 POSIX.1-2001 also specifies
251 .BR getpgid ()
252 and the version of
253 .BR setpgrp ()
254 that takes no arguments.
255 (POSIX.1-2008 marks this
256 .BR setpgrp ()
257 specification as obsolete.)
259 The version of
260 .BR getpgrp ()
261 with one argument and the version of
262 .BR setpgrp ()
263 that takes two arguments derive from 4.2BSD,
264 and are not specified by POSIX.1.
265 .SH NOTES
266 A child created via
267 .BR fork (2)
268 inherits its parent's process group ID.
269 The PGID is preserved across an
270 .BR execve (2).
272 Each process group is a member of a session and each process is a
273 member of the session of which its process group is a member.
274 (See
275 .BR credentials (7).)
277 A session can have a controlling terminal.
278 At any time, one (and only one) of the process groups
279 in the session can be the foreground process group
280 for the terminal;
281 the remaining process groups are in the background.
282 If a signal is generated from the terminal (e.g., typing the
283 interrupt key to generate
284 .BR SIGINT ),
285 that signal is sent to the foreground process group.
286 (See
287 .BR termios (3)
288 for a description of the characters that generate signals.)
289 Only the foreground process group may
290 .BR read (2)
291 from the terminal;
292 if a background process group tries to
293 .BR read (2)
294 from the terminal, then the group is sent a
295 .B SIGTTIN
296 signal, which suspends it.
298 .BR tcgetpgrp (3)
300 .BR tcsetpgrp (3)
301 functions are used to get/set the foreground
302 process group of the controlling terminal.
305 .BR setpgid ()
307 .BR getpgrp ()
308 calls are used by programs such as
309 .BR bash (1)
310 to create process groups in order to implement shell job control.
312 If the termination of a process causes a process group to become orphaned,
313 and if any member of the newly orphaned process group is stopped, then a
314 .B SIGHUP
315 signal followed by a
316 .B SIGCONT
317 signal will be sent to each process
318 in the newly orphaned process group.
319 .\" exit.3 refers to the following text:
320 An orphaned process group is one in which the parent of
321 every member of process group is either itself also a member
322 of the process group or is a member of a process group
323 in a different session (see also
324 .BR credentials (7)).
325 .SH SEE ALSO
326 .BR getuid (2),
327 .BR setsid (2),
328 .BR tcgetpgrp (3),
329 .BR tcsetpgrp (3),
330 .BR termios (3),
331 .BR credentials (7)