I am trying to create a c program where I fork a total of 4 children..
... When the first child is created, the parent waits until it finishes
... The last three children are forked (at the same time) and can finish at their own pace
... My code
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#define MSGSIZE 16
int main(int argc, char *argv[])
{
int status;
if( fork() != 0) // create a child
{
// we are in parent
// Forking child 1
wait(&status);
if( fork() != 0) // create another child
{
// we are in parent --> child 2
if( fork() != 0) // create another child
{
// we are in parent
if( fork() != 0) // create another child
{
// we are in parent
}
else
{
// we are in the 4nd child
}
}
else
{
// we are in the 3nd child
}
}
else
{
// we are in the 2nd child
// first child
for (int a = 0; a < 50; a++)
{
printf("a) %d\n", a + a / 2);
}
}
}
else
{
// first child
for (int a = 0; a < 12; a++)
{
printf("a) %d\n", a + a);
}
}
exit(0);
}//main
... When I run it my program ends, and reruns on its own.
EXAMPLE
a) 15
a) 16
a) 18
mrobinson@TOSHIBA312:$ a) 19
a) 21
a) 22
a) 24
a) 25
a) 27
Any help will be greatly appreciated!