Turn from: http://blog.csdn.net/anxuegang/article/details/6636400
Waitpid system Call "
Function Description:
Waits for the process to change its state. All of the following calls are used to wait for a change in the state of the child process to obtain information about the child process that the state has changed. The state change can be considered to be: 1. The child process has been terminated. 2. The signal causes the child process to stop executing. 3. Implementation of signal recovery sub process. In the case of a child process termination, the wait call will allow the system to release the resources associated with the child process. If you do not perform a wait, the aborted subprocess will remain in the "zombie" state.
If the child process is found to have changed state, the calls are returned immediately. Conversely, the call is blocked until the child process state changes or is interrupted by the signal processing handle (if the system call is not restarted through the Sigaction Sa_restart flag).
The wait system call suspends the process in the current execution until one of its child processes terminates. Waitpid suspends execution of the current process until the specified child process state changes. By default, Waitpid only waits for the aborted state's subprocess, but this behavior can be changed by an option. Waitid system calls provide more granular control over which child process state changes are waiting for.
The child process has been terminated, and the parent process has not yet performed a wait operation on it, and the child process is transferred to the zombie state. A process in which the kernel has a "zombie" state retains the least amount of information (process identity, termination status, Resource usage information), and then the parent process can get child process information when it executes wait. As long as the zombie process is not removed from the system by a wait, it will occupy a field in the kernel process table. If the process table is filled, the kernel will no longer be able to generate new processes. If the parent process terminates, its zombie process is adopted by the INIT process and automatically executes the 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 have the following
Less than-1//means waiting for all the subprocess whose process group identity equals the PID absolute value.
-1//means waiting for any child processes.
0//means waiting for any process whose group identity is equal to the calling process group identity.
A process that is greater than 0//means waiting for its process identity to be equal to PID.
Status: Refers to the return status of the child process, which can be retrieved by using the following macros
wifexited (status)//return True as fruit process terminates normally, for example, by calling exit (), _exit (), or returning from main () return statement.
Wexitstatus (status)//returns the exit status of the child process. This should come from the parameter specified when the child process calls exit () or _exit (), or the lowest byte from the main internal return statement parameter. It should only be used if the wifexited returns True.
wifsignaled (status)//return True as fruit process terminated by signal
Wtermsig (status)//returns the number of semaphores that caused the child process to terminate. It should only be used if the wifsignaled returns True.
Wcoredump (status)//return True as fruit process leads to kernel dump. It should only be used if the wifsignaled returns True. Not all platforms support this macro, and should be placed in #ifdef wcoredump when used ... #endif内部.
wifstopped (status)//returns TRUE if the signal causes the child process to stop executing.
Wstopsig (status)//returns the number of semaphores that caused the child process to stop executing. It should only be used if the wifstopped returns True.
wifcontinued (status)//returns TRUE if the signal causes the child process to continue executing.
Options: Can be a combination of 0 or more of the following symbolic constants through or operations
Wnohang//If no child process exits, return immediately
wuntraced//If there is a process in the stopped state will cause the call to return.
wcontinued//If the stopped process continues to run due to the arrival of the Sigcont signal, the call will return.
The following Linux-specific options are not available for Waitid
__wclone//Wait only for "clone" subprocess. A "clone" process is a process that does not send a signal to the parent process or sends a SIGCHLD signal to the parent process when it terminates.
__wall//waits for all types of child processes, including "clone" and "Non-clone".
__wnothread//will not wait for descendants of other threads of the same thread group.
Wuntraced and wcontinued only work if the SIGCHLD signal does not have a sa_nocldstop flag set.
Idtype,id: These two parameters are combined to indicate which subprocess you should choose to wait for, possibly
Idtype = = p_pid//waits for the process to identify the child process that matches the ID.
Idtype = = P_pgid//waits for the process group to identify any child processes that match the ID.
Idtype = = P_all/wait for any child process, id not function
The child process state change flag that can be specified in the options parameter has the following constants, which can be combined by the OR operation
wexited//waits for the aborted child process.
wstopped//Waits for the child process because the signal has stopped executing.
wcontinued//Waits for the child process that the signal has been resumed for execution.
Wnohang//function as Waitpid.
Wnowait//retains the waiting state of the child process, and subsequent wait calls can retrieve the status information of the subprocess again.
INFOP: When a successful return is performed, Waitid fills the following fields of the structure that the INFOP points to
Si_pid//child process identification.
The real user identity of the Si_uid//subprocess.
Si_signo//is always set to SIGCHLD.
The exit state of the Si_status//subprocess, or the signal that the child process exits, stops executing or resumes execution, and needs to be interpreted according to the Si_code field.
The Si_code//possible value has cld_exited (child process call _exit exit), cld_killed (the child process is signaled), cld_stopped (signal causes the child process to stop executing), cld_continued (signal recovery subprocess continues to execute).
Return Description:
Wait (): When successfully executed, returns the identity of the terminating subprocess. Failure returns-1;
Waitpid (): When executed successfully, returns the subprocess identity of the state change. Failure returns 1 if the Wnohang flag is specified and the process state specified by the PID does not change, 0 is returned.
Waitid (): returns 0 when a successful execution or Wnohang flag is set and the child process state specified by ID is not changed. Failure returned-1.
The error value may have the following
Echild: The process specified by the parameter does not exist or is not a child process of the calling process
Eintr;wnohang is not set to capture a signal or SIGCHLD signal that is not blocked
Invalid einval:options parameter
Example:
$./a.out &
Child PID is 32360
[1] 32359
$ kill-stop 32360
Stopped by signal 19
$ Kill-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) {/* Child Process Execution * *
printf ("Child PID is%ld/n", (long) getpid ());
if (argc = 1)
Pause (); /* Wait for signals *
_exit (Atoi (argv[1));
else {/* Parent process Executing * *
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);
}