child_status


The following program forks a child process and then uses the waitpid system call to reap the child and inspect the reason why the child terminated. In particular, the program uses the WIFEXITED macro to see if the child process terminated normally (through a return from main or a call to exit/_exit) and, if so, uses WEXITSTATUS to retrieve the child's exit status. Similarly, the program uses the WIFSIGNALED macro to determine if the process was killed by a signal, and, if so, retrieves the signal number with WTERMSIG.

If the program is run without any arguments, the child process calls pause to suspsend its execution until it receives (that is, is killed by) a signal. If called with one argument, the argument is the value the child should pass to _exit; rather than pause, the child immediately terminates with this exit status.

child_status.c