wait

Waits for a child process to terminate.

Library:LibC
Classification:POSIX
Service:General C Services

Syntax

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

Parameters

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.

Return Values

When the status of a child process is available, returns the pid of the process.

If wait 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

63

EINTR

The function was interrupted.

70

ECHILD

The calling process has no existing child process.

Remarks

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.

See Also

waitpid