Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the second parameter of waitpid() mean?

Tags:

c

pid

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?

like image 440
mko Avatar asked Aug 06 '12 08:08

mko


People also ask

What are the parameters of Waitpid?

Parameters. (Input) A process ID or a process group ID to identify the child process or processes on which waitpid() should operate. (Input) Pointer to an area where status information about how the child process ended is to be placed. (Input) An integer field containing flags that define how waitpid() should operate.

How many parameters are there in Waitpid () system call?

Basically you have 3 parameters: pid is the process you are waiting for.

What does Waitpid () return?

Returned value If successful, waitpid() returns a value of the process (usually a child) whose status information has been obtained. If WNOHANG was given, and if there is at least one process (usually a child) whose status information is not available, waitpid() returns 0.

What is significance of Waitpid () system call what are its parameters?

The waitpid() system call suspends execution of the current process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behaviour is modifiable via the options argument, as described below.


1 Answers

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.

like image 131
Aftnix Avatar answered Oct 08 '22 02:10

Aftnix