the function prototypes for wait are:
#include <sys/types.h> #include <sys/wait.h> pid_t Wait (int *status)Once the process has called wait, it immediately blocks itself, and the wait automatically parses if a child process of the current process has exited, and if it finds such a child process that has become a zombie, wait will collect information about the child process and destroy it and return it, if no such child process is found. Wait will always be stuck here until one appears. Parameters:The parameter status is used to hold some state when the collection process exits, which is a pointer to type int. But if we don't care about how this subprocess dies, we just want to get rid of this zombie process (and in most cases we will), we can set this parameter to NULL, just like this:pid = Wait (NULL);return Value:If successful, wait returns the process ID of the child process being collected if the calling process does not have child processes, the call fails, and wait returns-1 while errno is set to Echild.the function prototypes for Waitpid are:#include <sys/types.h> #include <sys/wait.h> pid_t waitpid (pid_t pid,int *status,int options)In essence, system calls Waitpid and wait are exactly the same, but the waitpid has more than two user-controlled parameter PID and options, which gives us another more flexible way to program.Parameters: (Status Ibid.)PID:As can be seen from the parameter's name PID and type pid_t, a process ID is required here. But when the PID takes a different value, there are different meanings here.when Pid>0, just wait for the process ID to be equal to the PID of the child process, no matter how many other child processes have run the end of the exit, as long as the specified child process has not ended, Waitpid will continue to wait. when Pid=-1, waiting for any one of the child processes to exit without any restrictions, at which time the waitpid and wait functions exactly the same. when Pid=0, wait for any child process in the same process group, and Waitpid will not ignore it if the child process has joined another process group. when Pid<-1, waits for any child process in the specified process group, the ID of the process group equals the absolute value of the PID.Options:Options provides some additional option to control Waitpid, which is currently supported only in LinuxWnohangAndwuntracedTwo options, this is a two constant, you can use the "|" Operators to connect them using, for example:Ret=waitpid ( -1,null,wnohang | wuntraced); If we do not want to use them, we can also set options to 0, such as:ret=waitpid ( -1,null,0); If you call Waitpid with the Wnohang parameter, it will return immediately, even if no child process exits, and will not wait forever like wait. And the wuntraced parameter, because involves some trace debugging aspect knowledge, in addition very few uses, here does not have the expense pen and ink, the interested reader may consult the related material on its own. See here, the smart reader may have seen the clue--wait is not the packaging waitpid? Yes, look at the < kernel source directory >/include/unistd.h file 349-352 lines will find the following program segment:
- Static inline pid_t Wait (int * wait_stat)
- {
- Return Waitpid ( -1,wait_stat,0); return values and Errors
- }
return Value:The return value of the waitpid is slightly more complicated than wait, in a total of 3 cases: when returned normally, WAITPID returns the process of the child process that was collected id; if the option Wnohang is set and the Waitpid in the call does not find the child process that has exited can be collected, then 0 is returned; If an error occurs in the call, 1 is returned, errno is set to the appropriate value to indicate the error, when the PID indicates that the child process does not exist, or if the process exists, but not the child process of the calling process, Waitpid will be returned with an error, and errno is set to Echild other: Call Wait&waitpid to handle the aborted child process:pid_t Wait (int * status);pid_t waitpid (pid_t pid,int * status, int options);Two functions return two values: The return value of the function and the terminated child process ID, and the state of the child process's termination is returned by the status pointer. The difference between wait&waitpid is obvious, wait waits for the first terminating child process, and waitpid can specify to wait for a specific child process. The difference may be more pronounced in the following situations:when there are 5 clients connected to the server, that is, five sub-processes corresponding to 5 customers, at this time, five customers almost at the same time request termination, so that, almost simultaneously, five fin to the server, the same, five SIGCHLD signal to the server, however, UNIX signals are often not queued, so it is clear that the signal processing function will only be executed once, leaving the remaining four sub-processes as zombie processes residing in the kernel space. At this point, the right solution is to use Waitpid ( -1, &stat, Wnohang) to prevent the zombie process from leaving. Where the PID is-1 indicates waiting for any child process, and the Wnohang selection notifies the kernel not to block when there are no terminated process entries.wait&waitpid Differences:WAITPID provides 3 functions that the wait function cannot achieve: 1.waitpid waits for a particular child process, while wait returns a subprocess of any terminating state; 2.waitpid provides a non-blocking version of wait; The 3.WAITPID supports job control (with the wuntraced option). A macro used to check that wait and waitpid two functions return a terminating state: The child process states returned by these functions are saved in the status pointer.Use the following 3 macros to check the status:wifexited (status):True if it is normal to terminate. At this point the executable wexitstatus (status): Takes a child process to the lower 8 bits of the exit or _exit parameter.wifsignaled (status):True if the exception is aborted. The executable Wtermsig (status): Takes the signal number that causes the child process to terminate.wifstopped (status):True if the child process is currently paused. Executable Wstopsig (status): Takes a signal number that causes the child process to pauseNote:(online to see the feeling method is very good, recommended)
If you use Wait () and Waitpid () in the parent process to hang the parent process, the workaround is:
(1).you can use the signal function to install handler for SIGCHLD. After the child process finishes, the parent process receives the signal and can call the wait recycle in handler. (2).If the parent process does not care what time the child process ends, then the kernel can be notified with
signal (SIGCLD, sig_ign) or
signal (SIGCHLD, sig_ign) , not interested in the end of the sub-process. After the child process is finished, the kernel recycles and no longer sends a signal to the parent process. (3).Fork two times, the parent process fork a child process, and then continue to work, the child process fork a grandchild process to exit, then the Sun process is init takeover, after the sun process is finished, Init will be recycled. But the recycling of the sub-process to do it yourself.
Wait and waitpid detailed "turn"