What does the second parameter of waitpid() mean?

c pid
17,587

Solution 1

It is a bit-field for options, the only one available is WNOWAIT, which means to leave the child in a waitable state; a later wait call can be used to again retrieve the child status information.

See: http://linux.die.net/man/2/waitpid

Solution 2

from man pages :

   If  status is not NULL, wait() and waitpid() store status infor-
   mation in the int to which  it  points.   This  integer  can  be
   inspected  with  the  following  macros  (which take the integer
   itself as an argument, not a pointer to it, as is done in wait()
   and waitpid()!):

   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.

   WTERMSIG(status)
          returns  the  number  of the signal that caused the child
          process to terminate.  This macro should only be employed
          if WIFSIGNALED returned true.

   WCOREDUMP(status)
          returns  true  if  the  child produced a core dump.  This
          macro should only be  employed  if  WIFSIGNALED  returned
          true.  This macro is not specified in POSIX.1-2001 and is
          not available on some Unix  implementations  (e.g.,  AIX,
          SunOS).   Only  use this enclosed in #ifdef WCOREDUMP ...
          #endif.

   WIFSTOPPED(status)
          returns true if the child process was stopped by delivery
          of  a  signal; this is only possible if the call was done
          using WUNTRACED or when the child is  being  traced  (see
          ptrace(2)).

   WSTOPSIG(status)
          returns  the  number of the signal which caused the child
          to stop.  This macro should  only  be  employed  if  WIF-
          STOPPED returned true.

   WIFCONTINUED(status)
          (since  Linux  2.6.10)  returns true if the child process
          was resumed by delivery of SIGCONT.

So it stores status of the "how the child terminated".

You can use the macros to investigate how exactly the child terminated, and you can define some actions depending to the child's termination status.

Solution 3

      pid = fork();
      if(pid < 0)
      {
        printf("fork failed\n");
        return -1;
      }
      else if(pid == 0)
      {
        sleep(5);
        printf("Child process\n");
        return 2;
      }
      else
      {
        printf("Parent process\n");
        kill(pid, SIGKILL);
        waitpid(pid, &ret, 0);
        if(WIFEXITED(ret))
          printf("Child process returned normally\n");
        if(WIFSIGNALED(ret))
          printf("Child process terminated by signal\n");
        return 1;
      }

As you can see that the return value can be used to check how a particular process terminated and take actions on the basis of that.

If you comment the kill line from the code, the child process will terminate properly.

Share:
17,587
mko
Author by

mko

Updated on June 10, 2022

Comments

  • mko
    mko almost 2 years

    From a existing question here, someone gave this example code:

    int status;
    child_pid = fork();
    if (child_pid == 0) {
         // in child; do stuff including perhaps exec
    } else if (child_pid == -1) {
         // failed to fork 
    } else {
         if (waitpid(child_pid, &status, 0) == child_pid) {
              // child exited or interrupted; now you can do something with status
         } else {
              // error etc
         }
     }
    

    Could anyone explain to me what the second parameter of waitpid() is used for?