Reprinted please indicate the source: http://blog.csdn.net/muge0913/article/details/7317452
#include <sys/types.h>#include <stdio.h>#include <sys/wait.h>void check_exit(int status);main(){ pid_t pid; int status; if((pid = fork()) < 0) { printf("fork error!!\n"); exit(0); } else if(pid == 0) { printf("child process exit\n"); exit(0); } else { if(wait(&status) != pid) { printf("wait error!!"); exit(0); } check_exit(status); }}void check_exit(int status){ if(WIFEXITED(status)) printf("eixt\n"); else if(WIFSIGNALED(status)) printf("killed by signal\n"); else if(WIFSTOPPED(status)) printf("stopped by signal\n"); else if(WIFCONTINUED(status)) printf("continued");}
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.
Wait (waiting for the sub-process to be interrupted or terminated)
#include<sys/types.h> #include<sys/wait.h> pid_t wait (int * status);
Function Description
Wait () temporarily stops the execution of the current process (suspends the parent process) until a signal arrives or the child process ends. If the child process has ended when wait () is called, wait () immediately returns the child process end status value. The end status value of a sub-process is returned by the status parameter, and the process identifier of the sub-process is returned quickly. If you do not care about the end status value, you can set the parameter status to null. If no sub-process exists in the process that calls wait, the call will fail. For the sub-process end status value, see
Waitpid ()
If the execution is successful, the sub-process identifier (PID) is returned. If an error occurs,-1 is returned. The cause of failure is stored in errno.