#include<stdio.h>
#include<stdlib.h>
void child1(void)
{
int t;
printf("in child1\n");
srand(getpid());
t=rand()%10+1;
printf("child1 delay for %d seconds\n",t);
sleep(t);
printf("child1 exiting....\n");
exit(0);
}
void child2(void)
{
int t;
printf("in child2\n");
srand(getpid());
t=rand()%10+1;
printf("child2 delay for %d seconds\n",t);
sleep(t);
printf("child2 exiting....\n");
exit(0);
}
void parent(void)
{
int status;
printf("parent waiting...\n");
while(1){
wait(&status);
status>>=8;
if(status==2)
break;
}
printf("child exiting...\n");
printf("parent exiting....\n");
exit(0);
}
main()
{
if(fork())
{
if(fork())
parent();
else
child2();
}
else
child1();
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in child1
parent waiting...
child1 delay for 2 seconds
in child2
child2 delay for 3 seconds
child1 exiting....
child2 exiting....