Wait () and Waitpid ()
Function description
The wait () function is used to block the parent process (that is, the process that calls wait () until a child process finishes or the process receives a specified signal. If the parent process has no child processes or its child processes have ended, the wait () function returns immediately.
Waitpid () acts like Wait (), but it does not have to wait for the first terminating child process (which can specify a child process that needs to wait for finalization), and it has several options, such as providing a non-blocking version of the Wait () feature, and also supporting job control. In fact, the wait () function is just a special case of the Waitpid () function, and the Waitpid () function is called directly when implementing the Wait () function inside Linux.
function format
Format of the wait () function
is the format of the Waitpid () function
Basic experiments
Experiment 1
In this experiment, you first create a child process using fork () and then let its child process pause 5s (using the Sleep () function). Next, use the Waitpid () function for the original parent process, and use the parameter Wnohang that the parent process will not block. If a child process exits, waitpid () returns the child process number, or waitpid () returns 0 if no child process exits, and the parent process is judged every 1s cycle. The flowchart of the program is as follows:
Program source code I upload to the website, you can download the waitpid.c file for free, click here to download
After downloading the file, use the command: GCC waitpid.c-o waitpid
Then execute the command:./waitpid results such as;
The execution flow of the program can be seen from the output results. The parent process is executed first, the parent process sleeps 1s, the child process is executed, and the child process sleeps for 5 seconds, then the parent process listens. Oh I go I do not analyze the process, afraid to say again confused.
Experiment 2
This experiment uses the function wait (), as in Experiment 2, is the first to use fork () to create a new child process, and then let the child process pause 5s. Next, use the Wait () function for the original parent process. The difference is that the wait () function causes the parent process to block, as can be seen through the results of this experiment. The code is as follows:
Execution results such as
I suggest you try it yourself, and you can clearly see the difference. wait.c file point this download
/* wait.c file */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
pid_t PC,PR;
Pc=fork ();/* Create a new child process */
if (pc<0)
{
printf ("Error fork\n");
}
else if (pc==0)/* Child process */
{
/* Child process paused 5s*/
printf ("I am the Child progress.") I am Going sleep!\n ");
Sleep (5);
/* Child process quits normally */
printf ("I am the Child progress.") I am Going exit!\n ");
Exit (0);
}
else/* Parent Process */
{
Pr=wait (NULL);
if (pr<0)
{
printf ("Some error occured.\n");
}
printf ("I am the Father progress. I Get child exit code:%d\n ", PR);
}
}
/* WAITPID.C */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main ()
{
pid_t PC,PR;
Pc=fork ();/* Create a new child process */
if (pc<0)/* ERROR handling */
{
printf ("Error fork\n");
}
else if (pc==0)/* Child process */
{
/* Child process paused 5s*/
printf ("I am the Child progress.") I am Going sleep!\n ");
Sleep (5);
printf ("I am the Child progress.") I am Going exit!\n ");
/* Child process quits normally */
Exit (0);
}
else/* Parent Process */
{
/* Loop to test if the child process exits */
Do
{
/* Call Waitpid (), and the parent process does not block */
Pr=waitpid (Pc,null,wnohang);
/* Kawai process has not exited, the parent process is paused 1s*/
if (pr==0)/* Returns 0 stating that the child process did not exit */
{
printf ("I am the Father progress. The child process had not exited\n ");
Sleep (1);
}
} while (pr==0);
/* If the child process is found to exit, print out the appropriate case */
if (PR==PC)
{
printf ("I am the Father progress. I Get child exit code:%d\n ", PR);
}
Else
{
printf ("Some error occured.\n");
}
}
}