989 lines
24 KiB
HTML
989 lines
24 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of WAIT</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>WAIT</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-11-19<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
wait, waitpid, waitid - wait for process to change state
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B>
|
|
|
|
<BR>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/sys/wait.h">sys/wait.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>pid_t wait(int *</B><I>wstatus</I><B>);</B>
|
|
|
|
<P>
|
|
|
|
<B>pid_t waitpid(pid_t </B><I>pid</I><B>, int *</B><I>wstatus</I><B>, int </B><I>options</I><B>);</B>
|
|
|
|
<P>
|
|
|
|
<B>int waitid(idtype_t </B><I>idtype</I><B>, id_t </B><I>id</I><B>, siginfo_t *</B><I>infop</I><B>, int </B><I>options</I><B>);</B>
|
|
|
|
<BR> /* This is the glibc and POSIX interface; see
|
|
<BR> NOTES for information on the raw system call. */
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
|
|
|
|
<B>waitid</B>():
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
Since glibc 2.26:
|
|
_XOPEN_SOURCE >= 500 ||
|
|
|
|
<BR> _POSIX_C_SOURCE >= 200809L
|
|
<BR>
|
|
|
|
Glibc 2.25 and earlier:
|
|
<BR> _XOPEN_SOURCE
|
|
<BR> || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
|
|
<BR> || /* Glibc versions <= 2.19: */ _BSD_SOURCE
|
|
</DL>
|
|
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
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 the terminated child remains in
|
|
a "zombie" state (see NOTES below).
|
|
<P>
|
|
|
|
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</B>
|
|
|
|
flag of
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2)).
|
|
|
|
In the remainder of this page, a child whose state has changed
|
|
and which has not yet been waited upon by one of these system
|
|
calls is termed
|
|
<I>waitable</I>.
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H3>wait() and waitpid()</H3>
|
|
|
|
The
|
|
<B>wait</B>()
|
|
|
|
system call suspends execution of the calling thread until one of its
|
|
children terminates.
|
|
The call
|
|
<I>wait(&wstatus)</I>
|
|
|
|
is equivalent to:
|
|
<P>
|
|
|
|
|
|
|
|
waitpid(-1, &wstatus, 0);
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<B>waitpid</B>()
|
|
|
|
system call suspends execution of the calling thread until a
|
|
child specified by
|
|
<I>pid</I>
|
|
|
|
argument has changed state.
|
|
By default,
|
|
<B>waitpid</B>()
|
|
|
|
waits only for terminated children, but this behavior is modifiable
|
|
via the
|
|
<I>options</I>
|
|
|
|
argument, as described below.
|
|
<P>
|
|
|
|
The value of
|
|
<I>pid</I>
|
|
|
|
can be:
|
|
<DL COMPACT>
|
|
<DT id="2">< -1<DD>
|
|
meaning wait for any child process whose process group ID is
|
|
equal to the absolute value of
|
|
<I>pid</I>.
|
|
|
|
<DT id="3">-1<DD>
|
|
meaning wait for any child process.
|
|
<DT id="4">0<DD>
|
|
meaning wait for any child process whose process group ID is
|
|
equal to that of the calling process at the time of the call to
|
|
<B>waitpid</B>().
|
|
|
|
<DT id="5">> 0<DD>
|
|
meaning wait for the child whose process ID is equal to the
|
|
value of
|
|
<I>pid</I>.
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
The value of
|
|
<I>options</I>
|
|
|
|
is an OR of zero or more of the following constants:
|
|
<DL COMPACT>
|
|
<DT id="6"><B>WNOHANG</B>
|
|
|
|
<DD>
|
|
return immediately if no child has exited.
|
|
<DT id="7"><B>WUNTRACED</B>
|
|
|
|
<DD>
|
|
also return if a child has stopped
|
|
(but not traced via
|
|
<B><A HREF="/cgi-bin/man/man2html?2+ptrace">ptrace</A></B>(2)).
|
|
|
|
Status for
|
|
<I>traced</I>
|
|
|
|
children which have stopped is provided
|
|
even if this option is not specified.
|
|
<DT id="8"><B>WCONTINUED</B> (since Linux 2.6.10)
|
|
|
|
<DD>
|
|
also return if a stopped child has been resumed by delivery of
|
|
<B>SIGCONT</B>.
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
(For Linux-only options, see below.)
|
|
<P>
|
|
|
|
If
|
|
<I>wstatus</I>
|
|
|
|
is not NULL,
|
|
<B>wait</B>()
|
|
|
|
and
|
|
<B>waitpid</B>()
|
|
|
|
store status information in the <I>int</I> 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
|
|
<B>wait</B>()
|
|
|
|
and
|
|
<B>waitpid</B>()!):
|
|
|
|
<DL COMPACT>
|
|
<DT id="9"><B>WIFEXITED(</B><I>wstatus</I><B>)</B>
|
|
|
|
<DD>
|
|
returns true if the child terminated normally, that is,
|
|
by calling
|
|
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2),
|
|
|
|
or by returning from main().
|
|
<DT id="10"><B>WEXITSTATUS(</B><I>wstatus</I><B>)</B>
|
|
|
|
<DD>
|
|
returns the exit status of the child.
|
|
This consists of the least significant 8 bits of the
|
|
<I>status</I>
|
|
|
|
argument that the child specified in a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2)
|
|
|
|
or as the argument for a return statement in main().
|
|
This macro should be employed only if
|
|
<B>WIFEXITED</B>
|
|
|
|
returned true.
|
|
<DT id="11"><B>WIFSIGNALED(</B><I>wstatus</I><B>)</B>
|
|
|
|
<DD>
|
|
returns true if the child process was terminated by a signal.
|
|
<DT id="12"><B>WTERMSIG(</B><I>wstatus</I><B>)</B>
|
|
|
|
<DD>
|
|
returns the number of the signal that caused the child process to
|
|
terminate.
|
|
This macro should be employed only if
|
|
<B>WIFSIGNALED</B>
|
|
|
|
returned true.
|
|
<DT id="13"><B>WCOREDUMP(</B><I>wstatus</I><B>)</B>
|
|
|
|
<DD>
|
|
returns true if the child produced a core dump (see
|
|
<B><A HREF="/cgi-bin/man/man2html?5+core">core</A></B>(5)).
|
|
|
|
This macro should be employed only if
|
|
<B>WIFSIGNALED</B>
|
|
|
|
returned true.
|
|
<DT id="14"><DD>
|
|
This macro is not specified in POSIX.1-2001 and is not available on
|
|
some UNIX implementations (e.g., AIX, SunOS).
|
|
Therefore, enclose its use inside
|
|
<I>#ifdef WCOREDUMP ... #endif</I>.
|
|
|
|
<DT id="15"><B>WIFSTOPPED(</B><I>wstatus</I><B>)</B>
|
|
|
|
<DD>
|
|
returns true if the child process was stopped by delivery of a signal;
|
|
this is possible only if the call was done using
|
|
<B>WUNTRACED</B>
|
|
|
|
or when the child is being traced (see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+ptrace">ptrace</A></B>(2)).
|
|
|
|
<DT id="16"><B>WSTOPSIG(</B><I>wstatus</I><B>)</B>
|
|
|
|
<DD>
|
|
returns the number of the signal which caused the child to stop.
|
|
This macro should be employed only if
|
|
<B>WIFSTOPPED</B>
|
|
|
|
returned true.
|
|
<DT id="17"><B>WIFCONTINUED(</B><I>wstatus</I><B>)</B>
|
|
|
|
<DD>
|
|
(since Linux 2.6.10)
|
|
returns true if the child process was resumed by delivery of
|
|
<B>SIGCONT</B>.
|
|
|
|
</DL>
|
|
<A NAME="lbAF"> </A>
|
|
<H3>waitid()</H3>
|
|
|
|
The
|
|
<B>waitid</B>()
|
|
|
|
system call (available since Linux 2.6.9) provides more precise
|
|
control over which child state changes to wait for.
|
|
<P>
|
|
|
|
The
|
|
<I>idtype</I>
|
|
|
|
and
|
|
<I>id</I>
|
|
|
|
arguments select the child(ren) to wait for, as follows:
|
|
<DL COMPACT>
|
|
<DT id="18"><I>idtype</I> == <B>P_PID</B><DD>
|
|
Wait for the child whose process ID matches
|
|
<I>id</I>.
|
|
|
|
<DT id="19"><I>idtype</I> == <B>P_PIDFD</B> (since Linux 5.4)<DD>
|
|
|
|
Wait for the child referred to by the PID file descriptor specified in
|
|
<I>id</I>.
|
|
|
|
(See
|
|
<B><A HREF="/cgi-bin/man/man2html?2+pidfd_open">pidfd_open</A></B>(2)
|
|
|
|
for further information on PID file descriptors.)
|
|
<DT id="20"><I>idtype</I> == <B>P_PGID</B><DD>
|
|
Wait for any child whose process group ID matches
|
|
<I>id</I>.
|
|
|
|
Since Linux 5.4,
|
|
|
|
if
|
|
<I>id</I>
|
|
|
|
is zero, then wait for any child that is in the same process group
|
|
as the caller's process group at the time of the call.
|
|
<DT id="21"><I>idtype</I> == <B>P_ALL</B><DD>
|
|
Wait for any child;
|
|
<I>id</I>
|
|
|
|
is ignored.
|
|
</DL>
|
|
<P>
|
|
|
|
The child state changes to wait for are specified by ORing
|
|
one or more of the following flags in
|
|
<I>options</I>:
|
|
|
|
<DL COMPACT>
|
|
<DT id="22"><B>WEXITED</B>
|
|
|
|
<DD>
|
|
Wait for children that have terminated.
|
|
<DT id="23"><B>WSTOPPED</B>
|
|
|
|
<DD>
|
|
Wait for children that have been stopped by delivery of a signal.
|
|
<DT id="24"><B>WCONTINUED</B>
|
|
|
|
<DD>
|
|
Wait for (previously stopped) children that have been
|
|
resumed by delivery of
|
|
<B>SIGCONT</B>.
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
The following flags may additionally be ORed in
|
|
<I>options</I>:
|
|
|
|
<DL COMPACT>
|
|
<DT id="25"><B>WNOHANG</B>
|
|
|
|
<DD>
|
|
As for
|
|
<B>waitpid</B>().
|
|
|
|
<DT id="26"><B>WNOWAIT</B>
|
|
|
|
<DD>
|
|
Leave the child in a waitable state; a later wait call
|
|
can be used to again retrieve the child status information.
|
|
</DL>
|
|
<P>
|
|
|
|
Upon successful return,
|
|
<B>waitid</B>()
|
|
|
|
fills in the following fields of the
|
|
<I>siginfo_t</I>
|
|
|
|
structure pointed to by
|
|
<I>infop</I>:
|
|
|
|
<DL COMPACT>
|
|
<DT id="27"><I>si_pid</I><DD>
|
|
The process ID of the child.
|
|
<DT id="28"><I>si_uid</I><DD>
|
|
The real user ID of the child.
|
|
(This field is not set on most other implementations.)
|
|
<DT id="29"><I>si_signo</I><DD>
|
|
Always set to
|
|
<B>SIGCHLD</B>.
|
|
|
|
<DT id="30"><I>si_status</I><DD>
|
|
Either the exit status of the child, as given to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2)
|
|
|
|
(or
|
|
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)),
|
|
|
|
or the signal that caused the child to terminate, stop, or continue.
|
|
The
|
|
<I>si_code</I>
|
|
|
|
field can be used to determine how to interpret this field.
|
|
<DT id="31"><I>si_code</I><DD>
|
|
Set to one of:
|
|
<B>CLD_EXITED</B>
|
|
|
|
(child called
|
|
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2));
|
|
|
|
<B>CLD_KILLED</B>
|
|
|
|
(child killed by signal);
|
|
<B>CLD_DUMPED</B>
|
|
|
|
(child killed by signal, and dumped core);
|
|
<B>CLD_STOPPED</B>
|
|
|
|
(child stopped by signal);
|
|
<B>CLD_TRAPPED</B>
|
|
|
|
(traced child has trapped); or
|
|
<B>CLD_CONTINUED</B>
|
|
|
|
(child continued by
|
|
<B>SIGCONT</B>).
|
|
|
|
</DL>
|
|
<P>
|
|
|
|
If
|
|
<B>WNOHANG</B>
|
|
|
|
was specified in
|
|
<I>options</I>
|
|
|
|
and there were no children in a waitable state, then
|
|
<B>waitid</B>()
|
|
|
|
returns 0 immediately and
|
|
the state of the
|
|
<I>siginfo_t</I>
|
|
|
|
structure pointed to by
|
|
<I>infop</I>
|
|
|
|
depends on the implementation.
|
|
To (portably) distinguish this case from that where a child was in a
|
|
waitable state, zero out the
|
|
<I>si_pid</I>
|
|
|
|
field before the call and check for a nonzero value in this field
|
|
after the call returns.
|
|
<P>
|
|
|
|
POSIX.1-2008 Technical Corrigendum 1 (2013) adds the requirement that when
|
|
<B>WNOHANG</B>
|
|
|
|
is specified in
|
|
<I>options</I>
|
|
|
|
and there were no children in a waitable state, then
|
|
<B>waitid</B>()
|
|
|
|
should zero out the
|
|
<I>si_pid</I>
|
|
|
|
and
|
|
<I>si_signo</I>
|
|
|
|
fields of the structure.
|
|
On Linux and other implementations that adhere to this requirement,
|
|
it is not necessary to zero out the
|
|
<I>si_pid</I>
|
|
|
|
field before calling
|
|
<B>waitid</B>().
|
|
|
|
However,
|
|
not all implementations follow the POSIX.1 specification on this point.
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
<B>wait</B>():
|
|
|
|
on success, returns the process ID of the terminated child;
|
|
on error, -1 is returned.
|
|
<P>
|
|
|
|
<B>waitpid</B>():
|
|
|
|
on success, returns the process ID of the child whose state has changed;
|
|
if
|
|
<B>WNOHANG</B>
|
|
|
|
was specified and one or more child(ren) specified by
|
|
<I>pid</I>
|
|
|
|
exist, but have not yet changed state, then 0 is returned.
|
|
On error, -1 is returned.
|
|
<P>
|
|
|
|
<B>waitid</B>():
|
|
|
|
returns 0 on success or
|
|
if
|
|
<B>WNOHANG</B>
|
|
|
|
was specified and no child(ren) specified by
|
|
<I>id</I>
|
|
|
|
has yet changed state;
|
|
on error, -1 is returned.
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
Each of these calls sets
|
|
<I>errno</I>
|
|
|
|
to an appropriate value in the case of an error.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="32"><B>ECHILD</B>
|
|
|
|
<DD>
|
|
(for
|
|
<B>wait</B>())
|
|
|
|
The calling process does not have any unwaited-for children.
|
|
<DT id="33"><B>ECHILD</B>
|
|
|
|
<DD>
|
|
(for
|
|
<B>waitpid</B>()
|
|
|
|
or
|
|
<B>waitid</B>())
|
|
|
|
The process specified by
|
|
<I>pid</I>
|
|
|
|
(<B>waitpid</B>())
|
|
|
|
or
|
|
<I>idtype</I>
|
|
|
|
and
|
|
<I>id</I>
|
|
|
|
(<B>waitid</B>())
|
|
|
|
does not exist or is not a child of the calling process.
|
|
(This can happen for one's own child if the action for
|
|
<B>SIGCHLD</B>
|
|
|
|
is set to
|
|
<B>SIG_IGN</B>.
|
|
|
|
See also the <I>Linux Notes</I> section about threads.)
|
|
<DT id="34"><B>EINTR</B>
|
|
|
|
<DD>
|
|
<B>WNOHANG</B>
|
|
|
|
was not set and an unblocked signal or a
|
|
<B>SIGCHLD</B>
|
|
|
|
was caught; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
|
|
|
|
<DT id="35"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>options</I>
|
|
|
|
argument was invalid.
|
|
</DL>
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
SVr4, 4.3BSD, POSIX.1-2001.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
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
|
|
<B><A HREF="/cgi-bin/man/man2html?1+init">init</A></B>(1),
|
|
|
|
(or by the nearest "subreaper" process as defined through the use of the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+prctl">prctl</A></B>(2)
|
|
|
|
<B>PR_SET_CHILD_SUBREAPER</B>
|
|
|
|
operation);
|
|
<B><A HREF="/cgi-bin/man/man2html?1+init">init</A></B>(1)
|
|
|
|
automatically performs a wait to remove the zombies.
|
|
<P>
|
|
|
|
POSIX.1-2001 specifies that if the disposition of
|
|
<B>SIGCHLD</B>
|
|
|
|
is set to
|
|
<B>SIG_IGN</B>
|
|
|
|
or the
|
|
<B>SA_NOCLDWAIT</B>
|
|
|
|
flag is set for
|
|
<B>SIGCHLD</B>
|
|
|
|
(see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2)),
|
|
|
|
then children that terminate do not become zombies and a call to
|
|
<B>wait</B>()
|
|
|
|
or
|
|
<B>waitpid</B>()
|
|
|
|
will block until all children have terminated, and then fail with
|
|
<I>errno</I>
|
|
|
|
set to
|
|
<B>ECHILD</B>.
|
|
|
|
(The original POSIX standard left the behavior of setting
|
|
<B>SIGCHLD</B>
|
|
|
|
to
|
|
<B>SIG_IGN</B>
|
|
|
|
unspecified.
|
|
Note that even though the default disposition of
|
|
<B>SIGCHLD</B>
|
|
|
|
is "ignore", explicitly setting the disposition to
|
|
<B>SIG_IGN</B>
|
|
|
|
results in different treatment of zombie process children.)
|
|
<P>
|
|
|
|
Linux 2.6 conforms to the POSIX requirements.
|
|
However, Linux 2.4 (and earlier) does not:
|
|
if a
|
|
<B>wait</B>()
|
|
|
|
or
|
|
<B>waitpid</B>()
|
|
|
|
call is made while
|
|
<B>SIGCHLD</B>
|
|
|
|
is being ignored, the call behaves just as though
|
|
<B>SIGCHLD</B>
|
|
|
|
were not being ignored, that is, the call blocks until the next child
|
|
terminates and then returns the process ID and status of that child.
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Linux notes</H3>
|
|
|
|
In the Linux kernel, a kernel-scheduled thread is not a distinct
|
|
construct from a process.
|
|
Instead, a thread is simply a process
|
|
that is created using the Linux-unique
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2)
|
|
|
|
system call; other routines such as the portable
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3)
|
|
|
|
call are implemented using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(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.
|
|
<P>
|
|
|
|
The following Linux-specific
|
|
<I>options</I>
|
|
|
|
are for use with children created using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2);
|
|
|
|
they can also, since Linux 4.7,
|
|
|
|
be used with
|
|
<B>waitid</B>():
|
|
|
|
<DL COMPACT>
|
|
<DT id="36"><B>__WCLONE</B>
|
|
|
|
<DD>
|
|
|
|
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
|
|
<B>SIGCHLD</B>
|
|
|
|
to its parent upon termination.)
|
|
This option is ignored if
|
|
<B>__WALL</B>
|
|
|
|
is also specified.
|
|
<DT id="37"><B>__WALL</B> (since Linux 2.4)
|
|
|
|
<DD>
|
|
|
|
Wait for all children, regardless of
|
|
type ("clone" or "non-clone").
|
|
<DT id="38"><B>__WNOTHREAD</B> (since Linux 2.4)
|
|
|
|
<DD>
|
|
|
|
Do not wait for children of other threads in
|
|
the same thread group.
|
|
This was the default before Linux 2.4.
|
|
</DL>
|
|
<P>
|
|
|
|
Since Linux 4.7,
|
|
|
|
|
|
|
|
the
|
|
<B>__WALL</B>
|
|
|
|
flag is automatically implied if the child is being ptraced.
|
|
<A NAME="lbAL"> </A>
|
|
<H3>C library/kernel differences</H3>
|
|
|
|
<B>wait</B>()
|
|
|
|
is actually a library function that (in glibc) is implemented as a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+wait4">wait4</A></B>(2).
|
|
|
|
<P>
|
|
|
|
On some architectures, there is no
|
|
<B>waitpid</B>()
|
|
|
|
system call;
|
|
|
|
instead, this interface is implemented via a C library
|
|
wrapper function that calls
|
|
<B><A HREF="/cgi-bin/man/man2html?2+wait4">wait4</A></B>(2).
|
|
|
|
<P>
|
|
|
|
The raw
|
|
<B>waitid</B>()
|
|
|
|
system call takes a fifth argument, of type
|
|
<I>struct rusage *</I>.
|
|
|
|
If this argument is non-NULL,
|
|
then it is used to return resource usage information about the child,
|
|
in the same manner as
|
|
<B><A HREF="/cgi-bin/man/man2html?2+wait4">wait4</A></B>(2).
|
|
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getrusage">getrusage</A></B>(2)
|
|
|
|
for details.
|
|
<A NAME="lbAM"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
According to POSIX.1-2008, an application calling
|
|
<B>waitid</B>()
|
|
|
|
must ensure that
|
|
<I>infop</I>
|
|
|
|
points to a
|
|
<I>siginfo_t</I>
|
|
|
|
structure (i.e., that it is a non-null pointer).
|
|
On Linux, if
|
|
<I>infop</I>
|
|
|
|
is NULL,
|
|
<B>waitid</B>()
|
|
|
|
succeeds, and returns the process ID of the waited-for child.
|
|
Applications should avoid relying on this inconsistent,
|
|
nonstandard, and unnecessary feature.
|
|
<A NAME="lbAN"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
|
|
The following program demonstrates the use of
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
|
|
|
|
and
|
|
<B>waitpid</B>().
|
|
|
|
The program creates a child process.
|
|
If no command-line argument is supplied to the program,
|
|
then the child suspends its execution using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+pause">pause</A></B>(2),
|
|
|
|
to allow the user to send signals to the child.
|
|
Otherwise, if a command-line argument is supplied,
|
|
then the child exits immediately,
|
|
using the integer supplied on the command line as the exit status.
|
|
The parent process executes a loop that monitors the child using
|
|
<B>waitpid</B>(),
|
|
|
|
and uses the W*() macros described above to analyze the wait status value.
|
|
<P>
|
|
|
|
The following shell session demonstrates the use of the program:
|
|
<P>
|
|
|
|
|
|
|
|
$<B> ./a.out &</B>
|
|
|
|
Child PID is 32360
|
|
[1] 32359
|
|
$<B> kill -STOP 32360</B>
|
|
|
|
stopped by signal 19
|
|
$<B> kill -CONT 32360</B>
|
|
|
|
continued
|
|
$<B> kill -TERM 32360</B>
|
|
|
|
killed by signal 15
|
|
[1]+ Done ./a.out
|
|
$
|
|
|
|
|
|
<A NAME="lbAO"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/sys/wait.h">sys/wait.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> pid_t cpid, w;
|
|
<BR> int wstatus;
|
|
<P>
|
|
<BR> cpid = fork();
|
|
<BR> if (cpid == -1) {
|
|
<BR> perror("fork");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (cpid == 0) { /* Code executed by child */
|
|
<BR> printf("Child PID is %ld\n", (long) getpid());
|
|
<BR> if (argc == 1)
|
|
<BR> pause(); /* Wait for signals */
|
|
<BR> _exit(atoi(argv[1]));
|
|
<P>
|
|
<BR> } else { /* Code executed by parent */
|
|
<BR> do {
|
|
<BR> w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
|
|
<BR> if (w == -1) {
|
|
<BR> perror("waitpid");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (WIFEXITED(wstatus)) {
|
|
<BR> printf("exited, status=%d\n", WEXITSTATUS(wstatus));
|
|
<BR> } else if (WIFSIGNALED(wstatus)) {
|
|
<BR> printf("killed by signal %d\n", WTERMSIG(wstatus));
|
|
<BR> } else if (WIFSTOPPED(wstatus)) {
|
|
<BR> printf("stopped by signal %d\n", WSTOPSIG(wstatus));
|
|
<BR> } else if (WIFCONTINUED(wstatus)) {
|
|
<BR> printf("continued\n");
|
|
<BR> }
|
|
<BR> } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
|
|
<BR> exit(EXIT_SUCCESS);
|
|
<BR> }
|
|
}
|
|
|
|
<A NAME="lbAP"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+kill">kill</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+ptrace">ptrace</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+signal">signal</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+wait4">wait4</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?5+core">core</A></B>(5),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+credentials">credentials</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7)
|
|
|
|
<A NAME="lbAQ"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="39"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="40"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="41"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="42"><A HREF="#lbAE">wait() and waitpid()</A><DD>
|
|
<DT id="43"><A HREF="#lbAF">waitid()</A><DD>
|
|
</DL>
|
|
<DT id="44"><A HREF="#lbAG">RETURN VALUE</A><DD>
|
|
<DT id="45"><A HREF="#lbAH">ERRORS</A><DD>
|
|
<DT id="46"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="47"><A HREF="#lbAJ">NOTES</A><DD>
|
|
<DL>
|
|
<DT id="48"><A HREF="#lbAK">Linux notes</A><DD>
|
|
<DT id="49"><A HREF="#lbAL">C library/kernel differences</A><DD>
|
|
</DL>
|
|
<DT id="50"><A HREF="#lbAM">BUGS</A><DD>
|
|
<DT id="51"><A HREF="#lbAN">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="52"><A HREF="#lbAO">Program source</A><DD>
|
|
</DL>
|
|
<DT id="53"><A HREF="#lbAP">SEE ALSO</A><DD>
|
|
<DT id="54"><A HREF="#lbAQ">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:35 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|