A different way of saying it: you can control the execution of child processes by call
wait() in the parent.
wait() forces the parent to suspend execution until the child is finished.
wait() returns the process number of the child that finished. If the child finishes before the parent gets around to calling
wait(), then when
wait() is called by the parent, it will return immediately with the child process number.
Here's a little example that guarantees that the output of the child will precede that of the parent:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>
void main (void)
{
if (fork () == 0)
(void) fprintf (stdout, "this is the child\n");
else {
(void) wait ((int *) NULL);
(void) fprintf (stdout, "this is the parent\n");
}
exit (EXIT_SUCCESS);
}
Run it:
Code:
prog
this is the child
this is the parent
You can have more than one child process by simply calling
fork() more than once.
Hope this helps some.