Skip to content
Related Articles

Related Articles

Exit status of a child process in Linux
  • Difficulty Level : Medium
  • Last Updated : 27 Mar, 2018

It is known that fork() system call is used to create a new process which becomes child of the caller process.
Upon exit, the child leaves an exit status that should be returned to the parent. So, when the child finishes it becomes a zombie.

Whenever the child exits or stops, the parent is sent a SIGCHLD signal.
The parent can use the system call wait() or waitpid() along with the macros WIFEXITED and WEXITSTATUS with it to learn about the status of its stopped child.

(*)wait() system call : It suspends execution of the calling process until one of its children terminates.
Syntax of wait() system call:

pid_t wait(int *status);

(*)The waitpid() system call : It suspends execution of the calling process until a child specified by pid argument has changed state.
Syntax of waitpid() system call :

pid_t waitpid(pid_t pid, int *status, int options)

Note: By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument such as WIFEXITED, WEXITSTATUS etc.



The value of pid can be :

  1. Less than -1 : Meaning wait for any child process whose process group ID is equal to the absolute value of pid.
  2. Equal to -1 : Meaning wait for any child process.
  3. Equal to 0 : Meaning wait for any child process whose process group ID is equal to that of the calling process.
  4. Greater than 0 : Meaning wait for the child whose process ID is equal to the value of pid.

WIFEXITED and WEXITSTATUS are two of the options which can be used to know the exit status of the child.
WIFEXITED(status) : returns true if the child terminated normally.

WEXITSTATUS(status) : returns the exit status of the child. This macro should be employed only if WIFEXITED returned true.

Below is a C implementation in which child uses execl() function but the path specified to execl() is undefned.
Let us see what is the exit status value of the child that parent gets.




// C code to find the exit status of child process
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
  
// Driver code
int main(void)
{
    pid_t pid = fork();
      
    if ( pid == 0 )
    {
       /* The pathname of the file passed to execl()
          is not defined   */
       execl("/bin/sh", "bin/sh", "-c", "./nopath", "NULL");
    }
  
    int status;
      
    waitpid(pid, &status, 0);
  
    if ( WIFEXITED(status) )
    {
        int exit_status = WEXITSTATUS(status);        
        printf("Exit status of the child was %d\n"
                                     exit_status);
    }
    return 0;
}

Output:


Note : Above code may not work with online compiler as fork() is disabled.

Here the exit status is 127 which indicates that there is some problem with path or there is a typo.

Few exit status codes are listed below for extra information :

  • 1 : Miscellaneous errors, such as “divide by zero” and other impermissible operations.
  • 2 : Missing keyword or command, or permission problem.
  • 126 : Permission problem or command is not an executable
  • 128 : invalid argument to exit.

Note : The C standard does not define the meaning of return codes. Rules for the use of return codes vary on different platforms.

My Personal Notes arrow_drop_up
Recommended Articles
Page :