Wait and Waitpid detailed

Source: Internet
Author: User
Tags function prototype

The function prototype for wait is:

#include <sys/types.h>

#include <sys/wait.h>


pid_t Wait (int *status)

Once the process has called wait, immediately block yourself, by wait to automatically analyze whether a subprocess of the current process has exited, if it finds such a child process that has become a zombie, wait collects the information of the child process, and then returns it after it has been completely destroyed, and if no such child process is found, Wait will always be blocked here until one appears.

Parameter status is used to save some states when the collection process exits, which is a pointer to the type int. But if we don't care about how this subprocess died, just want to wipe out the zombie process (in fact, in most cases, we would think so), we can set this parameter to NULL, as follows:

PID = Wait (NULL);

If successful, wait returns the process ID of the child process being collected, and if the calling process does not have a subprocess, the call fails, at which point the wait returns-1 while errno is placed as echild.

The Waitpid function prototype is:

The prototype of the WAITPID system call in the Linux function library is:

#include <sys/types.h> #include <sys/wait.h>


pid_t waitpid (pid_t pid,int *status,int options)

In essence, the system calls Waitpid and wait is exactly the same, but waitpid two more user-controlled parameter PID and options, thus providing us with another more flexible way of programming.

Let's take a look at these two parameters in detail:

PID from the parameter of the name PID and type pid_t can be seen, here is the need for a process ID. But when the PID takes a different value, there are different meanings here.

Pid>0, only wait for the process ID equal to the PID of the child process, no matter how many other children already have run the end of the exit, as long as the specified subprocess has not finished, Waitpid will continue to wait.

When Pid=-1, wait for any child process to exit without any restrictions, at which point the waitpid and waiting function exactly the same.

When pid=0, wait for any child processes in the same process group, and Waitpid will not ignore it if the child process has joined another process group.

When pid<-1, wait for any child processes in the specified process group, the ID of the process group equals the absolute value of the PID.

Options options provide some additional options to control Waitpid, which currently supports only Wnohang and wuntraced two options in Linux, which are two constants that can be used with "|" Operators to connect them to use, such as:

Ret=waitpid ( -1,null,wnohang | wuntraced);

If we do not want to use them, we can also set the options to 0, such as: Et=waitpid ( -1,null,0);     If you use the Wnohang parameter to call Waitpid, it will return immediately, even if no child process exits, and will not wait as long as waiting.    and wuntraced parameters, due to some tracking debugging knowledge, coupled with very little use, here is not a lot of ink, interested readers can access the relevant materials. See here, smart readers may have seen the clue--wait is not a packaged waitpid it. Yes, look < kernel source directory >/include/unistd.h file 349-352 will find the following program segment:

Static inline pid_t Wait (int * wait_stat)

{

Return Waitpid ( -1,wait_stat,0);

}

The return value of the returned value and error waitpid are slightly more complex than wait, with a total of 3 cases:

When normal returns, WAITPID returns the process ID of the child process being collected;

Returns 0 if the option Wnohang is set, and Waitpid in the call discovers that no child processes have been evicted to collect;

If an error occurs in the call, returns-1, at which point the errno is set to the appropriate value to indicate the error, and when the PID indicates that the subprocess does not exist, or that the process exists, but is not a child of the calling process, WAITPID returns with an error, when errno is set to echild the other: call Wait&waitpid to handle terminated child processes:

id_t Wait (int * statloc); pid_t waitpid (pid_t pid,int *statloc, int options);

All two functions return two values: The return value of the function and the terminated subprocess ID, while the state of the child process termination is returned through the Statloc pointer.

The difference between wait&waitpid is obvious, wait waits for the first aborted subprocess, and waitpid can specify waiting for a specific subprocess. This distinction may be more pronounced in the following situations: when there are 5 customers connected to the server, which means that there are five child processes corresponding to 5 customers, at this time, five customers almost at the same time request to terminate, so that almost at the same time, five fin to the server, the same, Five SIGCHLD signals arrive at the server, however, the UNIX signal is often not queued, obviously, the signal processing function will only be executed once, leaving the remaining four child processes as zombie processes residing in the kernel space. At this point, the correct solution is to use Waitpid ( -1, &stat, Wnohang) to prevent the zombie process from leaving behind.

Where the PID is-1 indicates the child process waiting for the first aborted, and the Wnohang selection notifies the kernel not to block when no process item has been terminated.

Wait&waitpid difference:

WAITPID provides 3 features that are not implemented by the wait function: Waitpid waits for a specific subprocess, while waiting returns a subprocess of any terminating state; Waitpid provides a non-blocking version of wait; Waitpid supports job control (with the wuntraced option). Macros used to check for wait and waitpid two functions to return to the signaled state: the child process status returned by both functions is saved in the Statloc pointer, and the following 3 macros allow you to check the status: Wifexited (status): True if terminated normally. Executable Wexitstatus (status): Takes the child process to the lower 8 bits of the exit or _exit parameter.

Wifsignaled (status): True if the exception is terminated.

At this time executable Wtermsig (status): Takes the signal number that terminates the child process.

wifstopped (status): True if the child process is currently paused. At this time you can

Execute Wstopsig (status): Takes a signal number to suspend a child process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.