One, wait () function
When you call the Wait () function in a process
(1) If all of its subroutines are still running, block
(2) If a child process is terminated, wait for the parent process to get its terminating state.
(3) If there are no child processes, an error is returned.
In the following instance, wait () is called in the parent process, and if the child process has not finished running, it calls itself into a blocking state.
After the child process has finished running, the child process's resources are recycled and run again.
#include <stdio.h> #include <unistd.h> #include <wait.h> #include <stdlib.h>int main () { int i=0; int j=0; int status; int count =0; int a = fork (); if (a>0) { printf ("This is parent, PID =%d\n", Getpid ()); for (i= 0;i<=10;i++) { printf ("Parent is%d\n", i); Sleep (1); Wait (&status); } Wait (&status); } else { for (j=0;j<=10;j++) { printf ("%d\n", j); Sleep (1); } } return 0;}
UNIX environment Advanced Programming----Process Control wait ()