What does the second parameter waitpid () mean?

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

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 } } 

Can someone explain to me what the second waitpid() parameter is used for?

+4
source share
3 answers

This is a bit-field for options; the only available is WNOWAIT, which means leaving the child in a waiting state; a later wait call can be used to retrieve information about the status of the child.

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

+2
source

from the 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 (eg, 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. 

Thus, he retains the status of "how the child ends."

You can use macros to examine exactly how the child ended, and you can define some actions depending on the completion status of the child process.

+6
source
  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, the return value can be used to check how a particular process terminates and takes action based on this.

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

+2
source

All Articles