Knowledge about wait and waitpid Functions

Source: Internet
Author: User

Wait (waiting for the sub-process to be interrupted or terminated)
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 value of the sub-process, see waitpid ()
If the execution is successful, the sub-process identifier (PID) is returned. If an error occurs, the sub-process identifier (PID) is returned.
Return Value
-1. The cause of failure is stored in errno.

Waitpid (waiting for sub-process interruption or termination)
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 wait () is called, wait () 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:
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's end state is returned and stored in status. There are several macros under it to 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.
If the execution is successful, the sub-process identifier (PID) is returned. If an error occurs, the sub-process identifier (PID) is returned.
Return Value
-1. The cause of failure is stored in errno.


/******
* Waitpid. C-simple wait usage
*********/

# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/Wait. H>
# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Pid_t childpid;
Int status;

Childpid = fork ();

If (-1 = childpid)
{
Perror ("fork ()");
Exit (exit_failure );
}
Else if (0 = childpid)
{
Puts ("in child process ");
Sleep (3); // Let the child process sleep for 3 seconds to see the behavior of the parent process
Printf ("/tchild pid = % d/N", getpid ());
Printf ("/tchild ppid = % d/N", getppid ());
Exit (exit_success );
}
Else
{
Waitpid (childpid, & status, 0 );
Puts ("in parent ");
Printf ("/tparent pid = % d/N", getpid ());
Printf ("/tparent ppid = % d/N", getppid ());
Printf ("/tchild process exited with status % d/N", status );
}
Exit (exit_success );
}

[Root @ localhost SRC] # GCC waitpid. c
[Root @ localhost SRC] #./A. Out
In child process
Child pid = 4469
Child ppid = 4468
In parent
Parent pid = 4468
Parent ppid = 4379.
Child process exited with status 0
[Root @ localhost SRC] #

If you comment out the above "waitpid (childpid, & status, 0);" line, the program execution effect is as follows:
[Root @ localhost SRC] #./A. Out
In child process
In parent
Parent pid = 4481
Parent ppid = 4379.
Child process exited with status 1331234400
[Root @ localhost SRC] # Child pid = 4482
Child ppid = 1

The child process has not exited, and the parent process has exited.

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.