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