gtpc2m9gC/C++ Language Support User's Guide

wait-Wait for Status Information from a Child Process

This function causes the calling process to be suspended until one of its child processes exits.

Format

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

stat_loc
A pointer to an integer where the wait function will return the status of the child process. If the wait function returns because the status of a child process is available, the return value will equal the process identifier (ID) of the exiting process. For this, if the value of stat_loc is not NULL, information is stored in the location pointed to by stat_loc. If status is returned from a terminated child process that returned a value of 0, the value stored at the location pointed to by stat_loc will be 0. If the return is greater than 0, you can evaluate this information using the following macros:

WEXITSTATUS

WIFEXITED

WIFSIGNALED

WTERMSIG.

Normal Return

If successful, the return code is set to the process ID of the process for which status is available.

Error Return

If unsuccessful, the wait function returns a value of -1 and sets errno to the following:

ECHILD
The calling process does not have any child processes that need to be handled by the wait function.

Programming Considerations

Examples

The following example creates a child process and then calls the wait function to ensure that the child process was completed successfully.

#include <sysapi.h>
#include <signal.h>
#include <sys/wait.h>

·
·
·
pid_t child_pid; pid_t rc_pid; int chld_state; /* Create a child process*/
·
·
·
child_pid = tpf_fork(&create_parameters);
·
·
·
/* Check status of child process*/ rc_pid = wait( &chld_state ); if (rc_pid > 0) { if (WIFEXITED(chld_state)) { printf("Child exited with RC=%d\n",WEXITSTATUS(chld_state)); } if (WISIGNALED(chld_state)) { printf("Child exited via signal %d\n",WTERMSIG(chld_state)); } } else /* if no PID returned, then an error */ { if (errno == ECHILD) { printf("No children exist.\n"); } else { printf("Unexpected error.\n"); abort(); } }
·
·
·

Related Information