OSDN Git Service

1593f12db881606ed75f53129209c886cc40eb82
[linuxjm/LDP_man-pages.git] / draft / man2 / wait.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (c) 1993 by Thomas Koenig <ig25@rz.uni-karlsruhe.de>
4 .\" and Copyright (c) 2004 by Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" License.
26 .\"
27 .\" Modified Sat Jul 24 13:30:06 1993 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified Sun Aug 21 17:42:42 1994 by Rik Faith <faith@cs.unc.edu>
29 .\"          (Thanks to Koen Holtman <koen@win.tue.nl>)
30 .\" Modified Wed May 17 15:54:12 1995 by Rik Faith <faith@cs.unc.edu>
31 .\"           To remove *'s from status in macros (Thanks to Michael Shields).
32 .\" Modified as suggested by Nick Duffek <nsd@bbc.com>, aeb, 960426
33 .\" Modified Mon Jun 23 14:09:52 1997 by aeb - add EINTR.
34 .\" Modified Thu Nov 26 02:12:45 1998 by aeb - add SIGCHLD stuff.
35 .\" Modified Mon Jul 24 21:37:38 2000 by David A. Wheeler
36 .\"          <dwheeler@dwheeler.com> - noted thread issues.
37 .\" Modified 26 Jun 01 by Michael Kerrisk
38 .\"          Added __WCLONE, __WALL, and __WNOTHREAD descriptions
39 .\" Modified 2001-09-25, aeb
40 .\" Modified 26 Jun 01 by Michael Kerrisk, <mtk.manpages@gmail.com>
41 .\"     Updated notes on setting disposition of SIGCHLD to SIG_IGN
42 .\" 2004-11-11, mtk
43 .\"     Added waitid(2); added WCONTINUED and WIFCONTINUED()
44 .\"     Added text on SA_NOCLDSTOP
45 .\"     Updated discussion of SA_NOCLDWAIT to reflect 2.6 behavior
46 .\"     Much other text rewritten
47 .\" 2005-05-10, mtk, __W* flags can't be used with waitid()
48 .\" 2008-07-04, mtk, removed erroneous text about SA_NOCLDSTOP
49 .\"
50 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya all rights reserved.
51 .\" Japanese Version Copyright (c) 2005 Akihiro MOTOKI all rights reserved.
52 .\" Translated 1997-03-04, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
53 .\" Modified 2000-09-23, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
54 .\" Updated 2001-06-25, Kentaro Shirakata <argrath@ub32.org>
55 .\" Updated 2001-10-15, Kentaro Shirakata <argrath@ub32.org>
56 .\" Updated 2001-12-13, Kentaro Shirakata <argrath@ub32.org>
57 .\" Updated 2002-10-21, Kentaro Shirakata <argrath@ub32.org>
58 .\" Updated 2003-01-27, Kentaro Shirakata <argrath@ub32.org>
59 .\" Updated 2003-09-12, Kentaro Shirakata <argrath@ub32.org>
60 .\" Updated 2005-03-19, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
61 .\" Updated 2005-12-26, Akihiro MOTOKI
62 .\" Updated 2008-07-31, Akihiro MOTOKI, LDP v3.05
63 .\"
64 .\"WORD:        signal handling         シグナル処理
65 .\"WORD:        suspend                 停止(suspend)
66 .\"WORD:        zombie                  ゾンビ(zombie)
67 .\"WORD:        child process           子プロセス
68 .\"WORD:        process group           プロセス・グループ
69 .\"WORD:        process group ID        プロセス・グループID
70 .\"WORD:        stop                    停止(stop)
71 .\"WORD:        effective user ID       実効ユーザーID
72 .\"WORD:        block                   禁止(block)
73 .\"
74 .TH WAIT 2 2010-09-26 "Linux" "Linux Programmer's Manual"
75 .\"O .SH NAME
76 .SH 名前
77 .\"O wait, waitpid, waitid \- wait for process to change state
78 wait, waitpid, waitid \- プロセスの状態変化を待つ
79 .\"O .SH SYNOPSIS
80 .SH 書式
81 .B #include <sys/types.h>
82 .br
83 .B #include <sys/wait.h>
84 .sp
85 .BI "pid_t wait(int *" "status" );
86
87 .BI "pid_t waitpid(pid_t " pid ", int *" status ", int " options );
88
89 .BI "int waitid(idtype_t " idtype ", id_t " id \
90 ", siginfo_t *" infop ", int " options );
91 .sp
92 .in -4n
93 .\"O Feature Test Macro Requirements for glibc (see
94 .\"O .BR feature_test_macros (7)):
95 glibc 向けの機能検査マクロの要件
96 .RB ( feature_test_macros (7)
97 参照):
98 .in
99 .sp
100 .ad l
101 .PD 0
102 .BR waitid ():
103 .RS 4
104 _SVID_SOURCE ||
105 _XOPEN_SOURCE\ >=\ 500 ||
106 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
107 .br
108 .\"O || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
109 || /* glibc 2.12 以降: */ _POSIX_C_SOURCE\ >=\ 200809L
110 .RE
111 .PD
112 .ad
113 .\"O .SH DESCRIPTION
114 .SH 説明
115 .\"O All of these system calls are used to wait for state changes
116 .\"O in a child of the calling process, and obtain information
117 .\"O about the child whose state has changed.
118 .\"O A state change is considered to be: the child terminated;
119 .\"O the child was stopped by a signal; or the child was resumed by a signal.
120 .\"O In the case of a terminated child, performing a wait allows
121 .\"O the system to release the resources associated with the child;
122 .\"O if a wait is not performed, then the terminated child remains in
123 .\"O a "zombie" state (see NOTES below).
124 これらのシステムコールはいずれも、呼び出し元プロセスの子プロセスの
125 状態変化を待ち、状態が変化したその子プロセスの情報を取得するのに
126 使用される。
127 状態変化とは以下のいずれかである:
128 子プロセスの終了、シグナルによる子プロセスの停止、
129 シグナルによる子プロセスの再開。
130 子プロセスが終了した場合は、wait を実行することで、
131 システムがその子プロセスに関連するリソースを解放できるようになる。
132 wait が実行されなかった場合には、終了した子プロセスは
133 「ゾンビ」状態で残り続ける (下記の注意の章を参照のこと)。
134
135 .\"O If a child has already changed state, then these calls return immediately.
136 .\"O Otherwise they block until either a child changes state or
137 .\"O a signal handler interrupts the call (assuming that system calls
138 .\"O are not automatically restarted using the
139 .\"O .B SA_RESTART
140 .\"O flag of
141 .\"O .BR sigaction (2)).
142 .\"O In the remainder of this page, a child whose state has changed
143 .\"O and which has not yet been waited upon by one of these system
144 .\"O calls is termed
145 .\"O .IR waitable .
146 子プロセスの状態変化がすでに発生していた場合、これらのコールは
147 すぐに復帰する。それ以外の場合は、子プロセスの状態変化が起こるか、
148 シグナルハンドラによりシステムコールが中断されるまで、
149 停止 (block) する (後者は、
150 .BR sigaction (2)
151
152 .B SA_RESTART
153 フラグによりシステムコールが自動的に再スタートするようになっていない
154 場合の動作である)。
155 以下の説明では、状態変化が起こったがこれらのシステムコールのいずれかに
156 よって待たれていない子プロセスを
157 .I waitable
158 (待ち可能) と呼ぶ。
159 .\"O .SS "wait() and waitpid()"
160 .SS "wait() と waitpid()"
161 .\"O The
162 .\"O .BR wait ()
163 .\"O system call suspends execution of the calling process until one of its
164 .\"O children terminates.
165 .\"O The call
166 .\"O .I wait(&status)
167 .\"O is equivalent to:
168 .\"O .nf
169 .\"O
170 .\"O     watpid(\-1, &status, 0);
171 .\"O .fi
172 .BR wait ()
173 システムコールは、子プロセスのいずれかが終了するまで
174 呼び出し元のプロセスの実行を一時停止する。
175 以下の二つの呼び出しは等価である:
176 .nf
177
178     wait(&status);
179
180     waitpid(\-1, &status, 0);
181 .fi
182
183 .\"O The
184 .\"O .BR waitpid ()
185 .\"O system call suspends execution of the calling process until a
186 .\"O child specified by
187 .\"O .I pid
188 .\"O argument has changed state.
189 .\"O By default,
190 .\"O .BR waitpid ()
191 .\"O waits only for terminated children, but this behavior is modifiable
192 .\"O via the
193 .\"O .I options
194 .\"O argument, as described below.
195 .BR waitpid ()
196 システムコールは、
197 .I pid
198 引き数で指定した子プロセスの状態変化が起こるまで、
199 呼び出し元のプロセスの実行を一時停止する。デフォルトでは、
200 .BR waitpid ()
201 は子プロセスの終了だけを待つが、この動作は
202 .I options
203 引き数により変更可能である。
204
205 .\"O The value of
206 .\"O .I pid
207 .\"O can be:
208 .I pid
209 に指定できる値は以下の通り:
210 .IP "< \-1"
211 .\"O meaning wait for any child process whose process group ID is
212 .\"O equal to the absolute value of
213 .\"O .IR pid .
214 プロセスグループID が
215 .I pid
216 の絶対値に等しい子プロセスのいずれかが終了するまでを待つ。
217 .IP \-1
218 .\"O meaning wait for any child process.
219 子プロセスのどれかが終了するまで待つ。
220 .IP 0
221 .\"O meaning wait for any child process whose process group ID is
222 .\"O equal to that of the calling process.
223 プロセスグループID が呼び出したプロセスのものと等しい
224 子プロセスを待つ。
225 .IP "> 0"
226 .\"O meaning wait for the child whose process ID is equal to the
227 .\"O value of
228 .\"O .IR pid .
229 プロセスID が
230 .I pid
231 に等しい子プロセスを待つ。
232 .PP
233 .\"O The value of
234 .\"O .I options
235 .\"O is an OR of zero or more of the following constants:
236 .I options
237 の値は次の定数の 0 個以上の論理和である:
238 .TP 12
239 .B WNOHANG
240 .\"O return immediately if no child has exited.
241 状態変化が起こった子プロセスがない場合にすぐに復帰する。
242 .TP
243 .B WUNTRACED
244 .\"O also return if a child has stopped
245 .\"O (but not traced via
246 .\"O .BR ptrace (2)).
247 .\"O Status for
248 .\"O .I traced
249 .\"O children which have stopped is provided
250 .\"O even if this option is not specified.
251 子プロセスが停止した場合にも復帰する (子プロセスが
252 .BR ptrace (2)
253 でトレースされている場合は除く)。
254 このオプションが指定されていない場合でも、停止したプロセスが
255 「トレース (traced)」されていれば、子プロセスの状態が報告される。
256 .TP
257 .\"O .BR WCONTINUED " (Since Linux 2.6.10)"
258 .BR WCONTINUED " (Linux 2.6.10 以降)"
259 .\"O also return if a stopped child has been resumed by delivery of
260 .\"O .BR SIGCONT .
261 停止した子プロセスが
262 .B SIGCONT
263 の配送により再開した場合にも復帰する。
264 .PP
265 .\"O (For Linux-only options, see below.)
266 (Linux 専用オプションについては後述する)
267 .PP
268 .\"O If
269 .\"O .I status
270 .\"O is not NULL,
271 .\"O .BR wait ()
272 .\"O and
273 .\"O .BR waitpid ()
274 .\"O store status information in the \fIint\fP to which it points.
275 .I status
276 が NULL でなければ、
277 .BR wait ()
278
279 .BR waitpid ()
280
281 .I status
282 で指す \fIint\fP に状態情報を格納する。
283 .\"O This integer can be inspected with the following macros (which
284 .\"O take the integer itself as an argument, not a pointer to it,
285 .\"O as is done in
286 .\"O .BR wait ()
287 .\"O and
288 .\"O .BR waitpid ()!):
289 この整数は以下のマクロを使って検査できる。
290 (これらのマクロの引き数には、
291 .BR wait ()
292
293 .BR waitpid ()
294 が書き込んだ整数そのものを指定する。ポインタではない!)
295 .TP
296 .BI WIFEXITED( status )
297 .\"O returns true if the child terminated normally, that is,
298 .\"O by calling
299 .\"O .BR exit (3)
300 .\"O or
301 .\"O .BR _exit (2),
302 .\"O or by returning from main().
303 子プロセスが正常に終了した場合に真を返す。
304 「正常に」とは、
305 .BR exit (3)
306
307 .BR _exit (2)
308 が呼び出された場合、もしくは
309 main() から復帰した場合である。
310 .TP
311 .BI WEXITSTATUS( status )
312 .\"O returns the exit status of the child.
313 .\"O This consists of the least significant 8 bits of the
314 .\"O .I status
315 .\"O argument that the child specified in a call to
316 .\"O .BR exit (3)
317 .\"O or
318 .\"O .BR _exit (2)
319 .\"O or as the argument for a return statement in main().
320 .\"O This macro should only be employed if
321 .\"O .B WIFEXITED
322 .\"O returned true.
323 子プロセスの終了ステータスを返す。
324 終了ステータスは
325 .I status
326 引き数の下位 8ビットで構成されており、
327 .BR exit (3)
328
329 .BR _exit (2)
330 の呼び出し時に渡された値、もしくは main() の return 文の
331 引き数として指定された値である。
332 このマクロを使用するのは
333 .B WIFEXITED
334 が真を返した場合だけにすべきである。
335 .TP
336 .BI WIFSIGNALED( status )
337 .\"O returns true if the child process was terminated by a signal.
338 子プロセスがシグナルにより終了した場合に真を返す。
339 .TP
340 .BI WTERMSIG( status )
341 .\"O returns the number of the signal that caused the child process to
342 .\"O terminate.
343 .\"O This macro should only be employed if
344 .\"O .B WIFSIGNALED
345 .\"O returned true.
346 子プロセス終了の原因となったシグナルの番号を返す。
347 このマクロを使用するのは
348 .B WIFSIGNALED
349 が真を返した場合だけにすべきである。
350 .TP
351 .BI WCOREDUMP( status )
352 .\"O returns true if the child produced a core dump.
353 .\"O This macro should only be employed if
354 .\"O .B WIFSIGNALED
355 .\"O returned true.
356 .\"O This macro is not specified in POSIX.1-2001 and is not available on
357 .\"O some UNIX implementations (e.g., AIX, SunOS).
358 .\"O Only use this enclosed in #ifdef WCOREDUMP ... #endif.
359 子プロセスがコアダンプを生成した場合に真を返す。
360 このマクロを使用するのは
361 .B WIFSIGNALED
362 が真を返した場合だけにすべきである。
363 このマクロは POSIX.1-2001 では規定されておらず、
364 (AIX, SunOS などの) いくつかの UNIX の実装では利用できない。
365 必ず #ifdef WCOREDUMP ... #endif で括って使用すること。
366 .TP
367 .BI WIFSTOPPED( status )
368 .\"O returns true if the child process was stopped by delivery of a signal;
369 .\"O this is only possible if the call was done using
370 .\"O .B WUNTRACED
371 .\"O or when the child is being traced (see
372 .\"O .BR ptrace (2)).
373 子プロセスがシグナルの配送により停止した場合に真を返す。
374 これが真になるのは、システムコールが
375 .B WUNTRACED
376 を指定して呼び出された場合か、子プロセスがトレースされている場合
377 .RB ( ptrace (2)
378 参照) だけである。
379 .TP
380 .BI WSTOPSIG( status )
381 .\"O returns the number of the signal which caused the child to stop.
382 .\"O This macro should only be employed if
383 .\"O .B WIFSTOPPED
384 .\"O returned true.
385 子プロセスを停止させたシグナルの番号を返す。
386 このマクロを使用するのは
387 .B WIFSTOPPED
388 が 0 以外を返した場合だけにすべきである。
389 .TP
390 .BI WIFCONTINUED( status )
391 .\"O (since Linux 2.6.10)
392 (Linux 2.6.10 以降)
393 .\"O returns true if the child process was resumed by delivery of
394 .\"O .BR SIGCONT .
395 子プロセスが
396 .B SIGCONT
397 の配送により再開した場合に真を返す。
398 .SS "waitid()"
399 .\"O The
400 .\"O .BR waitid ()
401 .\"O system call (available since Linux 2.6.9) provides more precise
402 .\"O control over which child state changes to wait for.
403 .BR waitid ()
404 システムコール (Linux 2.6.9 以降で利用可能) を使うと、
405 子プロセスのどの状態変化を待つかについてより細かな制御ができる。
406
407 .\"O The
408 .\"O .I idtype
409 .\"O and
410 .\"O .I id
411 .\"O arguments select the child(ren) to wait for, as follows:
412 引き数
413 .I idtype
414
415 .I id
416 でどの子プロセスを待つかを選択する:
417 .IP "\fIidtype\fP == \fBP_PID\fP"
418 .\"O Wait for the child whose process ID matches
419 .\"O .IR id .
420 プロセスID が
421 .I id
422 と一致する子プロセスを待つ。
423 .IP "\fIidtype\fP == \fBP_PGID\fP"
424 .\"O Wait for any child whose process group ID matches
425 .\"O .IR id .
426 プロセスグループID が
427 .I id
428 と一致する子プロセスを待つ。
429 .IP "\fIidtype\fP == \fBP_ALL\fP"
430 .\"O Wait for any child;
431 .\"O .I id
432 .\"O is ignored.
433 子プロセス全部を対象に待つ。
434 .I id
435 は無視される。
436 .PP
437 .\"O The child state changes to wait for are specified by ORing
438 .\"O one or more of the following flags in
439 .\"O .IR options :
440 子プロセスのどの状態変化を待つかは以下のフラグで指定する
441 .RI ( options
442 には 1個以上のフラグの論理和をとって指定する):
443 .TP 12
444 .B WEXITED
445 .\"O Wait for children that have terminated.
446 子プロセスの終了を待つ。
447 .TP
448 .B WSTOPPED
449 .\"O Wait for children that have been stopped by delivery of a signal.
450 子プロセスがシグナルの配送により停止するのを待つ。
451 .TP
452 .B WCONTINUED
453 .\"O Wait for (previously stopped) children that have been
454 .\"O resumed by delivery of
455 .\"O .BR SIGCONT .
456 (停止していた) 子プロセスが
457 .B SIGCONT
458 が配送されて再開するのを待つ。
459 .PP
460 .\"O The following flags may additionally be ORed in
461 .\"O .IR options :
462 さらに以下のフラグを論理和の形で
463 .I options
464 に指定できる:
465 .TP 12
466 .B WNOHANG
467 .\"O As for
468 .\"O .BR waitpid ().
469 .BR waitpid ()
470 と同様。
471 .TP
472 .B WNOWAIT
473 .\"O Leave the child in a waitable state; a later wait call
474 .\"O can be used to again retrieve the child status information.
475 waitable 状態のプロセスをそのままにする。この後で wait コールを
476 使って、同じ子プロセスの状態情報をもう一度取得することができる。
477 .PP
478 .\"O Upon successful return,
479 .\"O .BR waitid ()
480 .\"O fills in the following fields of the
481 .\"O .I siginfo_t
482 .\"O structure pointed to by
483 .\"O .IR infop :
484 成功した場合には、
485 .BR waitid ()
486
487 .I infop
488 が指す
489 .I siginfo_t
490 構造体の以下のフィールドを設定する:
491 .TP 12
492 \fIsi_pid\fP
493 .\"O The process ID of the child.
494 子プロセスのプロセスID。
495 .TP
496 \fIsi_uid\fP
497 .\"O The real user ID of the child.
498 .\"O (This field is not set on most other implementations.)
499 子プロセスの実ユーザID
500 (このフィールドは他のほとんどの実装では設定されない)。
501 .TP
502 \fIsi_signo\fP
503 .\"O Always set to
504 .\"O .BR SIGCHLD .
505 常に
506 .B SIGCHLD
507 が設定される。
508 .TP
509 \fIsi_status\fP
510 .\"O Either the exit status of the child, as given to
511 .\"O .BR _exit (2)
512 .\"O (or
513 .\"O .BR exit (3)),
514 .\"O or the signal that caused the child to terminate, stop, or continue.
515 .\"O The
516 .\"O .I si_code
517 .\"O field can be used to determine how to interpret this field.
518 .BR _exit (2)
519 (か
520 .BR exit (3))
521 に指定された子プロセスの終了ステータス、もしくは
522 子プロセスの終了、停止、再開の原因となったシグナルが設定される。
523 このフィールドをどう解釈するかは、
524 .I si_code
525 フィールドを参照して決めることができる。
526 .TP
527 \fIsi_code\fP
528 .\"O Set to one of:
529 .\"O .B CLD_EXITED
530 .\"O (child called
531 .\"O .BR _exit (2));
532 .\"O .B CLD_KILLED
533 .\"O (child killed by signal);
534 .\"O .B CLD_DUMPED
535 .\"O (child killed by signal, and dumped core);
536 .\"O .B CLD_STOPPED
537 .\"O (child stopped by signal);
538 .\"O .B CLD_TRAPPED
539 .\"O (traced child has trapped); or
540 .\"O .B CLD_CONTINUED
541 .\"O (child continued by
542 .\"O .BR SIGCONT ).
543 以下のいずれかが設定される:
544 .B CLD_EXITED
545 (子プロセスが
546 .BR _exit (2)
547 を呼び出した);
548 .B CLD_KILLED
549 (シグナルにより子プロセスが kill された);
550 .B CLD_DUMPED
551 (シグナルにより子プロセスが kill され、コア・ダンプが行われた);
552 .B CLD_STOPPED
553 (シグナルにより子プロセスが停止した);
554 .BR CLD_TRAPPED
555 (トレースされていた子プロセスがトラップを受信した);
556 .B CLD_CONTINUED
557 .RB ( SIGCONT
558 により子プロセスが再開された)。
559 .PP
560 .\"O If
561 .\"O .B WNOHANG
562 .\"O was specified in
563 .\"O .I options
564 .\"O and there were no children in a waitable state, then
565 .\"O .BR waitid ()
566 .\"O returns 0 immediately and
567 .\"O the state of the
568 .\"O .I siginfo_t
569 .\"O structure pointed to by
570 .\"O .I infop
571 .\"O is unspecified.
572 .B WNOHANG
573
574 .I options
575 に指定されていて、
576 waitable 状態の子プロセスがなかった場合には、
577 .BR waitid ()
578 はすぐに 0 を返す。このとき、
579 .I infop
580 が指す
581 .I siginfo_t
582 構造体の内容は不定である。
583 .\"O .\" POSIX.1-2001 leaves this possibility unspecified; most
584 .\"O .\" implementations (including Linux) zero out the structure
585 .\"O .\" in this case, but at at least one implementation (AIX 5.1)
586 .\"O .\" does not -- MTK Nov 04
587 .\" POSIX.1-2001 では、不定の可能性を残している。
588 .\" (Linux を含む) ほとんどの実装では、この場合に構造体を 0 で埋めるが、
589 .\" 少なくとも一つの実装 (AIX 5.1) ではそうならない。 -- MTK Nov 04
590 .\"O To distinguish this case from that where a child was in a
591 .\"O waitable state, zero out the
592 .\"O .I si_pid
593 .\"O field before the call and check for a nonzero value in this field
594 .\"O after the call returns.
595 この場合を waitable 状態の子プロセスがあった場合と区別するには、
596 .BR waitid ()
597 を呼び出す前に
598 .I si_pid
599 を 0 にしておき、コールが復帰した後でこのフィールドが 0 以外の値かどうか
600 をチェックすればよい。
601 .\"O .SH "RETURN VALUE"
602 .SH 返り値
603 .BR wait ():
604 .\"O on success, returns the process ID of the terminated child;
605 .\"O on error, \-1 is returned.
606 成功すると、終了した子プロセスのプロセスID を返す。
607 エラーの場合 \-1 を返す。
608
609 .BR waitpid ():
610 .\"O on success, returns the process ID of the child whose state has changed;
611 .\"O if
612 .\"O .B WNOHANG
613 .\"O was specified and one or more child(ren) specified by
614 .\"O.I pid
615 .\"O exist, but have not yet changed state, then 0 is returned.
616 .\"O On error, \-1 is returned.
617 成功すると、状態が変化した子プロセスのプロセスID を返す。
618 .B WNOHANG
619 が指定されていて、
620 .I pid
621 で指示された子プロセスが一つ以上存在するが、どの子プロセスでも
622 状態変化が起こっていなかった場合は、 0 を返す。
623 エラーの場合 \-1 を返す。
624
625 .BR waitid ():
626 .\"O returns 0 on success or
627 .\"O if
628 .\"O .B WNOHANG
629 .\"O was specified and no child(ren) specified by
630 .\"O .I id
631 .\"O has yet changed state;
632 .\"O on error, \-1 is returned.
633 成功すると 0 を返す。
634 .B WNOHANG
635 が指定されていて、
636 .I pid
637 で指示された子プロセスで状態変化が起こっていなかった場合にも
638 0 を返す。
639 エラーの場合 \-1 を返す。
640 .\" FIXME: As reported by Vegard Nossum, if infop is NULL, then waitid()
641 .\" returns the PID of the child.  Either this is a bug, or it is intended
642 .\" behavior that needs to be documented.  See my Jan 2009 LKML mail
643 .\" "waitid() return value strangeness when infop is NULL".
644 .\"O Each of these calls sets
645 .\"O .I errno
646 .\"O to an appropriate value in the case of an error.
647 エラーの場合、これらのシステムコールはいずれも
648 .I errno
649 に適切な値を設定する。
650 .\"O .SH ERRORS
651 .SH エラー
652 .TP
653 .\"O .B ECHILD
654 .\"O (for
655 .\"O .BR wait ())
656 .B ECHILD
657 .RB ( wait ()
658 の場合)
659 .\"O The calling process does not have any unwaited-for children.
660 呼び出し元プロセスには、wait を行っていない子プロセスはない。
661 .TP
662 .\"O .B ECHILD
663 .\"O (for
664 .\"O .BR waitpid ()
665 .\"O or
666 .\"O .BR waitid ())
667 .B ECHILD
668 .RB ( waitpid ()
669
670 .BR waitid ()
671 の場合)
672 .\"O The process specified by
673 .\"O .I pid
674 .\"O .RB ( waitpid ())
675 .\"O or
676 .\"O .I idtype
677 .\"O and
678 .\"O .I id
679 .\"O .RB ( waitid ())
680 .\"O does not exist or is not a child of the calling process.
681 .\"O (This can happen for one's own child if the action for
682 .\"O .B SIGCHLD
683 .\"O is set to
684 .\"O .BR SIG_IGN .
685 .\"O See also the \fILinux Notes\fP section about threads.)
686 .I pid
687 .RB ( waitpid ())
688
689 .I idtype
690
691 .I id
692 .RB ( waitid ())
693 で指定したプロセスが存在しないか、呼び出し元プロセスの子プロセスでない
694 .RB ( SIGCHLD
695 の動作に
696 .B SIG_IGN
697 を設定した場合には、自分自身の子プロセスでも起こりうる。
698 スレッドに関しては「Linux での注意」の節も参照すること)。
699 .TP
700 .B EINTR
701 .\"O .B WNOHANG
702 .\"O was not set and an unblocked signal or a
703 .\"O .B SIGCHLD
704 .\"O was caught; see.
705 .\"O .BR signal (7).
706 .B WNOHANG
707 が設定されておらず、禁止 (block) されていないシグナルや
708 .B SIGCHLD
709 を受信した。
710 .BR signal (7)
711 参照。
712 .TP
713 .B EINVAL
714 .\"O The
715 .\"O .I options
716 .\"O argument was invalid.
717 .I options
718 引き数が不正である。
719 .\"O .SH "CONFORMING TO"
720 .SH 準拠
721 SVr4, 4.3BSD, POSIX.1-2001.
722 .\"O .SH NOTES
723 .SH 注意
724 .\"O A child that terminates, but has not been waited for becomes a "zombie".
725 .\"O The kernel maintains a minimal set of information about the zombie
726 .\"O process (PID, termination status, resource usage information)
727 .\"O in order to allow the parent to later perform a wait to obtain
728 .\"O information about the child.
729 .\"O As long as a zombie is not removed from the system via a wait,
730 .\"O it will consume a slot in the kernel process table, and if
731 .\"O this table fills, it will not be possible to create further processes.
732 .\"O If a parent process terminates, then its "zombie" children (if any)
733 .\"O are adopted by
734 .\"O .BR init (8),
735 .\"O which automatically performs a wait to remove the zombies.
736 終了したが、wait されていない子プロセスは「ゾンビ」になる。
737 後で親プロセスが wait を実行して子プロセスについての情報を取得できるように、
738 カーネルはゾンビプロセスについて最小限の情報 (PID、終了ステータス、
739 リソース使用状況) を保持する。
740 ゾンビプロセスは、 wait によってシステムから削除されない限り、
741 カーネルのプロセステーブルの 1 エントリを消費する。このプロセステーブルが
742 一杯になると、新たにプロセスを作ることができなくなる。
743 親プロセスが終了すると、その親プロセスの「ゾンビ」の
744 子プロセスは (もしあれば)
745 .BR init (8)
746 の養子となる。
747 .BR init (8)
748 は wait を自動的に実行し、ゾンビを削除する。
749
750 .\"O POSIX.1-2001 specifies that if the disposition of
751 .\"O .B SIGCHLD
752 .\"O is set to
753 .\"O .B SIG_IGN
754 .\"O or the
755 .\"O .B SA_NOCLDWAIT
756 .\"O flag is set for
757 .\"O .B SIGCHLD
758 .\"O (see
759 .\"O .BR sigaction (2)),
760 .\"O then children that terminate do not become zombies and a call to
761 .\"O .BR wait ()
762 .\"O or
763 .\"O .BR waitpid ()
764 .\"O will block until all children have terminated, and then fail with
765 .\"O .I errno
766 .\"O set to
767 .\"O .BR ECHILD .
768 POSIX.1-2001 では以下のように規定されている。
769 .B SIGCHLD
770 の動作が
771 .B SIG_IGN
772 に設定されたか、
773 .B SIGCHLD
774 に対して
775 .B SA_NOCLDWAIT
776 フラグが設定された場合
777 .RB ( sigaction (2)
778 参照)、終了した子プロセスはゾンビにはならず、
779 .BR wait ()
780
781 .BR waitpid ()
782 の呼び出しは全ての子プロセスが終了するまで停止し、
783 子プロセスが全部終了した後
784 .I errno
785
786 .B ECHILD
787 を設定して失敗する。
788 .\"O (The original POSIX standard left the behavior of setting
789 .\"O .B SIGCHLD
790 .\"O to
791 .\"O .B SIG_IGN
792 .\"O unspecified.
793 .\"O Note that even though the default disposition of
794 .\"O .B SIGCHLD
795 .\"O is "ignore", explicitly setting the disposition to
796 .\"O .B SIG_IGN
797 .\"O results in different treatment of zombie process children.)
798 (もともとの POSIX 標準は
799 .B SIGCHLD
800
801 .B SIG_IGN
802 を設定した場合の振る舞いを未規定のままにしている。
803 .B SIGCHLD
804 のデフォルトの動作が「無視」であるにもかかわらず、
805 .B SIGCHLD
806 の動作として
807 .B SIG_IGN
808 を明示的に設定した場合にはゾンビプロセスの子プロセスの扱いが
809 異なる点に注意すること。)
810 .\"O Linux 2.6 conforms to this specification.
811 .\"O However, Linux 2.4 (and earlier) does not:
812 Linux 2.6 はこの仕様に準拠している。
813 しかし、Linux 2.4 (とそれ以前のバージョン) はそうではない:
814 .\"O if a
815 .\"O .BR wait ()
816 .\"O or
817 .\"O .BR waitpid ()
818 .\"O call is made while
819 .\"O .B SIGCHLD
820 .\"O is being ignored, the call behaves just as though
821 .\"O .B SIGCHLD
822 .\"O were not being ignored, that is, the call blocks until the next child
823 .\"O terminates and then returns the process ID and status of that child.
824 .B SIGCHLD
825 が無視される状態で
826 .BR wait ()
827 または
828 .BR waitpid ()
829 が呼び出された場合、
830 .B SIGCHLD
831 が無視されていないかのように振る舞う。
832 つまり、呼び出しによって次の子プロセスの終了までブロックされ、
833 終了した子プロセスの PID と状態が返される。
834 .\"O .SS Linux Notes
835 .SS Linux での注意
836 .\"O In the Linux kernel, a kernel-scheduled thread is not a distinct
837 .\"O construct from a process.
838 .\"O Instead, a thread is simply a process
839 .\"O that is created using the Linux-unique
840 .\"O .BR clone (2)
841 .\"O system call; other routines such as the portable
842 .\"O .BR pthread_create (3)
843 .\"O call are implemented using
844 .\"O .BR clone (2);
845 .\"O they cannot be used with
846 .\"O .BR waitid ():
847 Linux カーネルでは、カーネルによってスケジュールされるスレッドは
848 プロセスと明確に区別できる構成要素ではない。スレッドは Linux 固有の
849 .BR clone (2)
850 システムコールを使用して生成されるプロセスに過ぎない。
851 移植性のある
852 .BR pthread_create (3)
853 コールのような他のルーチンは
854 .BR clone (2)
855 を使用して実装されている;
856 これらでは
857 .BR waitid ()
858 を使うことはできない。
859 .\"O Before Linux 2.4, a thread was just a special case of a process,
860 .\"O and as a consequence one thread could not wait on the children
861 .\"O of another thread, even when the latter belongs to the same thread group.
862 Linux 2.4 より前では、スレッドは単に特殊なプロセスであったので、
863 例え同じスレッドグループであっても、
864 あるスレッドが別のスレッドの子プロセスが終了するのを待つことは出来なかった。
865 .\"O However, POSIX prescribes such functionality, and since Linux 2.4
866 .\"O a thread can, and by default will, wait on children of other threads
867 .\"O in the same thread group.
868 しかし、POSIX ではこのような機能を規定しており、
869 Linux 2.4 以降では、あるスレッドが同じスレッドグループの他のスレッドの
870 子プロセスが終了するのを待つことができるようになった。
871 そして将来はこれがデフォルトの動作になるであろう。
872 .LP
873 .\"O The following Linux-specific
874 .\"O .I options
875 .\"O are for use with children created using
876 .\"O .BR clone (2).
877 .BR clone (2)
878 を用いて作られた子プロセスには、以下の Linux 固有の
879 .I options
880 が使用できる。
881 .TP
882 .B __WCLONE
883 .\" since 0.99pl10
884 .\"O Wait for "clone" children only.
885 .\"O If omitted then wait
886 .\"O for "non-clone" children only.
887 .\"O (A "clone" child is one
888 .\"O which delivers no signal, or a signal other than
889 .\"O .B SIGCHLD
890 .\"O to its parent upon termination.)
891 "clone" な子プロセスだけを待つ。
892 指定されなかった場合は非 "clone" な子プロセスだけを待つ
893 ("clone" な子プロセスは、終了時に親プロセスへ全くシグナルを送らないか、
894 .B SIGCHLD
895 以外のシグナルを送る)。
896 .\"O This option is ignored if
897 .\"O .B __WALL
898 .\"O is also specified.
899 このオプションは
900 .B __WALL
901 も指定された場合は無視される。
902 .TP
903 .\"O .BR __WALL " (since Linux 2.4)"
904 .BR __WALL " (Linux 2.4 以降)"
905 .\" since patch-2.3.48
906 .\"O Wait for all children, regardless of
907 .\"O type ("clone" or "non-clone").
908 "clone" であるかないかに関わらず、
909 全ての子プロセスを待つ。
910 .TP
911 .\"O .BR __WNOTHREAD " (since Linux 2.4)"
912 .BR __WNOTHREAD " (Linux 2.4 以降)"
913 .\" since patch-2.4.0-test8
914 .\"O Do not wait for children of other threads in
915 .\"O the same thread group.
916 .\"O This was the default before Linux 2.4.
917 同じスレッドグループの他のスレッドの子プロセスは待たない。
918 Linux 2.4 より前ではデフォルトであった。
919 .\"O .SH EXAMPLE
920 .SH 例
921 .\"O .\" fork.2 refers to this example program.
922 .\" fork.2 もこのサンプルプログラムを参照している。
923 .\"O The following program demonstrates the use of
924 .\"O .BR fork (2)
925 .\"O and
926 .\"O .BR waitpid ().
927 .\"O The program creates a child process.
928 .\"O If no command-line argument is supplied to the program,
929 .\"O then the child suspends its execution using
930 .\"O .BR pause (2),
931 .\"O to allow the user to send signals to the child.
932 .\"O Otherwise, if a command-line argument is supplied,
933 .\"O then the child exits immediately,
934 .\"O using the integer supplied on the command line as the exit status.
935 .\"O The parent process executes a loop that monitors the child using
936 .\"O .BR waitpid (),
937 .\"O and uses the W*() macros described above to analyze the wait status value.
938 以下のプログラムは、
939 .BR fork (2)
940
941 .BR waitpid ()
942 の使用方法の例を示している。
943 このプログラムでは子プロセスを生成する。
944 コマンドライン引き数が指定されなかったときは、
945 子プロセスは
946 .BR pause (2)
947 を使ってその実行を一時停止し、ユーザがその子プロセスに
948 シグナルを送信できるようにする。
949 コマンドライン引き数が指定された場合は、
950 子プロセスは直ちに終了し、
951 コマンドラインで指定された整数を終了ステータスとして使用する。
952 親プロセスは、
953 .BR waitpid ()
954 を使って子プロセスを監視し、
955 wait のステータス値を上記の W*() マクロを使って解析するという
956 ループを実行する。
957
958 .\"O The following shell session demonstrates the use of the program:
959 下記ののシェル・セッションはプログラムの実行例である:
960 .in +4n
961 .nf
962
963 .RB "$" " ./a.out &"
964 Child PID is 32360
965 [1] 32359
966 .RB "$" " kill \-STOP 32360"
967 stopped by signal 19
968 .RB "$" " kill \-CONT 32360"
969 continued
970 .RB "$" " kill \-TERM 32360"
971 killed by signal 15
972 [1]+  Done                    ./a.out
973 $
974 .fi
975 .in
976 .\"O .SS Program source
977 .SS プログラムのソース
978 \&
979 .nf
980 #include <sys/wait.h>
981 #include <stdlib.h>
982 #include <unistd.h>
983 #include <stdio.h>
984
985 int
986 main(int argc, char *argv[])
987 {
988     pid_t cpid, w;
989     int status;
990
991     cpid = fork();
992     if (cpid == \-1) {
993         perror("fork");
994         exit(EXIT_FAILURE);
995     }
996
997     if (cpid == 0) {            /* Code executed by child */
998         printf("Child PID is %ld\\n", (long) getpid());
999         if (argc == 1)
1000             pause();                    /* Wait for signals */
1001         _exit(atoi(argv[1]));
1002
1003     } else {                    /* Code executed by parent */
1004         do {
1005             w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
1006             if (w == \-1) {
1007                 perror("waitpid");
1008                 exit(EXIT_FAILURE);
1009             }
1010
1011             if (WIFEXITED(status)) {
1012                 printf("exited, status=%d\\n", WEXITSTATUS(status));
1013             } else if (WIFSIGNALED(status)) {
1014                 printf("killed by signal %d\\n", WTERMSIG(status));
1015             } else if (WIFSTOPPED(status)) {
1016                 printf("stopped by signal %d\\n", WSTOPSIG(status));
1017             } else if (WIFCONTINUED(status)) {
1018                 printf("continued\\n");
1019             }
1020         } while (!WIFEXITED(status) && !WIFSIGNALED(status));
1021         exit(EXIT_SUCCESS);
1022     }
1023 }
1024 .fi
1025 .\"O .SH "SEE ALSO"
1026 .SH 関連項目
1027 .BR _exit (2),
1028 .BR clone (2),
1029 .BR fork (2),
1030 .BR kill (2),
1031 .BR ptrace (2),
1032 .BR sigaction (2),
1033 .BR signal (2),
1034 .BR wait4 (2),
1035 .BR pthread_create (3),
1036 .BR credentials (7),
1037 .BR signal (7)