1SETPGID(2) Linux Programmer's Manual SETPGID(2)
2
3
4
6 setpgid, getpgid, setpgrp, getpgrp - set/get process group
7
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 int setpgid(pid_t pid, pid_t pgid);
13 pid_t getpgid(pid_t pid);
14
15 pid_t getpgrp(void); /* POSIX.1 version */
16 pid_t getpgrp(pid_t pid); /* BSD version */
17
18 int setpgrp(void); /* System V version */
19 int setpgrp(pid_t pid, pid_t pgid); /* BSD version */
20
21 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
22
23 getpgid():
24 _XOPEN_SOURCE >= 500
25 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
26
27 setpgrp() (POSIX.1):
28 _XOPEN_SOURCE >= 500
29 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
30 || /* Glibc versions <= 2.19: */ _SVID_SOURCE
31
32 setpgrp() (BSD), getpgrp() (BSD):
33 [These are available only before glibc 2.19]
34 _BSD_SOURCE &&
35 ! (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE ||
36 _GNU_SOURCE || _SVID_SOURCE)
37
39 All of these interfaces are available on Linux, and are used for get‐
40 ting and setting the process group ID (PGID) of a process. The pre‐
41 ferred, POSIX.1-specified ways of doing this are: getpgrp(void), for
42 retrieving the calling process's PGID; and setpgid(), for setting a
43 process's PGID.
44
45 setpgid() sets the PGID of the process specified by pid to pgid. If
46 pid is zero, then the process ID of the calling process is used. If
47 pgid is zero, then the PGID of the process specified by pid is made the
48 same as its process ID. If setpgid() is used to move a process from
49 one process group to another (as is done by some shells when creating
50 pipelines), both process groups must be part of the same session (see
51 setsid(2) and credentials(7)). In this case, the pgid specifies an ex‐
52 isting process group to be joined and the session ID of that group must
53 match the session ID of the joining process.
54
55 The POSIX.1 version of getpgrp(), which takes no arguments, returns the
56 PGID of the calling process.
57
58 getpgid() returns the PGID of the process specified by pid. If pid is
59 zero, the process ID of the calling process is used. (Retrieving the
60 PGID of a process other than the caller is rarely necessary, and the
61 POSIX.1 getpgrp() is preferred for that task.)
62
63 The System V-style setpgrp(), which takes no arguments, is equivalent
64 to setpgid(0, 0).
65
66 The BSD-specific setpgrp() call, which takes arguments pid and pgid, is
67 a wrapper function that calls
68
69 setpgid(pid, pgid)
70
71 Since glibc 2.19, the BSD-specific setpgrp() function is no longer ex‐
72 posed by <unistd.h>; calls should be replaced with the setpgid() call
73 shown above.
74
75 The BSD-specific getpgrp() call, which takes a single pid argument, is
76 a wrapper function that calls
77
78 getpgid(pid)
79
80 Since glibc 2.19, the BSD-specific getpgrp() function is no longer ex‐
81 posed by <unistd.h>; calls should be replaced with calls to the POSIX.1
82 getpgrp() which takes no arguments (if the intent is to obtain the
83 caller's PGID), or with the getpgid() call shown above.
84
86 On success, setpgid() and setpgrp() return zero. On error, -1 is re‐
87 turned, and errno is set appropriately.
88
89 The POSIX.1 getpgrp() always returns the PGID of the caller.
90
91 getpgid(), and the BSD-specific getpgrp() return a process group on
92 success. On error, -1 is returned, and errno is set appropriately.
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.10 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 2017-09-15 SETPGID(2)