Reputation: 75
I take a look at wait man page but they only say that status of process is terminated, stop by signal or resume by signal. What if I pass in wstatus = 0. what happend. For example this code make the parent wait for all children to terminate. why status = 0?
pid_t wpid;
int status = 0;
while (wpid = wait(&status) > 0);
Upvotes: 1
Views: 3894
Reputation: 16925
The status
variable is an output parameter in which is encoded
information about the way the child process terminated.
You can test if(WIFEXITED(status))
to determine if the termination was normal;
i.e. due to exit()
or return from main()
.
If the previous condition is true, then you can obtain the integer
value of the exit code (<=255) with exit_code=WEXITSTATUS(status)
.
You can also test if(WIFSIGNALED(status))
to determine if the termination was
due to a signal.
In this case, you can obtain the integer value of the signal (see kill -L
for example) with kill_signal=WTERMSIG(status)
.
If you use waitpid()
, you can also test other special situations like suspend/resume, but this is less common
(see https://linux.die.net/man/2/wait).
Upvotes: 2
Reputation: 43
wait
is system call that makes the parent process
wait (i.e suspends it) for a state change in the child process
. The state change according to the documentation is:
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.
So the status
that is passed into wait
, if not NULL
, then it will be set to a value that provides info on how the child processe's state had changed.
This value can be inspected for more insight using macros like:
WIFEXITED(status) returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().
WEXITSTATUS(status) returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.
WIFSIGNALED(status) returns true if the child process was terminated by a signal.
There are a few more and can be found here.
And in your code,
wpid = wait(&status)
the wpid
is the pid
of the child process that has been terminated (which has to be >0)
Upvotes: 1