OSDN Git Service

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