OSDN Git Service

LDP: Update original to LDP v3.79
[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 2014-01-07 "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 .br
88     || /* Since glibc 2.19: */ _BSD_SOURCE
89 .fi
90 .sp
91 .BR setpgrp "()\ (BSD),"
92 .BR getpgrp "()\ (BSD) [before glibc 2.19]:"
93 .nf
94     _BSD_SOURCE &&
95         !\ (_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE ||
96            _XOPEN_SOURCE_EXTENDED || _GNU_SOURCE || _SVID_SOURCE)
97 .fi
98 .PD
99 .ad
100 .SH DESCRIPTION
101 All of these interfaces are available on Linux,
102 and are used for getting and setting the
103 process group ID (PGID) of a process.
104 The preferred, POSIX.1-specified ways of doing this are:
105 .BR getpgrp (void),
106 for retrieving the calling process's PGID; and
107 .BR setpgid (),
108 for setting a process's PGID.
109
110 .BR setpgid ()
111 sets the PGID of the process specified by
112 .I pid
113 to
114 .IR pgid .
115 If
116 .I pid
117 is zero, then the process ID of the calling process is used.
118 If
119 .I pgid
120 is zero, then the PGID of the process specified by
121 .I pid
122 is made the same as its process ID.
123 If
124 .BR setpgid ()
125 is used to move a process from one process
126 group to another (as is done by some shells when creating pipelines),
127 both process groups must be part of the same session (see
128 .BR setsid (2)
129 and
130 .BR credentials (7)).
131 In this case,
132 the \fIpgid\fP specifies an existing process group to be joined and the
133 session ID of that group must match the session ID of the joining process.
134
135 The POSIX.1 version of
136 .BR getpgrp (),
137 which takes no arguments,
138 returns the PGID of the calling process.
139
140 .BR getpgid ()
141 returns the PGID of the process specified by
142 .IR pid .
143 If
144 .I pid
145 is zero, the process ID of the calling process is used.
146 (Retrieving the PGID of a process other than the caller is rarely
147 necessary, and the POSIX.1
148 .BR getpgrp ()
149 is preferred for that task.)
150
151 The System\ V-style
152 .BR setpgrp (),
153 which takes no arguments, is equivalent to
154 .IR "setpgid(0,\ 0)" .
155
156 The BSD-specific
157 .BR setpgrp ()
158 call, which takes arguments
159 .I pid
160 and
161 .IR pgid ,
162 is is a wrapper function that calls
163
164     setpgid(pid, pgid)
165
166 .\" The true BSD setpgrp() system call differs in allowing the PGID
167 .\" to be set to arbitrary values, rather than being restricted to
168 .\" PGIDs in the same session.
169 Since glibc 2.19, the BSD-specific
170 .BR setpgrp ()
171 function is no longer exposed by
172 .IR <unistd.h> ;
173 calls should be replaced with the
174 .BR setpgid ()
175 call shown above.
176
177 The BSD-specific
178 .BR getpgrp ()
179 call, which takes a single
180 .I pid
181 argument, is a wrapper function that calls
182
183     getpgid(pid)
184
185 Since glibc 2.19, the BSD-specific
186 .BR getpgrp ()
187 function is no longer exposed by
188 .IR <unistd.h> ;
189 calls should be replaced with calls to the POSIX.1
190 .BR getpgrp ()
191 which takes no arguments (if the intent is to obtain the caller's PGID),
192 or with the
193 .BR getpgid ()
194 call shown above.
195 .SH RETURN VALUE
196 On success,
197 .BR setpgid ()
198 and
199 .BR setpgrp ()
200 return zero.
201 On error, \-1 is returned, and
202 .I errno
203 is set appropriately.
204
205 The POSIX.1
206 .BR getpgrp ()
207 always returns the PGID of the caller.
208
209 .BR getpgid (),
210 and the BSD-specific
211 .BR getpgrp ()
212 return a process group on success.
213 On error, \-1 is returned, and
214 .I errno
215 is set appropriately.
216 .SH ERRORS
217 .TP
218 .B EACCES
219 An attempt was made to change the process group ID
220 of one of the children of the calling process and the child had
221 already performed an
222 .BR execve (2)
223 .RB ( setpgid (),
224 .BR setpgrp ()).
225 .TP
226 .B EINVAL
227 .I pgid
228 is less than 0
229 .RB ( setpgid (),
230 .BR setpgrp ()).
231 .TP
232 .B EPERM
233 An attempt was made to move a process into a process group in a
234 different session, or to change the process
235 group ID of one of the children of the calling process and the
236 child was in a different session, or to change the process group ID of
237 a session leader
238 .RB ( setpgid (),
239 .BR setpgrp ()).
240 .TP
241 .B ESRCH
242 For
243 .BR getpgid ():
244 .I pid
245 does not match any process.
246 For
247 .BR setpgid ():
248 .I pid
249 is not the calling process and not a child of the calling process.
250 .SH CONFORMING TO
251 .BR setpgid ()
252 and the version of
253 .BR getpgrp ()
254 with no arguments
255 conform to POSIX.1-2001.
256
257 POSIX.1-2001 also specifies
258 .BR getpgid ()
259 and the version of
260 .BR setpgrp ()
261 that takes no arguments.
262 (POSIX.1-2008 marks this
263 .BR setpgrp ()
264 specification as obsolete.)
265
266 The version of
267 .BR getpgrp ()
268 with one argument and the version of
269 .BR setpgrp ()
270 that takes two arguments derive from 4.2BSD,
271 and are not specified by POSIX.1.
272 .SH NOTES
273 A child created via
274 .BR fork (2)
275 inherits its parent's process group ID.
276 The PGID is preserved across an
277 .BR execve (2).
278
279 Each process group is a member of a session and each process is a
280 member of the session of which its process group is a member.
281
282 A session can have a controlling terminal.
283 At any time, one (and only one) of the process groups
284 in the session can be the foreground process group
285 for the terminal;
286 the remaining process groups are in the background.
287 If a signal is generated from the terminal (e.g., typing the
288 interrupt key to generate
289 .BR SIGINT ),
290 that signal is sent to the foreground process group.
291 (See
292 .BR termios (3)
293 for a description of the characters that generate signals.)
294 Only the foreground process group may
295 .BR read (2)
296 from the terminal;
297 if a background process group tries to
298 .BR read (2)
299 from the terminal, then the group is sent a
300 .B SIGTTIN
301 signal, which suspends it.
302 The
303 .BR tcgetpgrp (3)
304 and
305 .BR tcsetpgrp (3)
306 functions are used to get/set the foreground
307 process group of the controlling terminal.
308
309 The
310 .BR setpgid ()
311 and
312 .BR getpgrp ()
313 calls are used by programs such as
314 .BR bash (1)
315 to create process groups in order to implement shell job control.
316
317 If a session has a controlling terminal, and the
318 .B CLOCAL
319 flag for that terminal is not set,
320 and a terminal hangup occurs, then the session leader is sent a
321 .BR SIGHUP .
322 If the session leader exits, then a
323 .B SIGHUP
324 signal will also be sent to each process in the foreground
325 process group of the controlling terminal.
326
327 If the exit of the process causes a process group to become orphaned,
328 and if any member of the newly orphaned process group is stopped, then a
329 .B SIGHUP
330 signal followed by a
331 .B SIGCONT
332 signal will be sent to each process
333 in the newly orphaned process group.
334 .\" exit.3 refers to the following text:
335 An orphaned process group is one in which the parent of
336 every member of process group is either itself also a member
337 of the process group or is a member of a process group
338 in a different session (see also
339 .BR credentials (7)).
340 .SH SEE ALSO
341 .BR getuid (2),
342 .BR setsid (2),
343 .BR tcgetpgrp (3),
344 .BR tcsetpgrp (3),
345 .BR termios (3),
346 .BR credentials (7)
347 .SH COLOPHON
348 This page is part of release 3.79 of the Linux
349 .I man-pages
350 project.
351 A description of the project,
352 information about reporting bugs,
353 and the latest version of this page,
354 can be found at
355 \%http://www.kernel.org/doc/man\-pages/.