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