tags:

views:

62

answers:

2

What is the use of waitpid()?

+5  A: 

It's used generally to wait until a specific process finishes (or otherwise changes state if you're using special flags), based on its process ID (otherwise known as a pid).

It can also be used to wait for any of a group of child processes, either one from a specific process group or any child of the current process.

See here for the gory details.

paxdiablo
+2  A: 

It blocks the calling process until a nominated child process exits (or makes some other transition such as being stopped.)

Typically you will use waitpid rather than generic wait when you may have more than one process and only care about one.

A typical use is

pid_t child_pid;
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
     }
 }
poolie