In UNIX, the parent process handles the exit status of a child process using the wait() or waitpid() system calls. These system calls allow the parent process to collect information about the termination status of its child processes. Here’s a brief overview of how the parent process handles the exit status of a child process:
-
wait()System Call:- The
wait()system call is a blocking call that causes the parent process to wait until one of its child processes terminates. When a child process terminates, its exit status and process ID are collected by the parent through thewait()system call.
#includepid_t wait(int *status); - The
statusparameter is a pointer to an integer where the exit status of the terminated child process will be stored.
- The
-
waitpid()System Call:- The
waitpid()system call provides more flexibility thanwait()by allowing the parent process to wait for a specific child process or for any child process in a specified process group. It also supports options for non-blocking behavior.
#includepid_t waitpid(pid_t pid, int *status, int options); - The
pidparameter specifies which child process to wait for. Ifpidis set to -1, the call waits for any child process. Thestatusparameter stores the exit status, and theoptionsparameter provides additional behavior options.
- The
-
Checking Exit Status:
-
After the
wait()orwaitpid()call returns, the parent process can check the exit status of the terminated child process. The exit status contains information about how the child process terminated, such as whether it exited normally or encountered an error. -
The
WIFEXITED(status)macro checks if the child process terminated normally, andWEXITSTATUS(status)retrieves the exit status.
if (WIFEXITED(status)) { // Child process terminated normally int exit_status = WEXITSTATUS(status); // Process exit_status as needed }- Other macros, such as
WIFSIGNALED(status),WTERMSIG(status), andWCOREDUMP(status), provide information about abnormal terminations due to signals.
-
-
Handling Multiple Child Processes:
- If a parent has multiple child processes, it can use a loop to repeatedly call
wait()orwaitpid()to collect exit statuses for each terminated child process.
while ((pid = wait(&status)) > 0) { // Process exit status } - If a parent has multiple child processes, it can use a loop to repeatedly call
-
Non-Blocking Behavior:
- To avoid blocking the parent process while waiting for child processes, the
waitpid()system call can be used with theWNOHANGoption. This allows the parent to continue executing other tasks while periodically checking for terminated child processes.
pid_t pid = waitpid(-1, &status, WNOHANG); if (pid > 0) { // Process exit status } - To avoid blocking the parent process while waiting for child processes, the
By using these system calls and associated macros, the parent process in UNIX can effectively handle the exit status of its child processes, allowing it to respond appropriately to the termination of child tasks and avoid leaving them in the zombie state.