Wait and waitpid

Source: Internet
Author: User

From: http://hi.baidu.com/wlzqi/blog/item/208cebc4b1c02dab8326ac79.html




The wait function is prototype:
  
# Include/* define the pid_t type */
# Include
Pid_t wait (int * Status)
  
 
Once a process calls wait, it immediately blocks itself. Wait automatically analyzes whether a sub-process of the current process has exited. If it finds such a sub-process that has become a zombie,
Wait will collect information about this sub-process and destroy it completely and return it. If such a sub-process is not found, wait will be blocked until one appears.
  
The status parameter is used to save some statuses when the collection process exits. It is a pointer to the int type. But if we don't care about how this sub-process died, we just want to destroy it (in fact, in most cases, we will think like this ), we can set this parameter to null, as shown below:
  
PID = wait (null );
  
If the call succeeds, wait returns the ID of the collected sub-process. If the call process does not have a sub-process, the call fails. In this case, wait returns-1 and errno is set to echild.

The function prototype of waitpid is:


Introduction
The prototype of waitpid system calling in the Linux function library is:
  
# Include/* define the pid_t type */
# Include
Pid_t waitpid (pid_t PID, int * status, int options)


In essence, the functions of the system call waitpid and wait are exactly the same, but the waitpid has two more user-controlled parameters PID and options, this provides us with a more flexible way of programming. The following two parameters are described in detail:
  
● PID
  
From the parameter name PID and type pid_t, we can see that what is needed here is a process ID. But when the PID gets different values, it has different meanings here.
  
When the PID is greater than 0, only the child process whose process ID is equal to the PID is waiting. No matter how many other child processes have ended and exited, as long as the specified child process has not ended, waitpid will keep waiting.
  
When pid =-1, wait for any sub-process to exit without any restrictions. At this time, waitpid and wait play the same role.
  
When pid = 0, wait for any sub-process in the same process group. If the sub-process has been added to another process group, waitpid will not ignore it.
  
PID <-1, wait for any sub-process in a specified process group, the ID of this process group is equal to the absolute value of PID.

● Options

Options provides some additional options to control waitpid. Currently, only the wnohang and wuntraced options are supported in Linux. These two constants can be connected using the "|" operator, for example:


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


If you do not want to use them, you can set options to 0, for example:


Ret = waitpid (-1, null, 0 );
  
If the wnohang parameter is used to call waitpid, even if no sub-process exits, it will return immediately and will not wait forever like wait.
  
However, the wuntraced parameter involves some tracking and debugging knowledge and is rarely used. There is not much to worry about here. Interested readers can check the relevant materials on their own.
  
As you can see, smart readers may already see the clues-isn't wait a packaged waitpid? Check the <kernel source code directory>/include/unistd. h file 349-352 to find the following program segments:
  
Static inline pid_t wait (int * wait_stat)
{
Return waitpid (-1, wait_stat, 0 );
}

Returned values and errors
  
The return value of waitpid is slightly more complex than that of wait. There are three cases:
  
● When the returned result is normal, waitpid returns the ID of the collected sub-process;
  
● If the wnohang option is set, and waitpid in the call finds that no child process has exited to collect data, 0 is returned;
  
● If an error occurs in the call,-1 is returned. errno is set to a value to indicate the error;
  
When the sub-process indicated by the PID does not exist or this process exists but is not a sub-process that calls the process, waitpid will return an error, and errno is set to echild


Others:

Call wait & waitpid to process the terminated sub-process:

Pid_t wait (int * statloc );
Pid_t waitpid (pid_t PID, int * statloc, int options );
Both functions return two values: the return value of the function and the ID of the terminated sub-process. The termination status of the sub-process is returned through the statloc pointer.


The difference between wait and waitpid is obvious. Wait waits for the first child process to terminate, while waitpid can specify to wait for a specific child process. The difference may be more obvious in the following situations:

When five clients connect to the server at the same time, that is to say, five sub-processes correspond to five customers respectively, at this time, the five customers almost simultaneously request termination. In this way, almost at the same time, five fin servers,
Similarly, five sigchld signals arrive at the server. However, Unix signals are usually not queued. Obviously, the signal processing function will only run once, and the remaining four sub-processes will be used
Zombie processes reside in the kernel space. In this case, the correct solution is to use waitpid (-1, & stat,
Wnohang) to prevent zombie processes from being left behind. The PID is-1, indicating that the child process is waiting for the first termination, And the wnohang option notifies the kernel not to block when no terminated process item exists.
.


Differences between wait and waitpid

Waitpid provides three functions that cannot be implemented by Wait functions:


Waitpid waits for a specific sub-process, and wait returns any terminated sub-process;
Waitpid provides a non-blocking version of wait;
Waitpid supports job control (with the wuntraced option ).
Macro used to check the termination status returned by the wait and waitpid functions:

The sub-process statuses returned by these two functions are saved in the statloc pointer. You can use the following three macros to check the status:


Wifexited (Status): true if it is terminated normally. This can be executed
Wexitstatus (Status): takes the 8-bit lower value that the sub-process sends to the exit or _ exit parameter.
Wifsignaled (Status): true if an exception is terminated. This can be executed
Wtermsig (Status): gets the signal number that causes the sub-process to terminate.
Wifstopped (Status): If the sub-process is currently suspended, it is true.
Wstopsig (Status): gets the signal number that suspends the sub-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.