2004-11-03 13:51:07 +00:00
|
|
|
.\" Hey Emacs! This file is -*- nroff -*- source.
|
|
|
|
.\"
|
2006-02-15 23:15:29 +00:00
|
|
|
.\" Copyright (c) 1993 by Thomas Koenig <ig25@rz.uni-karlsruhe.de>
|
2006-05-22 23:52:24 +00:00
|
|
|
.\" and Copyright (c) 2004 by Michael Kerrisk <mtk-manpages@gmx.net>
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\" License.
|
|
|
|
.\"
|
|
|
|
.\" Modified Sat Jul 24 13:30:06 1993 by Rik Faith <faith@cs.unc.edu>
|
|
|
|
.\" Modified Sun Aug 21 17:42:42 1994 by Rik Faith <faith@cs.unc.edu>
|
|
|
|
.\" (Thanks to Koen Holtman <koen@win.tue.nl>)
|
|
|
|
.\" Modified Wed May 17 15:54:12 1995 by Rik Faith <faith@cs.unc.edu>
|
|
|
|
.\" To remove *'s from status in macros (Thanks to Michael Shields).
|
|
|
|
.\" Modified as suggested by Nick Duffek <nsd@bbc.com>, aeb, 960426
|
|
|
|
.\" Modified Mon Jun 23 14:09:52 1997 by aeb - add EINTR.
|
|
|
|
.\" Modified Thu Nov 26 02:12:45 1998 by aeb - add SIGCHLD stuff.
|
|
|
|
.\" Modified Mon Jul 24 21:37:38 2000 by David A. Wheeler
|
|
|
|
.\" <dwheeler@dwheeler.com> - noted thread issues.
|
|
|
|
.\" Modified 26 Jun 01 by Michael Kerrisk
|
|
|
|
.\" Added __WCLONE, __WALL, and __WNOTHREAD descriptions
|
|
|
|
.\" Modified 2001-09-25, aeb
|
2004-11-03 14:43:40 +00:00
|
|
|
.\" Modified 26 Jun 01 by Michael Kerrisk, <mtk-manpages@gmx.net>
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Updated notes on setting disposition of SIGCHLD to SIG_IGN
|
2004-12-10 16:44:25 +00:00
|
|
|
.\" 2004-11-11, mtk
|
|
|
|
.\" Added waitid(2); added WCONTINUED and WIFCONTINUED()
|
2004-11-11 14:17:30 +00:00
|
|
|
.\" Added text on SA_NOCLDSTOP
|
|
|
|
.\" Updated discussion of SA_NOCLDWAIT to reflect 2.6 behaviour
|
|
|
|
.\" Much other text rewritten
|
2005-05-10 17:16:28 +00:00
|
|
|
.\" 2005-05-10, mtk, __W* flags can't be used with waitid()
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
2004-11-11 14:17:30 +00:00
|
|
|
.TH WAIT 2 2004-11-11 "Linux" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
2007-05-11 23:07:02 +00:00
|
|
|
wait, waitpid, waitid \- wait for process to change state
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #include <sys/types.h>
|
|
|
|
.br
|
|
|
|
.B #include <sys/wait.h>
|
|
|
|
.sp
|
|
|
|
.BI "pid_t wait(int *" "status" );
|
|
|
|
.br
|
|
|
|
.BI "pid_t waitpid(pid_t " pid ", int *" status ", int " options );
|
2004-11-11 14:17:30 +00:00
|
|
|
.br
|
2006-05-31 22:16:55 +00:00
|
|
|
.BI "int waitid(idtype_t " idtype ", id_t " id \
|
|
|
|
", siginfo_t *" infop ", int " options );
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
2004-11-11 14:17:30 +00:00
|
|
|
All of these system calls are used to wait for state changes
|
|
|
|
in a child of the calling process, and obtain information
|
|
|
|
about the child whose state has changed.
|
|
|
|
A state change is considered to be: the child terminated;
|
|
|
|
the child was stopped by a signal; or the child was resumed by a signal.
|
|
|
|
In the case of a terminated child, performing a wait allows
|
|
|
|
the system to release the resources associated with the child;
|
|
|
|
if a wait is not performed, then terminated the child remains in
|
|
|
|
a "zombie" state (see NOTES below).
|
|
|
|
|
|
|
|
If a child has already changed state, then these calls return immediately.
|
|
|
|
Otherwise they block until either a child changes state or
|
|
|
|
a signal handler interrupts the call (assuming that system calls
|
|
|
|
are not automatically restarted using the
|
|
|
|
.B SA_RESTART
|
|
|
|
flag of
|
|
|
|
.BR sigaction (2)).
|
|
|
|
In the remainder of this page, a child whose state has changed
|
2007-04-12 22:42:49 +00:00
|
|
|
and which has not yet been waited upon by one of these system
|
2004-11-11 14:40:35 +00:00
|
|
|
calls is termed
|
2004-11-11 14:17:30 +00:00
|
|
|
.IR waitable .
|
|
|
|
.SS "wait() and waitpid()"
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
2004-11-11 14:17:30 +00:00
|
|
|
.BR wait ()
|
|
|
|
system call suspends execution of the current process until one of its
|
|
|
|
children terminates.
|
|
|
|
The call
|
|
|
|
.I wait(&status)
|
|
|
|
is equivalent to:
|
|
|
|
.nf
|
|
|
|
|
2005-07-06 12:57:38 +00:00
|
|
|
waitpid(\-1, &status, 0);
|
2004-11-11 14:17:30 +00:00
|
|
|
.fi
|
2004-11-03 13:51:07 +00:00
|
|
|
|
|
|
|
The
|
2004-11-11 14:17:30 +00:00
|
|
|
.BR waitpid ()
|
|
|
|
system call suspends execution of the current process until a
|
|
|
|
child specified by
|
2004-11-03 13:51:07 +00:00
|
|
|
.I pid
|
2004-11-11 14:17:30 +00:00
|
|
|
argument has changed state.
|
|
|
|
By default,
|
|
|
|
.BR waitpid ()
|
|
|
|
waits only for terminated children, but this behaviour is modifiable
|
|
|
|
via the
|
|
|
|
.I options
|
|
|
|
argument, as described below.
|
2004-11-03 13:51:07 +00:00
|
|
|
|
|
|
|
The value of
|
|
|
|
.I pid
|
2004-11-11 14:17:30 +00:00
|
|
|
can be:
|
2004-11-03 13:51:07 +00:00
|
|
|
.IP "< \-1"
|
2004-11-11 14:17:30 +00:00
|
|
|
meaning wait for any child process whose process group ID is
|
2004-11-03 13:51:07 +00:00
|
|
|
equal to the absolute value of
|
|
|
|
.IR pid .
|
|
|
|
.IP \-1
|
2004-11-11 14:17:30 +00:00
|
|
|
meaning wait for any child process.
|
2004-11-03 13:51:07 +00:00
|
|
|
.IP 0
|
2004-11-11 14:17:30 +00:00
|
|
|
meaning wait for any child process whose process group ID is
|
2004-11-03 13:51:07 +00:00
|
|
|
equal to that of the calling process.
|
|
|
|
.IP "> 0"
|
2004-11-11 14:17:30 +00:00
|
|
|
meaning wait for the child whose process ID is equal to the
|
2004-11-03 13:51:07 +00:00
|
|
|
value of
|
|
|
|
.IR pid .
|
|
|
|
.PP
|
|
|
|
The value of
|
|
|
|
.I options
|
|
|
|
is an OR of zero or more of the following constants:
|
|
|
|
.TP
|
|
|
|
.B WNOHANG
|
2004-11-11 14:17:30 +00:00
|
|
|
return immediately if no child has exited.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B WUNTRACED
|
2004-11-11 14:40:35 +00:00
|
|
|
also return if a child has stopped
|
2004-11-11 14:17:30 +00:00
|
|
|
(but not traced via
|
2004-11-11 14:40:35 +00:00
|
|
|
.BR ptrace (2)).
|
|
|
|
Status for
|
|
|
|
.I traced
|
|
|
|
children which have stopped is provided
|
|
|
|
even if this option is not specified.
|
2004-11-11 14:17:30 +00:00
|
|
|
.TP
|
|
|
|
.B WCONTINUED
|
|
|
|
(Since Linux 2.6.10)
|
2004-11-11 14:40:35 +00:00
|
|
|
also return if a stopped child has been resumed by delivery of
|
2004-11-11 14:17:30 +00:00
|
|
|
.BR SIGCONT .
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
(For Linux-only options, see below.)
|
|
|
|
.PP
|
2007-04-12 22:42:49 +00:00
|
|
|
The
|
2004-11-11 14:17:30 +00:00
|
|
|
.B WUNTRACED
|
|
|
|
and
|
|
|
|
.B WCONTINUED
|
|
|
|
options are only effective if the
|
|
|
|
.B SA_NOCLDSTOP
|
|
|
|
flag has not been set for the
|
|
|
|
.B SIGCHLD
|
|
|
|
signal (see
|
|
|
|
.BR sigaction (2)).
|
|
|
|
.PP
|
2004-11-03 13:51:07 +00:00
|
|
|
If
|
|
|
|
.I status
|
2005-11-02 13:55:25 +00:00
|
|
|
is not NULL,
|
2004-11-11 14:17:30 +00:00
|
|
|
.BR wait ()
|
|
|
|
and
|
|
|
|
.BR waitpid ()
|
|
|
|
store status information in the \fIint\fR to which it points.
|
|
|
|
This integer can be inspected with the following macros (which
|
|
|
|
take the integer itself as an argument, not a pointer to it,
|
|
|
|
as is done in
|
|
|
|
.BR wait ()
|
|
|
|
and
|
|
|
|
.BR waitpid ()!):
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.BI WIFEXITED( status )
|
|
|
|
returns true if the child terminated normally, that is,
|
2004-11-11 14:17:30 +00:00
|
|
|
by calling
|
|
|
|
.BR exit (3)
|
|
|
|
or
|
2005-07-19 15:36:19 +00:00
|
|
|
.BR _exit (2),
|
2004-11-11 14:17:30 +00:00
|
|
|
or by returning from main().
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.BI WEXITSTATUS( status )
|
2004-11-11 14:17:30 +00:00
|
|
|
returns the exit status of the child.
|
|
|
|
This consists of the least significant 8 bits of the
|
|
|
|
.I status
|
|
|
|
argument that the child specified in a call to
|
2007-05-11 23:07:02 +00:00
|
|
|
.BR exit (3)
|
2004-11-11 14:17:30 +00:00
|
|
|
or
|
2007-05-11 23:07:02 +00:00
|
|
|
.BR _exit (2)
|
2004-11-11 14:17:30 +00:00
|
|
|
or as the argument for a return statement in main().
|
|
|
|
This macro should only be employed if
|
2004-11-03 13:51:07 +00:00
|
|
|
.B WIFEXITED
|
|
|
|
returned true.
|
|
|
|
.TP
|
|
|
|
.BI WIFSIGNALED( status )
|
2004-11-11 14:17:30 +00:00
|
|
|
returns true if the child process was terminated by a signal.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.BI WTERMSIG( status )
|
|
|
|
returns the number of the signal that caused the child process to
|
2007-04-12 22:42:49 +00:00
|
|
|
terminate.
|
|
|
|
This macro should only be employed if
|
2004-11-11 14:17:30 +00:00
|
|
|
.B WIFSIGNALED
|
|
|
|
returned true.
|
|
|
|
.TP
|
|
|
|
.BI WCOREDUMP( status )
|
|
|
|
returns true if the child produced a core dump.
|
|
|
|
This macro should only be employed if
|
2004-11-03 13:51:07 +00:00
|
|
|
.B WIFSIGNALED
|
2004-11-11 14:17:30 +00:00
|
|
|
returned true.
|
|
|
|
This macro is not specified in POSIX.1-2001 and is not available on
|
|
|
|
some Unix implementations (e.g., AIX, SunOS).
|
|
|
|
Only use this enclosed in #ifdef WCOREDUMP ... #endif.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.BI WIFSTOPPED( status )
|
2004-11-11 14:17:30 +00:00
|
|
|
returns true if the child process was stopped by delivery of a signal;
|
|
|
|
this is only possible if the call was done using
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR WUNTRACED
|
|
|
|
or when the child is being traced (see
|
|
|
|
.BR ptrace (2)).
|
|
|
|
.TP
|
|
|
|
.BI WSTOPSIG( status )
|
2007-04-12 22:42:49 +00:00
|
|
|
returns the number of the signal which caused the child to stop.
|
|
|
|
This macro should only be employed if
|
2004-11-03 13:51:07 +00:00
|
|
|
.B WIFSTOPPED
|
2004-11-11 14:17:30 +00:00
|
|
|
returned true.
|
|
|
|
.TP
|
|
|
|
.BI WIFCONTINUED( status )
|
|
|
|
(Since Linux 2.6.10)
|
|
|
|
returns true if the child process was resumed by delivery of
|
|
|
|
.BR SIGCONT .
|
|
|
|
.SS "waitid()"
|
|
|
|
The
|
|
|
|
.BR waitid ()
|
|
|
|
system call (available since Linux 2.6.9) provides more precise
|
|
|
|
control over which child state changes to wait for.
|
|
|
|
|
|
|
|
The
|
|
|
|
.I idtype
|
|
|
|
and
|
|
|
|
.I id
|
|
|
|
arguments select the child(ren) to wait for, as follows:
|
|
|
|
.IP "\fIidtype\fP == \fBP_PID\fP"
|
|
|
|
Wait for the child whose process ID matches
|
|
|
|
.IR id .
|
|
|
|
.IP "\fIidtype\fP == \fBP_PGID\fP"
|
|
|
|
Wait for any child whose process group ID matches
|
|
|
|
.IR id .
|
|
|
|
.IP "\fIidtype\fP == \fBP_ALL\fP"
|
|
|
|
Wait for any child;
|
|
|
|
.I id
|
|
|
|
is ignored.
|
|
|
|
.PP
|
|
|
|
The child state changes to wait for are specified by ORing
|
|
|
|
one or more of the following flags in
|
|
|
|
.IR options :
|
|
|
|
.TP
|
|
|
|
.B WEXITED
|
|
|
|
Wait for children that have terminated.
|
|
|
|
.TP
|
|
|
|
.B WSTOPPED
|
|
|
|
Wait for children that have been stopped by delivery of a signal.
|
|
|
|
.TP
|
|
|
|
.B WCONTINUED
|
|
|
|
Wait for (previously stopped) children that have been
|
|
|
|
resumed by delivery of
|
|
|
|
.BR SIGCONT .
|
|
|
|
.PP
|
|
|
|
The following flags may additionally be ORed in
|
|
|
|
.IR options :
|
|
|
|
.TP
|
|
|
|
.B WNOHANG
|
|
|
|
As for
|
|
|
|
.BR waitpid ().
|
|
|
|
.TP
|
|
|
|
.B WNOWAIT
|
|
|
|
Leave the child in a waitable state; a later wait call
|
|
|
|
can be used to again retrieve the child status information.
|
|
|
|
.PP
|
|
|
|
Upon successful return,
|
|
|
|
.BR waitid ()
|
|
|
|
fills in the following fields of the
|
|
|
|
.I siginfo_t
|
|
|
|
structure pointed to by
|
|
|
|
.IR infop :
|
|
|
|
.IP \fIsi_pid\fP
|
|
|
|
The process ID of the child.
|
|
|
|
.IP \fIsi_uid\fP
|
|
|
|
The real user ID of the child.
|
2006-01-23 06:41:46 +00:00
|
|
|
(This field is not set on most other implementations.)
|
2004-11-11 14:17:30 +00:00
|
|
|
.IP \fIsi_signo\fP
|
|
|
|
Always set to
|
|
|
|
.BR SIGCHLD .
|
|
|
|
.IP \fIsi_status\fP
|
|
|
|
Either the exit status of the child, as given to
|
|
|
|
.BR _exit (2)
|
|
|
|
(or
|
|
|
|
.BR exit (3)),
|
|
|
|
or the signal that caused the child to terminate, stop, or continue.
|
2007-04-12 22:42:49 +00:00
|
|
|
The
|
2004-11-11 14:17:30 +00:00
|
|
|
.I si_code
|
|
|
|
field can be used to determine how to interpret this field.
|
|
|
|
.IP \fIsi_code\fP
|
|
|
|
Set to one of:
|
|
|
|
.B CLD_EXITED
|
|
|
|
(child called
|
|
|
|
.BR _exit (2));
|
|
|
|
.B CLD_KILLED
|
|
|
|
(child killed by signal);
|
|
|
|
.B CLD_STOPPED
|
|
|
|
(child stopped by signal); or
|
|
|
|
.B CLD_CONTINUED
|
|
|
|
(child continued by
|
|
|
|
.BR SIGCONT ).
|
|
|
|
.PP
|
|
|
|
If
|
|
|
|
.B WNOHANG
|
|
|
|
was specified in
|
|
|
|
.I options
|
|
|
|
and there were no children in a waitable state, then
|
|
|
|
.BR waitid ()
|
|
|
|
returns 0 immediately and
|
|
|
|
the state of the
|
|
|
|
.I siginfo_t
|
|
|
|
structure pointed to by
|
|
|
|
.I infop
|
|
|
|
is unspecified.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\" POSIX.1-2001 leaves this possibility unspecified; most
|
2004-11-11 14:17:30 +00:00
|
|
|
.\" implementations (including Linux) zero out the structure
|
|
|
|
.\" in this case, but at at least one implementation (AIX 5.1)
|
|
|
|
.\" does not -- MTK Nov 04
|
|
|
|
To distinguish this case from that where a child was in a
|
|
|
|
waitable state, zero out the
|
|
|
|
.I si_pid
|
|
|
|
field before the call and check for a non-zero value in this field
|
|
|
|
after the call returns.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "RETURN VALUE"
|
2004-11-11 14:17:30 +00:00
|
|
|
.BR wait ():
|
2004-11-11 14:40:35 +00:00
|
|
|
on success, returns the process ID of the terminated child;
|
2004-11-11 14:17:30 +00:00
|
|
|
on error, \-1 is returned.
|
|
|
|
|
2005-07-19 15:36:19 +00:00
|
|
|
.BR waitpid ():
|
2004-11-11 14:40:35 +00:00
|
|
|
on success, returns the process ID of the child whose state has changed;
|
2004-11-11 14:17:30 +00:00
|
|
|
on error, \-1 is returned;
|
|
|
|
if
|
2004-11-03 13:51:07 +00:00
|
|
|
.B WNOHANG
|
2004-11-11 14:17:30 +00:00
|
|
|
was specified and no child(ren) specified by
|
|
|
|
.I pid
|
|
|
|
has yet changed state, then 0 is returned.
|
|
|
|
|
2005-07-19 15:36:19 +00:00
|
|
|
.BR waitid ():
|
2007-04-12 22:42:49 +00:00
|
|
|
returns 0 on success or
|
2004-11-11 14:17:30 +00:00
|
|
|
if
|
|
|
|
.B WNOHANG
|
|
|
|
was specified and no child(ren) specified by
|
|
|
|
.I id
|
|
|
|
has yet changed state;
|
|
|
|
on error, \-1 is returned.
|
|
|
|
|
|
|
|
Each of these calls sets
|
2004-11-03 13:51:07 +00:00
|
|
|
.I errno
|
2004-11-11 14:17:30 +00:00
|
|
|
to an appropriate value in the case of an error.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH ERRORS
|
|
|
|
.TP
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR ECHILD
|
|
|
|
(for
|
2005-10-19 07:29:28 +00:00
|
|
|
.BR wait ())
|
2004-11-03 13:51:07 +00:00
|
|
|
The calling process does not have any unwaited-for children.
|
|
|
|
.TP
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR ECHILD
|
|
|
|
(for
|
|
|
|
.BR waitpid ()
|
|
|
|
or
|
2005-10-19 07:29:28 +00:00
|
|
|
.BR waitid ())
|
2004-11-11 14:17:30 +00:00
|
|
|
The process specified by
|
2004-11-03 13:51:07 +00:00
|
|
|
.I pid
|
2004-11-11 14:17:30 +00:00
|
|
|
.RB ( waitpid ())
|
|
|
|
or
|
|
|
|
.I idtype
|
|
|
|
and
|
|
|
|
.I id
|
|
|
|
.RB ( waitid ())
|
2004-11-03 13:51:07 +00:00
|
|
|
does not exist or is not a child of the calling process.
|
|
|
|
(This can happen for one's own child if the action for SIGCHLD
|
2007-04-12 22:42:49 +00:00
|
|
|
is set to SIG_IGN.
|
|
|
|
See also the LINUX NOTES section about threads.)
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B EINTR
|
|
|
|
.B WNOHANG
|
|
|
|
was not set and an unblocked signal or a
|
|
|
|
.B SIGCHLD
|
|
|
|
was caught.
|
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
The
|
|
|
|
.I options
|
|
|
|
argument was invalid.
|
|
|
|
.SH NOTES
|
2004-11-11 14:17:30 +00:00
|
|
|
A child that terminates, but has not been waited for becomes a "zombie".
|
|
|
|
The kernel maintains a minimal set of information about the zombie
|
|
|
|
process (PID, termination status, resource usage information)
|
|
|
|
in order to allow the parent to later perform a wait to obtain
|
|
|
|
information about the child.
|
|
|
|
As long as a zombie is not removed from the system via a wait,
|
|
|
|
it will consume a slot in the kernel process table, and if
|
|
|
|
this table fills, it will not be possible to create further processes.
|
|
|
|
If a parent process terminates, then its "zombie" children (if any)
|
|
|
|
are adopted by
|
|
|
|
.BR init (8),
|
|
|
|
which automatically performs a wait to remove the zombies.
|
|
|
|
|
|
|
|
POSIX.1-2001 specifies that if the disposition of
|
|
|
|
.B SIGCHLD
|
2007-04-12 22:42:49 +00:00
|
|
|
is set to
|
2004-11-11 14:17:30 +00:00
|
|
|
.B SIG_IGN
|
2007-04-12 22:42:49 +00:00
|
|
|
or the
|
2004-11-11 14:17:30 +00:00
|
|
|
.B SA_NOCLDWAIT
|
|
|
|
flag is set for
|
|
|
|
.BR SIGCHLD
|
2007-04-12 22:42:49 +00:00
|
|
|
(see
|
2004-11-11 14:17:30 +00:00
|
|
|
.BR sigaction (2)),
|
|
|
|
then children that terminate do not become zombies and a call to
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR wait ()
|
|
|
|
or
|
|
|
|
.BR waitpid ()
|
2004-11-11 14:17:30 +00:00
|
|
|
will block until all children have terminated, and then fail with
|
2004-11-03 13:51:07 +00:00
|
|
|
.I errno
|
2004-11-11 14:17:30 +00:00
|
|
|
set to
|
|
|
|
.BR ECHILD .
|
|
|
|
(The original POSIX standard left the behaviour of setting
|
|
|
|
.B SIGCHLD
|
|
|
|
to
|
|
|
|
.B SIG_IGN
|
|
|
|
unspecified.)
|
|
|
|
Linux 2.6 conforms to this specification.
|
|
|
|
However, Linux 2.4 (and earlier) does not:
|
2004-11-03 13:51:07 +00:00
|
|
|
if a
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR wait ()
|
|
|
|
or
|
2005-10-19 07:29:28 +00:00
|
|
|
.BR waitpid ()
|
2004-11-11 14:17:30 +00:00
|
|
|
call is made while
|
|
|
|
.B SIGCHLD
|
|
|
|
is being ignored, the call behaves just as though
|
|
|
|
.B SIGCHLD
|
2005-04-18 13:35:29 +00:00
|
|
|
were not being ignored, that is, the call blocks until the next child
|
2004-11-11 14:40:35 +00:00
|
|
|
terminates and then returns the process ID and status of that child.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "LINUX NOTES"
|
|
|
|
In the Linux kernel, a kernel-scheduled thread is not a distinct
|
2007-04-12 22:42:49 +00:00
|
|
|
construct from a process.
|
|
|
|
Instead, a thread is simply a process
|
2004-11-03 13:51:07 +00:00
|
|
|
that is created using the Linux-unique
|
|
|
|
.BR clone (2)
|
|
|
|
system call; other routines such as the portable
|
|
|
|
.BR pthread_create (3)
|
|
|
|
call are implemented using
|
|
|
|
.BR clone (2).
|
|
|
|
Before Linux 2.4, a thread was just a special case of a process,
|
|
|
|
and as a consequence one thread could not wait on the children
|
|
|
|
of another thread, even when the latter belongs to the same thread group.
|
|
|
|
However, POSIX prescribes such functionality, and since Linux 2.4
|
|
|
|
a thread can, and by default will, wait on children of other threads
|
|
|
|
in the same thread group.
|
|
|
|
.LP
|
2006-12-17 01:34:44 +00:00
|
|
|
The following Linux specific
|
2004-11-03 13:51:07 +00:00
|
|
|
.I options
|
|
|
|
are for use with children created using
|
2005-05-10 17:16:28 +00:00
|
|
|
.BR clone (2);
|
|
|
|
they cannot be used with
|
|
|
|
.BR waitid ():
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B __WCLONE
|
|
|
|
.\" since 0.99pl10
|
2007-04-12 22:42:49 +00:00
|
|
|
Wait for "clone" children only.
|
|
|
|
If omitted then wait for "non-clone" children only.
|
|
|
|
(A "clone" child is one which delivers no signal, or a signal other than
|
2004-11-03 13:51:07 +00:00
|
|
|
.B SIGCHLD
|
|
|
|
to its parent upon termination.)
|
|
|
|
This option is ignored if
|
|
|
|
.B __WALL
|
|
|
|
is also specified.
|
|
|
|
.TP
|
|
|
|
.B __WALL
|
|
|
|
.\" since patch-2.3.48
|
|
|
|
(Since Linux 2.4) Wait for all children, regardless of
|
|
|
|
type ("clone" or "non-clone").
|
|
|
|
.TP
|
|
|
|
.B __WNOTHREAD
|
|
|
|
.\" since patch-2.4.0-test8
|
|
|
|
(Since Linux 2.4) Do not wait for children of other threads in
|
2007-04-12 22:42:49 +00:00
|
|
|
the same thread group.
|
|
|
|
This was the default before Linux 2.4.
|
2005-12-09 16:37:05 +00:00
|
|
|
.SH EXAMPLE
|
2005-12-12 09:19:46 +00:00
|
|
|
.\" fork.2 refers to this example program.
|
2007-04-12 22:42:49 +00:00
|
|
|
The following program demonstrates the use of
|
2007-05-14 21:10:08 +00:00
|
|
|
.BR fork (2)
|
2007-04-12 22:42:49 +00:00
|
|
|
and
|
2005-12-09 16:37:05 +00:00
|
|
|
.BR waitpid (2).
|
|
|
|
The program creates a child process.
|
2007-04-12 22:42:49 +00:00
|
|
|
If no command-line argument is supplied to the program,
|
|
|
|
then the child suspends its execution using
|
2005-12-09 16:37:05 +00:00
|
|
|
.BR pause (2),
|
|
|
|
to allow the user to send signals to the child.
|
|
|
|
Otherwise, if a command-line argument is supplied,
|
2007-04-12 22:42:49 +00:00
|
|
|
then the child exits immediately,
|
2005-12-09 16:37:05 +00:00
|
|
|
using the integer supplied on the command line as the exit status.
|
|
|
|
The parent process executes a loop that monitors the child using
|
|
|
|
.BR waitpid (2),
|
|
|
|
and uses the W*() macros described above to analyse the wait status value.
|
|
|
|
|
|
|
|
The following shell session demonstrates the use of the program:
|
|
|
|
.nf
|
|
|
|
|
|
|
|
$ ./a.out &
|
|
|
|
Child PID is 32360
|
|
|
|
[1] 32359
|
|
|
|
$ kill -STOP 32360
|
|
|
|
stopped by signal 19
|
|
|
|
$ kill -CONT 32360
|
|
|
|
continued
|
|
|
|
$ kill -TERM 32360
|
|
|
|
killed by signal 15
|
|
|
|
[1]+ Done ./a.out
|
|
|
|
$
|
|
|
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
pid_t cpid, w;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
cpid = fork();
|
|
|
|
if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
|
|
|
|
|
|
|
|
if (cpid == 0) { /* Code executed by child */
|
|
|
|
printf("Child PID is %ld\\n", (long) getpid());
|
|
|
|
if (argc == 1)
|
|
|
|
pause(); /* Wait for signals */
|
|
|
|
_exit(atoi(argv[1]));
|
|
|
|
|
|
|
|
} else { /* Code executed by parent */
|
|
|
|
do {
|
|
|
|
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
|
|
|
|
if (w == -1) { perror("waitpid"); exit(EXIT_FAILURE); }
|
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
printf("exited, status=%d\\n", WEXITSTATUS(status));
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
printf("killed by signal %d\\n", WTERMSIG(status));
|
|
|
|
} else if (WIFSTOPPED(status)) {
|
|
|
|
printf("stopped by signal %d\\n", WSTOPSIG(status));
|
|
|
|
} else if (WIFCONTINUED(status)) {
|
|
|
|
printf("continued\\n");
|
|
|
|
}
|
|
|
|
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.fi
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "CONFORMING TO"
|
2006-08-03 13:57:17 +00:00
|
|
|
SVr4, 4.3BSD, POSIX.1-2001.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
2004-11-11 14:17:30 +00:00
|
|
|
.BR _exit (2),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR clone (2),
|
2004-11-11 14:17:30 +00:00
|
|
|
.BR fork (2),
|
|
|
|
.BR kill (2),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR ptrace (2),
|
2004-11-11 14:17:30 +00:00
|
|
|
.BR sigaction (2),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR signal (2),
|
|
|
|
.BR wait4 (2),
|
|
|
|
.BR pthread_create (3),
|
|
|
|
.BR signal (7)
|