Linux wait Function Analysis

Source: Internet
Author: User

 

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.

Wait (waiting for the sub-process to be interrupted or terminated)
Waitpid, fork
Header file
# Include <sys/types. h>
# Include <sys/Wait. H>
Define the function pid_t wait (int * status );
Function Description
Wait () temporarily stops the execution of the current 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. For the end status values of sub-processes, see waitpid ().
Return Value
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.


Example 1
# Include <stdlib. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/Wait. H>
Int main ()
{
Pid_t PID;
Int status, I;
If (Fork () = 0 ){
Printf ("This is the child process. PID = % d \ n", getpid ());
Exit (5 );
} Else {
Sleep (1 );
Printf ("This is the parent process, wait for child... \ n ";
PID = wait (& status );
I = wexitstatus (Status );
Printf ("child's pid = % d. Exit status = % d \ n", PID, I );
}
}

Run
This is the child process. PID = 1501
This is the parent process. Wait for child...
Child's pid = 1501, exit status = 5


Example 2

# Include <iostream>
# Include <unistd. h>
# Include <sys/Wait. H>
Using namespace STD;
Int main (void)
{
Pid_t PID;
PID = fork ();
If (PID <0)
Exit (0 );
Else if (pid = 0)
{
// If the sub-process is sleeping for 20 seconds
Cout <"Children:" <getpid () <Endl;
Sleep (20 );
}
Else
{Cout <"Hello! I'm parent process! "<Endl;
// If the parent process is waiting here
Pid_t Pr = wait (null );
Cout <Pr <Endl;
}
Return 0;
}

Waitpid (waiting for sub-process interruption or termination)
Related functions: Wait, fork
Header file
# Include <sys/types. h>
# Include <sys/Wait. H>
Define the function pid_t waitpid (pid_t PID, int * status, int options );
Function Description
Waitpid () will temporarily stop the execution of the current process until a signal arrives or the child process ends. If the child process has ended when waitpid () is called, waitpid () 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.

The PID parameter is the identifier of the child process to wait. Other values are as follows:
PID <-1 any child process that waits for the Process Group Identifier to be the absolute value of the PID.
PID =-1 waits for any sub-process, which is equivalent to wait ().
PID = 0 wait for any sub-process with the same process ID as the current process.
PID> 0: Wait for any sub-process whose ID is PID.

The option parameter can be 0 or the following or combination:
If wnohang does not have any child process that has ended, it will return immediately and will not wait.
Wuntraced: If the sub-process is paused, it will return immediately, but the end state will not be ignored.

The sub-process end status is returned and stored in status. The following macros determine the end state:
Wifexited (Status) is not 0 if the sub-process ends normally.
Wexitstatus (Status) gets the end code returned by the sub-process exit (). Generally, wifexited is used to determine whether the macro can be used until it ends normally.
Wifsignaled (Status) If the sub-process ends because of a signal, the macro value is true.
Wtermsig (Status) gets the signal code of the sub-process terminated due to the signal. Generally, this macro is used only after wifsignaled is used for determination.
Wifstopped (Status) If the sub-process is paused, the macro value is true. This is generally the case only when wuntraced is used.
Wstopsig (Status) gets the signal code that causes the sub-process to pause. This macro is generally used only after wifstopped is used for determination.

Return Value
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.

For more information, see Wait ().

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.