waitpid

Waits for the specified child process to terminate.

Library:LibC
Classification:POSIX
Service:General C Services

Syntax

  #include <sys/wait.h> 
   
  pid_t waitpid (
     pid_t   pid,
     int    *stat_loc,
     int     options);
  

Parameters

pid

(IN) Specifies the child process:

  • If set to -1 or 0, specifies any child process. This is equivalent to the wait function.

  • If set to positive integer value, specifies the process ID of a single child process.

stat_loc

(OUT) Points to where the status information of the child process is returned. If a NULL pointer is passed, status information is not returned.

options

(IN) Indicates whether the calling thread should be suspended until status is available.

  • If set to WNOHANG (1), waitpid returns immediately, even when status is not available for the process specified by pid.

  • If set to 0, the calling thread is suspended until status is available.

Return Values

If set to wait for the status, returns the pid of the process when the status of the process is available.

If set not to wait with the WNOHANG option, returns 0 if the status of the specified pid is not available.

If waitpid returns because of an error or because the calling thread was interrupted, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

9

EINVAL

The options parameter is not valid.

63

EINTR

The function was interrupted.

70

ECHILD

The calling process has no existing child process.

Remarks

IMPORTANT:Remember that a detached process is not a child process. If you create a detached process and then use waitpid with the value returned by procve or procle to wait for that process to exit, your application will hang.

The stat_loc parameter returns 0 if the child process terminated by one of the following methods:

  • The process returned 0 from main.

  • The process called _exit or exit with a status argument of 0.

  • The process terminated because the last thread of the process terminated.

The following two macros can be used to evaluate the stat_loc value. The status parameter is the value returned in stat_loc.

WIFEXITED (status)

Evaluates to a nonzero value if status indicates that the child process terminated normally.

WEXITSTATUS (status)

If the value of WEXITSTATUS (status) is nonzero, the macro evaluates to the low-order 8 bits of the status argument which the child process passed to _exit or exit or the value the child process returned from main.

For sample code, see ProcXe.c.

See Also

wait