|
|
HP-UX Reference Volume 3 of 5 > wwait(2) |
|
NAMEwait, waitpid — wait for child process to stop or terminate SYNOPSIS
#include <sys/types.h> OH #include <sys/wait.h> pid_t wait(int *stat_loc); pid_t waitpid(pid_t pid, int *stat_loc, int options); DESCRIPTIONThe wait() and waitpid() functions allow the calling process to obtain status information pertaining to one of its child processes. Various options permit status information to be obtained for child processes that have terminated or stopped. If status information is available for two or more child processes, the order in which their status is reported is unspecified. The wait() function will suspend execution of the calling process until status information for one of its terminated child processes is available, or until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process. If status information is available prior to the call to wait(), return will be immediate. The waitpid() function will behave identically to wait(), if the pid argument is (pid_t)-1 and the options argument is 0. Otherwise, its behaviour will be modified by the values of the pid and options arguments. The pid argument specifies a set of child processes for which status is requested. The waitpid() function will only return the status of a child process from this set:
The options argument is constructed from the bitwise-inclusive OR of zero or more of the following flags, defined in the header <sys/wait.h>.
If the calling process has SA_NOCLDWAIT set or has SIGCHLD set to SIG_IGN, and the process has no unwaited for children that were transformed into zombie processes, it will block until all of its children terminate, and wait() and waitpid() will fail and set errno to ECHILD. If wait() or waitpid() return because the status of a child process is available, these functions will return a value equal to the process ID of the child process. In this case, if the value of the argument stat_loc is not a null pointer, information will be stored in the location pointed to by stat_loc. If and only if the status returned is from a terminated child process that returned 0 from main() or passed 0 as the status argument to _exit() or exit(), the value stored at the location pointed to by stat_loc will be 0. Regardless of its value, this information may be interpreted using the following macros, which are defined in <sys/wait.h> and evaluate to integral expressions; the stat_val argument is the integer value pointed to by stat_loc.
If the information pointed to by stat_loc was stored by a call to waitpid() that specified the WUNTRACED flag and did not specify the WCONTINUED flag, exactly one of the macros WIFEXITED(*stat_loc), WIFSIGNALED(*stat_loc), and WIFSTOPPED(*stat_loc) will evaluate to a non-zero value. If the information pointed to by stat_loc was stored by a call to waitpid() that specified the WUNTRACED and WCONTINUED flags, exactly one of the macros WIFEXITED(*stat_loc), WIFSIGNALED(*stat_loc), WIFSTOPPED(*stat_loc), and WIFCONTINUED (*stat_loc) will evaluate to a non-zero value. If the information pointed to by stat_loc was stored by a call to waitpid() that did not specify the WUNTRACED or WCONTINUED flags, or by a call to the wait() function, exactly one of the macros WIFEXITED(*stat_loc) and WIFSIGNALED(*stat_loc) will evaluate to a non-zero value. If the information pointed to by stat_loc was stored by a call to waitpid() that did not specify the WUNTRACED flag and specified the WCONTINUED flag, by a call to the wait() function, exactly one of the macros WIFEXITED(*stat_loc), WIFSIGNALED(*stat_loc), and WIFCONTINUED(*stat_loc) will evaluate to a non-zero value. There may be additional implementation-dependent circumstances under which wait() or waitpid() report status. This will not occur unless the calling process or one of its child processes explicitly makes use of a non-standard extension. In these cases the interpretation of the reported status is implementation-dependent. If a parent process terminates without waiting for all of its child processes to terminate, the remaining child processes will be assigned a new parent process ID corresponding to an implementation-dependent system process. RETURN VALUEIf wait() or waitpid() returns because the status of a child process is available, these functions will return a value equal to the process ID of the child process for which status is reported. If wait() or waitpid() returns due to the delivery of a signal to the calling process, -1 will be returned and errno will be set to EINTR. If waitpid() was invoked with WNOHANG set in options, it has at least one child process specified by pid for which status is not available, and status is not available for any process specified by pid, 0 will be returned. Otherwise, (pid_t)-1 will be returned, and errno will be set to indicate the error. ERRORSThe wait() function will fail if:
The waitpid() function will fail if:
APPLICATION USAGEThreads ConsiderationsIn a multi-threaded application, only the calling thread is suspended by wait() or waitpid(). wait() and waitpid() will not return until all threads in the process have reached the desired state. For example, wait() and waitpid() will not return until all threads have terminated. If the WUNTRACED or WCONTINUED options are specified for waitpid(), the function will not return until all threads have stopped or continued respectively. Issue 4The following change is incorporated for alignment with the ISO POSIX-1 standard:
Other changes are incorporated as follows:
Issue 4, Version 2The following changes are incorporated in the DESCRIPTION for X/OPEN UNIX conformance:
wait()If a parent process terminates without waiting for its child processes to terminate, the parent process ID of each child process is set to 1. This means the initialization process inherits the child processes.
waitpid()
NotesEarlier HP-UX versions documented the bit encodings of the status returned by wait() rather than the macros WCOREDUMP, WEXITSTATUS, WIFEXITED, WIFSIGNALED, WIFSTOPPED, WSTOPSIG, and WTERMSIG. Applications using those bit encodings will continue to work correctly. However, new applications should use the macros for maximum portability. In earlier HP-UX versions, the macros WIFEXITED, WIFSIGNALED, and WIFSTOPPED have the same definitions as the correspondingly named macros in the BSD 4.3 and earlier systems. Existing applications that depend on these definitions will continue to work correctly. However, if the application is recompiled, the feature test macro _BSD must be turned on for the compilation so that the old definitions of these macros are obtained. New definitions of these macros are in effect by default. The only difference between the old and new definitions is the type of the argument. Type union wait is used in the BSD definitions while type int is used in the default definitions. ERRORSIf wait() or waitpid() fails, errno is set to one of the following values.
WARNINGSThe behavior of wait() and waitpid() is affected if the SIGCLD signal is set to SIG_IGN. See the WARNINGS section of signal(5). Signal handlers that cause system calls to be restarted can affect the EINTR condition described above (see bsdproc(3C), sigaction(2), and sigvector(2)). |
|