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 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by the University of
16 .\" California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\" may be used to endorse or promote products derived from this software
19 .\" without specific prior written permission.
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 .\" @(#)getpgrp.2 6.4 (Berkeley) 3/10/91
35 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
36 .\" Modified 1995-04-15 by Michael Chastain <mec@shell.portal.com>:
38 .\" Modified 1996-07-21 by Andries Brouwer <aeb@cwi.nl>
39 .\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com>
40 .\" Modified 1999-09-02 by Michael Haardt <michael@moria.de>
41 .\" Modified 2002-01-18 by Michael Kerrisk <mtk.manpages@gmail.com>
42 .\" Modified 2003-01-20 by Andries Brouwer <aeb@cwi.nl>
43 .\" 2007-07-25, mtk, fairly substantial rewrites and rearrangements
46 .TH SETPGID 2 2010-09-26 "Linux" "Linux Programmer's Manual"
48 setpgid, getpgid, setpgrp, getpgrp \- set/get process group
50 .B #include <unistd.h>
52 .BI "int setpgid(pid_t " pid ", pid_t " pgid );
54 .BI "pid_t getpgid(pid_t " pid );
56 .BR "pid_t getpgrp(void);" " /* POSIX.1 version */"
58 .BI "pid_t getpgrp(pid_t " pid ");\ \ \ \ \ \ \ \ \ \ \ "
61 .BR "int setpgrp(void);" " /* System V version */"
63 .BI "int setpgrp(pid_t " pid ", pid_t " pgid ");\ "
67 Feature Test Macro Requirements for glibc (see
68 .BR feature_test_macros (7)):
75 _XOPEN_SOURCE\ >=\ 500 ||
76 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
78 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
81 .BR setpgrp "() (POSIX.1):"
83 _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
84 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
87 .BR setpgrp "()\ (BSD),"
88 .BR getpgrp "()\ (BSD):"
91 !\ (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE ||
92 _XOPEN_SOURCE_EXTENDED || _GNU_SOURCE || _SVID_SOURCE)
97 All of these interfaces are available on Linux,
98 and are used for getting and setting the
99 process group ID (PGID) of a process.
100 The preferred, POSIX.1-specified ways of doing this are:
102 for retrieving the calling process's PGID; and
104 for setting a process's PGID.
107 sets the PGID of the process specified by
113 is zero, then the process ID of the calling process is used.
116 is zero, then the PGID of the process specified by
118 is made the same as its process ID.
121 is used to move a process from one process
122 group to another (as is done by some shells when creating pipelines),
123 both process groups must be part of the same session (see
126 .BR credentials (7)).
128 the \fIpgid\fP specifies an existing process group to be joined and the
129 session ID of that group must match the session ID of the joining process.
131 The POSIX.1 version of
133 which takes no arguments,
134 returns the PGID of the calling process.
137 returns the PGID of the process specified by
141 is zero, the process ID of the calling process is used.
142 (Retrieving the PGID of a process other than the caller is rarely
143 necessary, and the POSIX.1
145 is preferred for that task.)
149 which takes no arguments, is equivalent to
150 .IR "setpgid(0,\ 0)" .
154 call, which takes arguments
159 .IR "setpgid(pid, pgid)" .
160 .\" The true BSD setpgrp() system call differs in allowing the PGID
161 .\" to be set to arbitrary values, rather than being restricted to
162 .\" PGIDs in the same session.
166 call, which takes a single
168 argument, is equivalent to
176 On error, \-1 is returned, and
178 is set appropriately.
182 always returns the PGID of the caller.
187 return a process group on success.
188 On error, \-1 is returned, and
190 is set appropriately.
194 An attempt was made to change the process group ID
195 of one of the children of the calling process and the child had
208 An attempt was made to move a process into a process group in a
209 different session, or to change the process
210 group ID of one of the children of the calling process and the
211 child was in a different session, or to change the process group ID of
220 does not match any process.
224 is not the calling process and not a child of the calling process.
230 conform to POSIX.1-2001.
232 POSIX.1-2001 also specifies
236 that takes no arguments.
237 (POSIX.1-2008 marks this
239 specification as obsolete.)
243 with one argument and the version of
245 that takes two arguments derive from 4.2BSD,
246 and are not specified by POSIX.1.
250 inherits its parent's process group ID.
251 The PGID is preserved across an
254 Each process group is a member of a session and each process is a
255 member of the session of which its process group is a member.
257 A session can have a controlling terminal.
258 At any time, one (and only one) of the process groups
259 in the session can be the foreground process group
261 the remaining process groups are in the background.
262 If a signal is generated from the terminal (e.g., typing the
263 interrupt key to generate
265 that signal is sent to the foreground process group.
268 for a description of the characters that generate signals.)
269 Only the foreground process group may
272 if a background process group tries to
274 from the terminal, then the group is sent a
276 signal, which suspends it.
281 functions are used to get/set the foreground
282 process group of the controlling terminal.
288 calls are used by programs such as
290 to create process groups in order to implement shell job control.
292 If a session has a controlling terminal, and the
294 flag for that terminal is not set,
295 and a terminal hangup occurs, then the session leader is sent a
297 If the session leader exits, then a
299 signal will also be sent to each process in the foreground
300 process group of the controlling terminal.
302 If the exit of the process causes a process group to become orphaned,
303 and if any member of the newly orphaned process group is stopped, then a
307 signal will be sent to each process
308 in the newly orphaned process group.
309 .\" exit.3 refers to the following text:
310 An orphaned process group is one in which the parent of
311 every member of process group is either itself also a member
312 of the process group or is a member of a process group
313 in a different session (see also
314 .BR credentials (7)).