]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/setpgid.2
sched_setattr.2: tfix
[thirdparty/man-pages.git] / man2 / setpgid.2
CommitLineData
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
50setpgid, 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
69Feature 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
100All of these interfaces are available on Linux,
101and are used for getting and setting the
102process group ID (PGID) of a process.
103The preferred, POSIX.1-specified ways of doing this are:
104.BR getpgrp (void),
105for retrieving the calling process's PGID; and
106.BR setpgid (),
107for setting a process's PGID.
efeece04 108.PP
e511ffb6 109.BR setpgid ()
0bbb92a3 110sets the PGID of the process specified by
fea681da
MK
111.I pid
112to
113.IR pgid .
114If
115.I pid
17c69289 116is zero, then the process ID of the calling process is used.
c13182ef 117If
fea681da 118.I pgid
17c69289 119is zero, then the PGID of the process specified by
fea681da 120.I pid
17c69289 121is made the same as its process ID.
60a90ecd
MK
122If
123.BR setpgid ()
124is used to move a process from one process
fea681da 125group to another (as is done by some shells when creating pipelines),
0bbb92a3
MK
126both process groups must be part of the same session (see
127.BR setsid (2)
e0bf9127 128and
0bbb92a3 129.BR credentials (7)).
c13182ef 130In this case,
fea681da
MK
131the \fIpgid\fP specifies an existing process group to be joined and the
132session ID of that group must match the session ID of the joining process.
efeece04 133.PP
0bbb92a3
MK
134The POSIX.1 version of
135.BR getpgrp (),
136which takes no arguments,
137returns the PGID of the calling process.
efeece04 138.PP
e511ffb6 139.BR getpgid ()
0bbb92a3 140returns the PGID of the process specified by
fea681da
MK
141.IR pid .
142If
143.I pid
a1ffe9f5 144is 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
146necessary, and the POSIX.1
147.BR getpgrp ()
148is preferred for that task.)
efeece04 149.PP
efbfd7ec 150The System\ V-style
aadd7709
MK
151.BR setpgrp (),
152which takes no arguments, is equivalent to
ab7b478b 153.IR "setpgid(0,\ 0)" .
efeece04 154.PP
0bbb92a3
MK
155The BSD-specific
156.BR setpgrp ()
157call, which takes arguments
158.I pid
fea681da 159and
0bbb92a3 160.IR pgid ,
8e6ee8f3 161is 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
168Since glibc 2.19, the BSD-specific
169.BR setpgrp ()
170function is no longer exposed by
171.IR <unistd.h> ;
172calls should be replaced with the
173.BR setpgid ()
174call shown above.
efeece04 175.PP
0bbb92a3
MK
176The BSD-specific
177.BR getpgrp ()
178call, which takes a single
179.I pid
ded44f9b 180argument, is a wrapper function that calls
efeece04 181.PP
ded44f9b 182 getpgid(pid)
efeece04 183.PP
ded44f9b
MK
184Since glibc 2.19, the BSD-specific
185.BR getpgrp ()
186function is no longer exposed by
187.IR <unistd.h> ;
188calls should be replaced with calls to the POSIX.1
189.BR getpgrp ()
190which takes no arguments (if the intent is to obtain the caller's PGID),
191or with the
192.BR getpgid ()
193call shown above.
47297adb 194.SH RETURN VALUE
fea681da 195On success,
ae050d9a 196.BR setpgid ()
c13182ef 197and
ae050d9a 198.BR setpgrp ()
c13182ef
MK
199return zero.
200On error, \-1 is returned, and
fea681da
MK
201.I errno
202is set appropriately.
efeece04 203.PP
0bbb92a3
MK
204The POSIX.1
205.BR getpgrp ()
aadd7709 206always returns the PGID of the caller.
efeece04 207.PP
0bbb92a3
MK
208.BR getpgid (),
209and the BSD-specific
210.BR getpgrp ()
211return a process group on success.
fea681da
MK
212On error, \-1 is returned, and
213.I errno
214is set appropriately.
fea681da
MK
215.SH ERRORS
216.TP
217.B EACCES
218An attempt was made to change the process group ID
219of one of the children of the calling process and the child had
60a90ecd
MK
220already performed an
221.BR execve (2)
222.RB ( setpgid (),
223.BR setpgrp ()).
fea681da
MK
224.TP
225.B EINVAL
226.I pgid
227is less than 0
60a90ecd
MK
228.RB ( setpgid (),
229.BR setpgrp ()).
fea681da
MK
230.TP
231.B EPERM
c13182ef
MK
232An attempt was made to move a process into a process group in a
233different session, or to change the process
234group ID of one of the children of the calling process and the
fea681da
MK
235child was in a different session, or to change the process group ID of
236a session leader
60a90ecd
MK
237.RB ( setpgid (),
238.BR setpgrp ()).
fea681da
MK
239.TP
240.B ESRCH
241For
e511ffb6 242.BR getpgid ():
fea681da
MK
243.I pid
244does not match any process.
245For
e511ffb6 246.BR setpgid ():
fea681da 247.I pid
a1ffe9f5 248is not the calling process and not a child of the calling process.
47297adb 249.SH CONFORMING TO
e511ffb6 250.BR setpgid ()
0bbb92a3 251and the version of
e511ffb6 252.BR getpgrp ()
d68b2713 253with no arguments
97c1eac8 254conform to POSIX.1-2001.
efeece04 255.PP
0bbb92a3 256POSIX.1-2001 also specifies
e511ffb6 257.BR getpgid ()
0bbb92a3
MK
258and the version of
259.BR setpgrp ()
260that takes no arguments.
55cfc574
MK
261(POSIX.1-2008 marks this
262.BR setpgrp ()
263specification as obsolete.)
efeece04 264.PP
0bbb92a3
MK
265The version of
266.BR getpgrp ()
267with one argument and the version of
268.BR setpgrp ()
269that takes two arguments derive from 4.2BSD,
270and are not specified by POSIX.1.
fea681da 271.SH NOTES
c13182ef 272A child created via
73ac0666
MK
273.BR fork (2)
274inherits its parent's process group ID.
aadd7709 275The PGID is preserved across an
73ac0666 276.BR execve (2).
efeece04 277.PP
0bbb92a3
MK
278Each process group is a member of a session and each process is a
279member of the session of which its process group is a member.
1a765504
MK
280(See
281.BR credentials (7).)
efeece04 282.PP
0bbb92a3
MK
283A session can have a controlling terminal.
284At any time, one (and only one) of the process groups
285in the session can be the foreground process group
286for the terminal;
287the remaining process groups are in the background.
288If a signal is generated from the terminal (e.g., typing the
289interrupt key to generate
290.BR SIGINT ),
291that signal is sent to the foreground process group.
292(See
293.BR termios (3)
294for a description of the characters that generate signals.)
aadd7709
MK
295Only the foreground process group may
296.BR read (2)
297from the terminal;
298if a background process group tries to
299.BR read (2)
15b17877 300from the terminal, then the group is sent a
07bfe93f 301.B SIGTTIN
0bbb92a3
MK
302signal, which suspends it.
303The
304.BR tcgetpgrp (3)
305and
306.BR tcsetpgrp (3)
307functions are used to get/set the foreground
308process group of the controlling terminal.
efeece04 309.PP
0bbb92a3 310The
e511ffb6 311.BR setpgid ()
c3dfd2c8 312and
0bbb92a3
MK
313.BR getpgrp ()
314calls are used by programs such as
315.BR bash (1)
316to create process groups in order to implement shell job control.
efeece04 317.PP
343313da 318If the termination of a process causes a process group to become orphaned,
0bbb92a3
MK
319and if any member of the newly orphaned process group is stopped, then a
320.B SIGHUP
321signal followed by a
322.B SIGCONT
323signal will be sent to each process
324in the newly orphaned process group.
a38dc43a
MK
325.\" exit.3 refers to the following text:
326An orphaned process group is one in which the parent of
327every member of process group is either itself also a member
328of the process group or is a member of a process group
329in 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)