I have read the following statement.
The status argument given to _exit() defines the termination status of the process, which is available to the p
When the child terminates, the kernel keeps some information about it. Among this information is the return code.
Meanwhile, the parent hasn't terminated. By calling wait
or waitpid
it simply asks the kernel "Hey, you know that child of mine? What's his status?".
Inside the _exit(int status) function, the process notifies the parent process via SIGCHLD that that it is about to terminate, and the low-order 8 bits of status are made available to the parent. There are three ways in which the exit status is handled
_exit()
doesn't return means that when a process calls _exit()
, that call never returns from it (in this case, because the process terminates).
_exit()
is a system call, and takes an argument. The kernel saves that argument in an internal structure. The kernel has a lightweight structure for processes that have already exited but have not yet been waited-for (zombies). When a process waits for its children via wait()
(another system call), the kernel retrieves that value, and forgets about the zombie.
Whenever a process exits (whether or not by calling _exit(int Exit_Status) ) the kernel sends SIGCHLD function to its parent. the parent may either
1. Ignore the incoming signal
2. Catch it by installing a signal handler
Specifically the parent may catch the exit status by calling the wait()or waitpid() function. In this case the LSB is made available to the parent. Specifically the status may be learnt as follows
int status;
wpid = waitpid(child_pid, &status, WUNTRACED);
Since only the last 8 bits are available it will be logical to mask the upper bit by doing a bitwise and operation with 255. A system defined macro does this for you
WEXITSTATUS(status);
Thus in order to get the child status - you may use after the waitpid statement
printf("child exited, status=%d\n", WEXITSTATUS(status));
Ignoring the SIGCHLD may lead to creation of zombie (defunct) process(es). Setting SA_NOCLDWAIT flag for SIGCHLD does not produce a zombie as the kernel reaps them. However, the code is not portable and its better to use wait system call.