OSDN Git Service

(split) LDP: Update original to LDP v3.53.
[linuxjm/LDP_man-pages.git] / original / man2 / setpgid.2
1 .\" Copyright (c) 1983, 1991 Regents of the University of California.
2 .\" and Copyright (C) 2007, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" All rights reserved.
4 .\"
5 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\"    must display the following acknowledgement:
16 .\"     This product includes software developed by the University of
17 .\"     California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\"    may be used to endorse or promote products derived from this software
20 .\"    without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\" %%%LICENSE_END
34 .\"
35 .\"     @(#)getpgrp.2   6.4 (Berkeley) 3/10/91
36 .\"
37 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
38 .\" Modified 1995-04-15 by Michael Chastain <mec@shell.portal.com>:
39 .\"   Added 'getpgid'.
40 .\" Modified 1996-07-21 by Andries Brouwer <aeb@cwi.nl>
41 .\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com>
42 .\" Modified 1999-09-02 by Michael Haardt <michael@moria.de>
43 .\" Modified 2002-01-18 by Michael Kerrisk <mtk.manpages@gmail.com>
44 .\" Modified 2003-01-20 by Andries Brouwer <aeb@cwi.nl>
45 .\" 2007-07-25, mtk, fairly substantial rewrites and rearrangements
46 .\" of text.
47 .\"
48 .TH SETPGID 2 2013-07-31 "Linux" "Linux Programmer's Manual"
49 .SH NAME
50 setpgid, getpgid, setpgrp, getpgrp \- set/get process group
51 .SH SYNOPSIS
52 .B #include <unistd.h>
53 .sp
54 .BI "int setpgid(pid_t " pid ", pid_t " pgid );
55 .br
56 .BI "pid_t getpgid(pid_t " pid );
57 .sp
58 .BR "pid_t getpgrp(void);" "                 /* POSIX.1 version */"
59 .br
60 .BI "pid_t getpgrp(pid_t " pid ");\ \ \ \ \ \ \ \ \ \ \ "
61 /* BSD version */
62 .sp
63 .BR "int setpgrp(void);" "                   /* System V version */"
64 .br
65 .BI "int setpgrp(pid_t " pid ", pid_t " pgid ");\ "
66 /* BSD version */
67 .sp
68 .in -4n
69 Feature Test Macro Requirements for glibc (see
70 .BR feature_test_macros (7)):
71 .in
72 .sp
73 .ad l
74 .PD 0
75 .BR getpgid ():
76 .RS 4
77 _XOPEN_SOURCE\ >=\ 500 ||
78 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
79 .br
80 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
81 .RE
82 .sp
83 .BR setpgrp "() (POSIX.1):"
84 .nf
85     _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
86     _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
87 .fi
88 .sp
89 .BR setpgrp "()\ (BSD),"
90 .BR getpgrp "()\ (BSD):"
91 .nf
92     _BSD_SOURCE &&
93         !\ (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE ||
94            _XOPEN_SOURCE_EXTENDED || _GNU_SOURCE || _SVID_SOURCE)
95 .fi
96 .PD
97 .ad
98 .SH DESCRIPTION
99 All of these interfaces are available on Linux,
100 and are used for getting and setting the
101 process group ID (PGID) of a process.
102 The preferred, POSIX.1-specified ways of doing this are:
103 .BR getpgrp (void),
104 for retrieving the calling process's PGID; and
105 .BR setpgid (),
106 for setting a process's PGID.
107
108 .BR setpgid ()
109 sets the PGID of the process specified by
110 .I pid
111 to
112 .IR pgid .
113 If
114 .I pid
115 is zero, then the process ID of the calling process is used.
116 If
117 .I pgid
118 is zero, then the PGID of the process specified by
119 .I pid
120 is made the same as its process ID.
121 If
122 .BR setpgid ()
123 is used to move a process from one process
124 group to another (as is done by some shells when creating pipelines),
125 both process groups must be part of the same session (see
126 .BR setsid (2)
127 and
128 .BR credentials (7)).
129 In this case,
130 the \fIpgid\fP specifies an existing process group to be joined and the
131 session ID of that group must match the session ID of the joining process.
132
133 The POSIX.1 version of
134 .BR getpgrp (),
135 which takes no arguments,
136 returns the PGID of the calling process.
137
138 .BR getpgid ()
139 returns the PGID of the process specified by
140 .IR pid .
141 If
142 .I pid
143 is zero, the process ID of the calling process is used.
144 (Retrieving the PGID of a process other than the caller is rarely
145 necessary, and the POSIX.1
146 .BR getpgrp ()
147 is preferred for that task.)
148
149 The System V-style
150 .BR setpgrp (),
151 which takes no arguments, is equivalent to
152 .IR "setpgid(0,\ 0)" .
153
154 The BSD-specific
155 .BR setpgrp ()
156 call, which takes arguments
157 .I pid
158 and
159 .IR pgid ,
160 is equivalent to
161 .IR "setpgid(pid, pgid)" .
162 .\" The true BSD setpgrp() system call differs in allowing the PGID
163 .\" to be set to arbitrary values, rather than being restricted to
164 .\" PGIDs in the same session.
165
166 The BSD-specific
167 .BR getpgrp ()
168 call, which takes a single
169 .I pid
170 argument, is equivalent to
171 .IR "getpgid(pid)" .
172 .SH RETURN VALUE
173 On success,
174 .BR setpgid ()
175 and
176 .BR setpgrp ()
177 return zero.
178 On error, \-1 is returned, and
179 .I errno
180 is set appropriately.
181
182 The POSIX.1
183 .BR getpgrp ()
184 always returns the PGID of the caller.
185
186 .BR getpgid (),
187 and the BSD-specific
188 .BR getpgrp ()
189 return a process group on success.
190 On error, \-1 is returned, and
191 .I errno
192 is set appropriately.
193 .SH ERRORS
194 .TP
195 .B EACCES
196 An attempt was made to change the process group ID
197 of one of the children of the calling process and the child had
198 already performed an
199 .BR execve (2)
200 .RB ( setpgid (),
201 .BR setpgrp ()).
202 .TP
203 .B EINVAL
204 .I pgid
205 is less than 0
206 .RB ( setpgid (),
207 .BR setpgrp ()).
208 .TP
209 .B EPERM
210 An attempt was made to move a process into a process group in a
211 different session, or to change the process
212 group ID of one of the children of the calling process and the
213 child was in a different session, or to change the process group ID of
214 a session leader
215 .RB ( setpgid (),
216 .BR setpgrp ()).
217 .TP
218 .B ESRCH
219 For
220 .BR getpgid ():
221 .I pid
222 does not match any process.
223 For
224 .BR setpgid ():
225 .I pid
226 is not the calling process and not a child of the calling process.
227 .SH CONFORMING TO
228 .BR setpgid ()
229 and the version of
230 .BR getpgrp ()
231 with no arguments
232 conform to POSIX.1-2001.
233
234 POSIX.1-2001 also specifies
235 .BR getpgid ()
236 and the version of
237 .BR setpgrp ()
238 that takes no arguments.
239 (POSIX.1-2008 marks this
240 .BR setpgrp ()
241 specification as obsolete.)
242
243 The version of
244 .BR getpgrp ()
245 with one argument and the version of
246 .BR setpgrp ()
247 that takes two arguments derive from 4.2BSD,
248 and are not specified by POSIX.1.
249 .SH NOTES
250 A child created via
251 .BR fork (2)
252 inherits its parent's process group ID.
253 The PGID is preserved across an
254 .BR execve (2).
255
256 Each process group is a member of a session and each process is a
257 member of the session of which its process group is a member.
258
259 A session can have a controlling terminal.
260 At any time, one (and only one) of the process groups
261 in the session can be the foreground process group
262 for the terminal;
263 the remaining process groups are in the background.
264 If a signal is generated from the terminal (e.g., typing the
265 interrupt key to generate
266 .BR SIGINT ),
267 that signal is sent to the foreground process group.
268 (See
269 .BR termios (3)
270 for a description of the characters that generate signals.)
271 Only the foreground process group may
272 .BR read (2)
273 from the terminal;
274 if a background process group tries to
275 .BR read (2)
276 from the terminal, then the group is sent a
277 .B SIGTTIN
278 signal, which suspends it.
279 The
280 .BR tcgetpgrp (3)
281 and
282 .BR tcsetpgrp (3)
283 functions are used to get/set the foreground
284 process group of the controlling terminal.
285
286 The
287 .BR setpgid ()
288 and
289 .BR getpgrp ()
290 calls are used by programs such as
291 .BR bash (1)
292 to create process groups in order to implement shell job control.
293
294 If a session has a controlling terminal, and the
295 .B CLOCAL
296 flag for that terminal is not set,
297 and a terminal hangup occurs, then the session leader is sent a
298 .BR SIGHUP .
299 If the session leader exits, then a
300 .B SIGHUP
301 signal will also be sent to each process in the foreground
302 process group of the controlling terminal.
303
304 If the exit of the process causes a process group to become orphaned,
305 and if any member of the newly orphaned process group is stopped, then a
306 .B SIGHUP
307 signal followed by a
308 .B SIGCONT
309 signal will be sent to each process
310 in the newly orphaned process group.
311 .\" exit.3 refers to the following text:
312 An orphaned process group is one in which the parent of
313 every member of process group is either itself also a member
314 of the process group or is a member of a process group
315 in a different session (see also
316 .BR credentials (7)).
317 .SH SEE ALSO
318 .BR getuid (2),
319 .BR setsid (2),
320 .BR tcgetpgrp (3),
321 .BR tcsetpgrp (3),
322 .BR termios (3),
323 .BR credentials (7)