Process Control (vii)---wait and waitpid functions

Source: Internet
Author: User
Tags terminates

the termination of a process can be divided into abnormal termination and normal termination.
    • When the process is terminated normally, the child process passes the Exit function to the parent process to pass the terminating state;
    • When the process is terminated abnormally, the kernel (not the process) is passed to the parent process with a signal number value.
so, regardless of whether the child process terminates normally or terminates abnormally, the parent process can get to the terminating state of the child process through the wait or Waitpid function, through the parameter status. The kernel sends a SIGCHLD signal to the parent process, regardless of whether the child process terminates gracefully or abnormally, or if the child process is paused, but the parent process ignores the default action for this signal. Of course we can also capture the signal. =====================================================function Prototypes:function Parameters:
    • Status: Points to the pointer used to hold the terminating state of the child process.
return Value:
    • Returns the ID number of the terminated child process when the call succeeds
    • Returns 1 when the call fails, and an error occurs when the parent process does not have a child process
The wait function can be used in the parent process to wait for the child process to terminate, and if the child process does not terminate, the parent process is blocked, the parent process can have more than one child process, but the wait function returns immediately after the termination of a sub-process and gets the status value of the child process termination through status. If you do not care about the terminating state of the child process, you can pass NULL. Note that the wait function returns only when the child process terminates, such as when a child process transitions from a running state to a paused state and the wait function does not return, and the parent process is still blocked. =====================================================function Prototypes:function Parameters:
    • PID: Used to decide which sub-processes to wait for
    • Status: Pointer to the end state of the child process to hold
    • Options: Some settings that you can set, such as non-blocking
return Value:
    • The process ID of the terminating child process is returned when the call succeeds.
    • Return 1 if the call fails
    • When the options are set to Wnohang, if no child process terminates, return 0 immediately

The function of the Waitpid function is much more powerful than the wait function, which is analyzed in detail below:the meaning of the parameter PID, the wait function waits for all child processes, and the Waitpid function can specify the waiting sub-process through the PID function, the PID can have the following several values:
    • PID =-1: Represents waiting for all child processes, and the wait function works the same
    • PID <-1: Represents a child process waiting for all process group numbers and PID absolute values to be equal
    • PID = 0: Represents a child process waiting on the process group number and calling the process ID equal
    • PID > 0: Represents a child process waiting for process ID equals PID
The parameters options can be set to 0 or a combination of Wnohang, wuntraced two macros (using BITS or operators). The usual use is 0 and Wnohang.
    • Wnohang: Represents that when no child process terminates, the Waitpid function does not block, but returns immediately. If not set, the default Waitpid function is blocked.
Call error Condition:
    • The wait function will only invoke an error in one case, that is, when the calling process has no child process.
    • The Waitpid function has a bit more error than the wait function, such as the specified process group is not the child process of the calling process, and an error occurs when there is no child process.

Process Control (vii)---wait and waitpid functions

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.