Linux wait Functions

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
It 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.

If the process does not have a child process, an error is returned immediately. The returned value is-1 (note that the wait () function returns immediately, instead of saying that the parent process has ended, the content after the statement in the parent process should still be executed)

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 () will temporarily stop the execution of the current process until a signal arrives or the sub-process ends.
. If the child process has ended when wait () is called, wait () will return immediately
Return the end status of the sub-process. The end status value of the sub-process is returned by the status parameter,
The process identifier of the child process will return quickly. If you do not care about the end status value
The status parameter can be set 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, the sub-process identifier (PID) is returned.
-1. The cause of failure is stored in errno.
Additional instructions
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 there is a signal or sub-process

End. If the child process has ended when waitpid () is called, waitpid () will immediately

Returns the end status of the sub-process. The end status value of the sub-process is returned by the status parameter,

The process identifier of the child process will return quickly. If you do not care about the end status value

The status parameter can be set 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:

Wnohang returns immediately if no child process has been completed.

Wait.

Wuntraced: If the sub-process is suspended, it will return immediately, but it will end.

The status is ignored.

The sub-process's end state is returned and stored in status. There are several macros under it to identify the end state.

Condition:

Wifexited (Status) is not 0 if the sub-process ends normally.

Wexitstatus (Status) gets the end code returned by the sub-process exit (),

Then, 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) obtains the signal code of the sub-process terminated by the signal. Generally

This macro is used only after wifsignaled is used for determination.

Wifstopped (Status) 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. Generally

This macro is 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, the sub-process identifier (PID) is returned.

-1. The cause of failure is stored in errno.

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.