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.
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
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.
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
35 .\" @(#)getpgrp.2 6.4 (Berkeley) 3/10/91
37 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
38 .\" Modified 1995-04-15 by Michael Chastain <mec@shell.portal.com>:
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
48 .TH SETPGID 2 2021-03-22 "Linux" "Linux Programmer's Manual"
50 setpgid, getpgid, setpgrp, getpgrp \- set/get process group
53 .B #include <unistd.h>
55 .BI "int setpgid(pid_t " pid ", pid_t " pgid );
56 .BI "pid_t getpgid(pid_t " pid );
58 .BR "pid_t getpgrp(void);" " /* POSIX.1 version */"
59 .BI "pid_t getpgrp(pid_t " pid ");\fR /* BSD version */"
61 .BR "int setpgrp(void);" " /* System V version */"
62 .BI "int setpgrp(pid_t " pid ", pid_t " pgid ");\fR /* BSD version */"
66 Feature Test Macro Requirements for glibc (see
67 .BR feature_test_macros (7)):
73 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
74 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
77 .BR setpgrp "() (POSIX.1):"
80 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
81 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
82 || /* Glibc <= 2.19: */ _SVID_SOURCE
85 .BR setpgrp "() (BSD),"
86 .BR getpgrp "() (BSD):"
88 [These are available only before glibc 2.19]
90 ! (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE
91 || _GNU_SOURCE || _SVID_SOURCE)
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:
99 for retrieving the calling process's PGID; and
101 for setting a process's PGID.
104 sets the PGID of the process specified by
110 is zero, then the process ID of the calling process is used.
113 is zero, then the PGID of the process specified by
115 is made the same as its process ID.
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
123 .BR credentials (7)).
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
130 which takes no arguments,
131 returns the PGID of the calling process.
134 returns the PGID of the process specified by
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
142 is preferred for that task.)
146 which takes no arguments, is equivalent to
147 .IR "setpgid(0,\ 0)" .
151 call, which takes arguments
155 is a wrapper function that calls
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
164 function is no longer exposed by
166 calls should be replaced with the
172 call, which takes a single
174 argument, is a wrapper function that calls
178 Since glibc 2.19, the BSD-specific
180 function is no longer exposed by
182 calls should be replaced with calls to the POSIX.1
184 which takes no arguments (if the intent is to obtain the caller's PGID),
194 On error, \-1 is returned, and
196 is set to indicate the error.
200 always returns the PGID of the caller.
205 return a process group on success.
206 On error, \-1 is returned, and
208 is set to indicate the error.
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
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
238 does not match any process.
242 is not the calling process and not a child of the calling process.
248 conform to POSIX.1-2001.
250 POSIX.1-2001 also specifies
254 that takes no arguments.
255 (POSIX.1-2008 marks this
257 specification as obsolete.)
261 with one argument and the version of
263 that takes two arguments derive from 4.2BSD,
264 and are not specified by POSIX.1.
268 inherits its parent's process group ID.
269 The PGID is preserved across an
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.
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
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
285 that signal is sent to the foreground process group.
288 for a description of the characters that generate signals.)
289 Only the foreground process group may
292 if a background process group tries to
294 from the terminal, then the group is sent a
296 signal, which suspends it.
301 functions are used to get/set the foreground
302 process group of the controlling terminal.
308 calls are used by programs such as
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
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)).