Home

WEXITSTATUS

WEXITSTATUS is a macro used in POSIX-compliant systems to obtain the exit status of a child process from the status value returned by wait or waitpid. It is defined in the header sys/wait.h and is used in conjunction with other wait-related macros such as WIFEXITED and WIFSIGNALED.

Usage and behavior

After a call to wait or waitpid, you receive an int status. You should first check whether

The status value can also indicate other termination reasons, such as termination by a signal. In that

Portability and notes

WEXITSTATUS, along with WIFEXITED and related macros, are part of the POSIX wait macros and are available

Example

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

pid_t pid = fork();

if (pid == 0) { _exit(2); }

int status;

waitpid(pid, &status, 0);

if (WIFEXITED(status)) {

int code = WEXITSTATUS(status); // code == 2

}

the
child
terminated
normally
by
using
WIFEXITED(status).
If
this
returns
true,
the
exit
status
can
be
retrieved
with
WEXITSTATUS(status).
The
value
returned
by
WEXITSTATUS
is
an
integer
in
the
range
0
to
255,
representing
the
argument
passed
to
exit
or
_exit
by
the
child
process.
WEXITSTATUS
is
only
meaningful
if
WIFEXITED(status)
is
true;
using
it
otherwise
yields
an
unspecified
result.
case,
WIFSIGNALED(status)
would
be
true
and
information
about
the
terminating
signal
can
be
obtained
with
WTERMSIG(status).
Other
macros,
such
as
WIFSTOPPED
and
WSTOPSIG,
provide
additional
information
on
stopped
processes.
on
systems
that
provide
the
POSIX
wait
interfaces.
Include
sys/wait.h
to
access
them.
While
the
exact
internal
encoding
of
the
status
value
is
implementation-defined,
the
macros
abstract
away
the
details,
offering
a
portable
way
to
interpret
child
termination
outcomes.