1SETPGID(2) Linux Programmer's Manual SETPGID(2)
2
3
4
6 setpgid, getpgid, setpgrp, getpgrp - set/get process group
7
9 #include <unistd.h>
10
11 int setpgid(pid_t pid, pid_t pgid);
12 pid_t getpgid(pid_t pid);
13
14 pid_t getpgrp(void); /* POSIX.1 version */
15 pid_t getpgrp(psid_t pid); /* BSD version */
16
17 int setpgrp(void); /* System V version */
18 int setpgrp(pid_t pid, pid_t pgid); /* BSD version */
19
20 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
21
22 getpgid(): _XOPEN_SOURCE >= 500
23 setpgrp() (POSIX.1): _SVID_SOURCE || _XOPEN_SOURCE >= 500
24
25 setpgrp() (BSD), getpgrp() (BSD): _BSD_SOURCE && ! (_POSIX_SOURCE ||
26 _POSIX_C_SOURCE || _XOPEN_SOURCE || _XOPEN_SOURCE_EXTENDED ||
27 _GNU_SOURCE || _SVID_SOURCE)
28
30 All of these interfaces are available on Linux, and are used for get‐
31 ting and setting the process group ID (PGID) of a process. The pre‐
32 ferred, POSIX.1-specified ways of doing this are: getpgrp(void), for
33 retrieving the calling process's PGID; and setpgid(), for setting a
34 process's PGID.
35
36 setpgid() sets the PGID of the process specified by pid to pgid. If
37 pid is zero, then the process ID of the calling process is used. If
38 pgid is zero, then the PGID of the process specified by pid is made the
39 same as its process ID. If setpgid() is used to move a process from
40 one process group to another (as is done by some shells when creating
41 pipelines), both process groups must be part of the same session (see
42 setsid(2) and credentials(7)). In this case, the pgid specifies an
43 existing process group to be joined and the session ID of that group
44 must match the session ID of the joining process.
45
46 The POSIX.1 version of getpgrp(), which takes no arguments, returns the
47 PGID of the calling process.
48
49 getpgid() returns the PGID of the process specified by pid. If pid is
50 zero, the process ID of the calling process is used. (Retrieving the
51 PGID of a process other than the caller is rarely necessary, and the
52 POSIX.1 getpgrp() is preferred for that task.)
53
54 The System V-style setpgrp(), which takes no arguments, is equivalent
55 to setpgid(0, 0).
56
57 The BSD-specific setpgrp() call, which takes arguments pid and pgid, is
58 equivalent to setpgid(pid, pgid).
59
60 The BSD-specific getpgrp() call, which takes a single pid argument, is
61 equivalent to getpgid(pid).
62
64 On success, setpgid() and setpgrp() return zero. On error, -1 is
65 returned, and errno is set appropriately.
66
67 The POSIX.1 getpgrp() always returns the PGID of the caller.
68
69 getpgid(), and the BSD-specific getpgrp() return a process group on
70 success. On error, -1 is returned, and errno is set appropriately.
71
73 EACCES An attempt was made to change the process group ID of one of the
74 children of the calling process and the child had already per‐
75 formed an execve(2) (setpgid(), setpgrp()).
76
77 EINVAL pgid is less than 0 (setpgid(), setpgrp()).
78
79 EPERM An attempt was made to move a process into a process group in a
80 different session, or to change the process group ID of one of
81 the children of the calling process and the child was in a dif‐
82 ferent session, or to change the process group ID of a session
83 leader (setpgid(), setpgrp()).
84
85 ESRCH For getpgid(): pid does not match any process. For setpgid():
86 pid is not the calling process and not a child of the calling
87 process.
88
90 setpgid() and the version of getpgrp() with no arguments conform to
91 POSIX.1-2001.
92
93 POSIX.1-2001 also specifies getpgid() and the version of setpgrp() that
94 takes no arguments. (POSIX.1-2008 marks this setpgrp() specification
95 as obsolete.)
96
97 The version of getpgrp() with one argument and the version of setpgrp()
98 that takes two arguments derive from 4.2BSD, and are not specified by
99 POSIX.1.
100
102 A child created via fork(2) inherits its parent's process group ID.
103 The PGID is preserved across an execve(2).
104
105 Each process group is a member of a session and each process is a mem‐
106 ber of the session of which its process group is a member.
107
108 A session can have a controlling terminal. At any time, one (and only
109 one) of the process groups in the session can be the foreground process
110 group for the terminal; the remaining process groups are in the back‐
111 ground. If a signal is generated from the terminal (e.g., typing the
112 interrupt key to generate SIGINT), that signal is sent to the fore‐
113 ground process group. (See termios(3) for a description of the charac‐
114 ters that generate signals.) Only the foreground process group may
115 read(2) from the terminal; if a background process group tries to
116 read(2) from the terminal, then the group is sent a SIGTSTP signal,
117 which suspends it. The tcgetpgrp(3) and tcsetpgrp(3) functions are
118 used to get/set the foreground process group of the controlling termi‐
119 nal.
120
121 The setpgid() and getpgrp() calls are used by programs such as bash(1)
122 to create process groups in order to implement shell job control.
123
124 If a session has a controlling terminal, and the CLOCAL flag for that
125 terminal is not set, and a terminal hangup occurs, then the session
126 leader is sent a SIGHUP. If the session leader exits, then a SIGHUP
127 signal will also be sent to each process in the foreground process
128 group of the controlling terminal.
129
130 If the exit of the process causes a process group to become orphaned,
131 and if any member of the newly orphaned process group is stopped, then
132 a SIGHUP signal followed by a SIGCONT signal will be sent to each
133 process in the newly orphaned process group.
134
136 getuid(2), setsid(2), tcgetpgrp(3), tcsetpgrp(3), termios(3), creden‐
137 tials(7)
138
140 This page is part of release 3.22 of the Linux man-pages project. A
141 description of the project, and information about reporting bugs, can
142 be found at http://www.kernel.org/doc/man-pages/.
143
144
145
146Linux 2008-08-06 SETPGID(2)