gtpc2m9g | C/C++ Language Support User's Guide |
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
- If there are any queued signals for the calling ECB to handle, these
signals are handled after the status of child processes is checked.
Signals received while waiting for child process status are handled as they
are received. This means that if the signal function is used
to install signal handlers, the placement of the wait function in
the code is subject to the same restrictions as the raise
function.
- If the signal function was coded to ignore SIGCHLD signals, the
wait function will not return until all of the child processes of
the callers have exited, at which time it returns a value of -1 with
errno set to ECHILD. Otherwise, the wait function
returns as soon as a child process exits. If a child process has exited
before the wait function is called, the wait function
returns immediately.
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