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(pid_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():
23 _XOPEN_SOURCE >= 500
24 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
25
26 setpgrp() (POSIX.1):
27 _XOPEN_SOURCE >= 500
28 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
29 || /* Glibc <= 2.19: */ _SVID_SOURCE
30
31 setpgrp() (BSD), getpgrp() (BSD):
32 [These are available only before glibc 2.19]
33 _BSD_SOURCE &&
34 ! (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE
35 || _GNU_SOURCE || _SVID_SOURCE)
36
38 All of these interfaces are available on Linux, and are used for get‐
39 ting and setting the process group ID (PGID) of a process. The pre‐
40 ferred, POSIX.1-specified ways of doing this are: getpgrp(void), for
41 retrieving the calling process's PGID; and setpgid(), for setting a
42 process's PGID.
43
44 setpgid() sets the PGID of the process specified by pid to pgid. If
45 pid is zero, then the process ID of the calling process is used. If
46 pgid is zero, then the PGID of the process specified by pid is made the
47 same as its process ID. If setpgid() is used to move a process from
48 one process group to another (as is done by some shells when creating
49 pipelines), both process groups must be part of the same session (see
50 setsid(2) and credentials(7)). In this case, the pgid specifies an ex‐
51 isting process group to be joined and the session ID of that group must
52 match the session ID of the joining process.
53
54 The POSIX.1 version of getpgrp(), which takes no arguments, returns the
55 PGID of the calling process.
56
57 getpgid() returns the PGID of the process specified by pid. If pid is
58 zero, the process ID of the calling process is used. (Retrieving the
59 PGID of a process other than the caller is rarely necessary, and the
60 POSIX.1 getpgrp() is preferred for that task.)
61
62 The System V-style setpgrp(), which takes no arguments, is equivalent
63 to setpgid(0, 0).
64
65 The BSD-specific setpgrp() call, which takes arguments pid and pgid, is
66 a wrapper function that calls
67
68 setpgid(pid, pgid)
69
70 Since glibc 2.19, the BSD-specific setpgrp() function is no longer ex‐
71 posed by <unistd.h>; calls should be replaced with the setpgid() call
72 shown above.
73
74 The BSD-specific getpgrp() call, which takes a single pid argument, is
75 a wrapper function that calls
76
77 getpgid(pid)
78
79 Since glibc 2.19, the BSD-specific getpgrp() function is no longer ex‐
80 posed by <unistd.h>; calls should be replaced with calls to the POSIX.1
81 getpgrp() which takes no arguments (if the intent is to obtain the
82 caller's PGID), or with the getpgid() call shown above.
83
85 On success, setpgid() and setpgrp() return zero. On error, -1 is re‐
86 turned, and errno is set to indicate the error.
87
88 The POSIX.1 getpgrp() always returns the PGID of the caller.
89
90 getpgid(), and the BSD-specific getpgrp() return a process group on
91 success. On error, -1 is returned, and errno is set to indicate the
92 error.
93
95 EACCES An attempt was made to change the process group ID of one of the
96 children of the calling process and the child had already per‐
97 formed an execve(2) (setpgid(), setpgrp()).
98
99 EINVAL pgid is less than 0 (setpgid(), setpgrp()).
100
101 EPERM An attempt was made to move a process into a process group in a
102 different session, or to change the process group ID of one of
103 the children of the calling process and the child was in a dif‐
104 ferent session, or to change the process group ID of a session
105 leader (setpgid(), setpgrp()).
106
107 ESRCH For getpgid(): pid does not match any process. For setpgid():
108 pid is not the calling process and not a child of the calling
109 process.
110
112 setpgid() and the version of getpgrp() with no arguments conform to
113 POSIX.1-2001.
114
115 POSIX.1-2001 also specifies getpgid() and the version of setpgrp() that
116 takes no arguments. (POSIX.1-2008 marks this setpgrp() specification
117 as obsolete.)
118
119 The version of getpgrp() with one argument and the version of setpgrp()
120 that takes two arguments derive from 4.2BSD, and are not specified by
121 POSIX.1.
122
124 A child created via fork(2) inherits its parent's process group ID.
125 The PGID is preserved across an execve(2).
126
127 Each process group is a member of a session and each process is a mem‐
128 ber of the session of which its process group is a member. (See cre‐
129 dentials(7).)
130
131 A session can have a controlling terminal. At any time, one (and only
132 one) of the process groups in the session can be the foreground process
133 group for the terminal; the remaining process groups are in the back‐
134 ground. If a signal is generated from the terminal (e.g., typing the
135 interrupt key to generate SIGINT), that signal is sent to the fore‐
136 ground process group. (See termios(3) for a description of the charac‐
137 ters that generate signals.) Only the foreground process group may
138 read(2) from the terminal; if a background process group tries to
139 read(2) from the terminal, then the group is sent a SIGTTIN signal,
140 which suspends it. The tcgetpgrp(3) and tcsetpgrp(3) functions are
141 used to get/set the foreground process group of the controlling termi‐
142 nal.
143
144 The setpgid() and getpgrp() calls are used by programs such as bash(1)
145 to create process groups in order to implement shell job control.
146
147 If the termination of a process causes a process group to become or‐
148 phaned, and if any member of the newly orphaned process group is
149 stopped, then a SIGHUP signal followed by a SIGCONT signal will be sent
150 to each process in the newly orphaned process group. An orphaned
151 process group is one in which the parent of every member of process
152 group is either itself also a member of the process group or is a mem‐
153 ber of a process group in a different session (see also creden‐
154 tials(7)).
155
157 getuid(2), setsid(2), tcgetpgrp(3), tcsetpgrp(3), termios(3), creden‐
158 tials(7)
159
161 This page is part of release 5.12 of the Linux man-pages project. A
162 description of the project, information about reporting bugs, and the
163 latest version of this page, can be found at
164 https://www.kernel.org/doc/man-pages/.
165
166
167
168Linux 2021-03-22 SETPGID(2)