Waitpid system call]
Function Description:
Wait for the process to change its status. Which of the following calls are used to wait for the sub-process status to change and obtain information about the sub-process whose status has changed. Status change can be considered as: 1. The sub-process has been terminated. 2. The signal causes the sub-process to stop running. 3. signal recovery sub-process execution. When a child process is terminated, wait calls allow the system to release resources associated with the child process. If you do not run wait, the terminated sub-process will stay in the "zombie" status.
If the sub-process changes, these calls will be immediately returned. Otherwise, the call will be blocked until the sub-process status changes or the signal processing handle is interrupted (if the system call does not start again through the sa_restart flag of sigaction ).
A wait System Call suspends the currently running process until one of its sub-processes is terminated. Waitpid suspends the execution of the current process until the status of the specified sub-process changes. By default, waitpid only waits for the child process to terminate, but this behavior can be changed through the option. Waitid system calls provide more precise control over which sub-process to wait for status changes.
The child process stops and the parent process does not perform wait operations on the child process. The child process is transferred to the "Frozen" state. Processes in the "zombie" State retain the minimum amount of information (process identification, termination status, and resource usage information). After that, the parent process can obtain sub-process information when executing wait. As long as the zombie process does not move from the system through wait, it occupies a column in the kernel table. If the progress table is filled up, the kernel will no longer generate new processes. If the parent process has been terminated, its dead child process will be adopted by the INIT process and automatically execute wait to remove them.
Usage:
# Include <sys/types. h>
# Include <sys/Wait. H>
Pid_t wait (int * status );
Pid_t waitpid (pid_t PID, int * status, int options );
Int waitid (idtype_t idtype, id_t ID, siginfo_t * INFOP, int options );
Parameters:
PID: Possible values include:
Less than-1 // means waiting for all sub-processes whose process group ID is equal to the absolute PID value.
-1 // means waiting for any sub-process.
0 // means waiting for any process whose group ID is equal to the call process group ID.
A process greater than 0 // means waiting for its process ID to be equal to PID.
Status: indicates the returned status of the sub-process. You can use the following macro to search for the returned status.
Wifexited (Status) // returns the result of a normal termination of the fruit process, for example, by calling exit (), _ exit (), or from the Return Statement of main.
Wexitstatus (Status) // return the exit status of the sub-process. This should come from the parameter specified when the sub-process calls exit () or _ exit (), or the lowest byte from the return statement parameter in main. It should be used only when wifexited returns true.
Wifsignaled (Status) // return if the fruit process is terminated by the signal
Wtermsig (Status) // returns the number of signals that cause the termination of sub-processes. It should be used only when wifsignaled returns true.
Wcoredump (Status) // returns a result that causes the kernel to be retained. It should be used only when wifsignaled returns true. Not all platforms support this macro, which should be placed inside # ifdef wcoredump... # endif.
Wifstopped (Status) // returns if the signal causes the sub-process to stop running.
Wstopsig (Status) // returns the number of signals that cause the sub-process to stop running. It should be used only when wifstopped returns true.
Wifcontinued (Status) // returns true if the signal causes the sub-process to continue execution.
Options: it can be a combination of 0 or more symbol constants that use the or operation.
Wnohang // if no sub-process exits, return immediately
Wuntraced // If a stopped process exists, the call will be returned.
Wcontinued // If the stopped process continues to run due to the arrival of the sigcont signal, the call will return.
The following are Linux-specific options and cannot be used for waitid.
_ Wclone // only wait for the child process of "clone. A "clone" process is a process that does not send a signal to the parent process upon termination, or does not send a sigchld signal to the parent process.
_ Wall // wait for all types of child processes, including "clone" and "non-clone ".
_ Wnothread // does not wait for the children of other threads in the same thread group.
Wuntraced and wcontinued work only when the sa_nocldstop flag is not set for the sigchld signal.
Idtype, ID: these two parameters are combined to indicate which sub-processes should be waiting.
Idtype = p_pid // The child process that waits for the process ID to match with the ID.
Idtype = p_pgid // wait for any sub-process whose ID matches the process group ID.
Idtype = p_all // wait for any sub-process, ID does not work
The sub-process status change flag that you can specify in the options parameter has the following constants, which can be combined by the or operation.
Wexited // wait for the child process to be terminated.
Wstopped // wait for the sub-process to be executed because the signal has stopped.
Wcontinued // wait for the sub-process that has been resumed due to the signal.
Wnohang // serves as waitpid.
Wnowait // retained the waiting status of the sub-process, and the subsequent wait call can obtain the status information of the sub-process again.
INFOP: When the returned result is successfully executed, waitid will fill in the following fields of the struct to which INFOP points
Si_pid // the ID of the sub-process.
Si_uid // The real User ID of the sub-process.
Si_signo // always set to sigchld.
Si_status // The exit status of the sub-process, or the signal that causes the sub-process to exit, stop, or resume execution, which must be explained according to the si_code field.
Si_code // possible values include cld_exited (sub-process call _ exit), cld_killed (sub-process killed by signal), cld_stopped (signal causes sub-process to stop execution ), cld_continued (signal recovery sub-process continues execution ).
Return description:
Wait (): when the process is successfully executed, the final sub-process identifier is returned. -1 is returned for failure;
Waitpid (): indicates the identity of the child process whose status changes when the execution is successful. If the wnohang flag is specified and the Process status specified by the PID does not change, 0 is returned.
Waitid (): If the execution succeeds or the wnohang flag is set and the sub-process status specified by ID does not change, 0 is returned. -1 is returned for failure.
The error values may include the following:
Echild: the process specified by the parameter does not exist or is not the sub-process that calls the process.
Eintr; wnohang is not set, while capturing an unblocked signal or sigchld Signal
The options VAL: Options parameter is invalid.
Example:
$./A. Out &
Child PID is 32360
[1] 32359
$ Kill-Stop 32360
Stopped by signal 19
$ Killed-cont 32360
Continued
$ Kill-term 32360
Killed by signal 15
[1] + done./A. Out
$
# Include <sys/Wait. H>
# Include <stdlib. h>
# Include <unistd. h>
# Include <stdio. h>
Int main (INT argc, char * argv [])
{
Pid_t CPID, W;
Int status;
CPID = fork ();
If (CPID =-1) {perror ("fork"); exit (exit_failure );}
If (CPID = 0) {/* sub-process execution */
Printf ("Child PID is % LD/N", (long) getpid ());
If (argc = 1)
Pause ();/* Wait for signals */
_ Exit (atoi (argv [1]);
} Else {/* parent process execution */
Do {
W = waitpid (CPID, & status, wuntraced | wcontinued );
If (W =-1) {perror ("waitpid"); exit (exit_failure );}
If (wifexited (Status )){
Printf ("exited, status = % d/N", wexitstatus (Status ));
} Else if (wifsignaled (Status )){
Printf ("killed by signal % d/N", wtermsig (Status ));
} Else if (wifstopped (Status )){
Printf ("stopped by signal % d/N", wstopsig (Status ));
} Else if (wifcontinued (Status )){
Printf ("continued/N ");
}
} While (! Wifexited (Status )&&! Wifsignaled (Status ));
Exit (exit_success );
}