One, wait () function
When you call the Wait () function in a process
(1) If all of its subroutines are still being executed, the blockage
(2) Assume that a child process has been terminated. Waits for the parent process to get its terminating state.
(3) If there are no child processes, an error is returned.
In the following instance. Call Wait () in the parent process, assuming that the child process has not completed yet, and then calls itself into the blocked state.
Wait for the child process to finish executing, after the child process's resources are reclaimed and then executed by itself.
#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 ()