#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
pid_t child_pid[19], wpid;
int status = 0;
int i=0;
void *tree1(void *il);
void *tree2(void *il);
void *tree3(void *il);
int main(void)
{
printf("parent_pid = %d\n", getpid());
pthread_t pt1, pt2, pt3, pt4, pt5, pt6;
int iret1, iret2, iret3, ret4, iret5, iret6;
child_pid[0] = fork();
if (child_pid[0]==0)
{
printf("Main parent process 0 (pid= %d) intitiated with Child id %d \n", getpid(), child_pid[0]);
iret1 = pthread_create(&pt1, NULL, tree1, (void *) i);
iret2 = pthread_create(&pt2, NULL, tree2, (void *) i);
iret3 = pthread_create(&pt3, NULL, tree3, (void *) i);
pthread_join(pt3, NULL);
pthread_join(pt2, NULL);
pthread_join(pt1, NULL);
}else if (child_pid[0] > 0)
{
sleep(1);
printf("\nInside parent process 0 with parent id %d", child_pid[0]);
waitpid(child_pid[0], &status,0);
printf("\nExiting process 0\n");
}
return 0;
}
void *tree1(void *il)
{
int status1=0, status7=0, status8=0, status9=0;
pid_t wpid1=0;
child_pid[1] = fork();
if (child_pid[1] == 0)
{
printf("\nIn child process 1, (pid = %d) and parent is (pid= %d)\n", getpid(),getppid());
child_pid[7] = fork();
if (child_pid[7] == 0)
{
printf("\nIn child process 7, (pid = %d) and parent is (pid= %d)\n", getpid(),getppid());
child_pid[8] = fork();
if (child_pid[8] == 0)
{
printf("\nIn child process 8, (pid = %d) and parent is (pid= %d)\n", getpid(),getppid());
child_pid[9] = fork();
if (child_pid[9] == 0)
{
printf("\nIn child process, 9 (pid = %d) and parent is (pid= %d)\n", getpid(),getppid());
}else if (child_pid[9] > 0)
{
printf("\nInside parent process 9 with parent id %d", child_pid[9]);
waitpid(child_pid[9], &status9,0);
printf("\nExiting process 9\n");
}
}else if (child_pid[8] > 0)
{
printf("\nInside parent process 8 with parent id %d", child_pid[8]);
waitpid(child_pid[8], &status8,0);
printf("\nExiting process 8\n");
}
}else if (child_pid[7] > 0)
{
printf("\nInside parent process 7 with parent id %d", child_pid[7]);
waitpid(child_pid[7], &status7,0);
printf("\nExiting process 7\n");
}
}
else if (child_pid[1] > 0)
{
printf("\nInside parent process 1 with parent id %d", child_pid[1]);
waitpid(child_pid[1], &status1,0);
printf("\nExiting process 1\n");
}
}
void *tree2(void *il)
{
int status2=0, status10=0;
child_pid[2] = fork();
if (child_pid[2] == 0)
{
printf("\nIn child process 2, (pid = %d) and parent is (pid= %d)\n", getpid(),getppid());
child_pid[10] = fork();
if (child_pid[10] == 0)
{
printf("\nIn child process 10, (pid = %d) and parent is (pid= %d)\n", getpid(),getppid());
}else if (child_pid[10] > 0)
{
printf("\nInside parent process 10 with parent id %d", child_pid[10]);
waitpid(child_pid[10], &status10,0);
printf("\nExiting process 10\n");
}
}else if (child_pid[2] > 0)
{
printf("\nInside parent process 2 with parent id %d", child_pid[2]);
waitpid(child_pid[2], &status2,0);
printf("\nExiting process 2\n");
/*while ((wpid = wait(&status2)) > 0)
{
printf("\nExit status of 2 (pid = %d) was %d (%s)\n", (int)wpid, status2,
(status2 > 0) ? "still running" : "Ended");exit(0);
}*/
}
}
void *tree3(void *il)
{
int status3=0, status11=0, status12=0;
child_pid[3] = fork();
if (child_pid[3] == 0)
{
printf("\nIn child process 3, (pid = %d) and parent is (pid= %d)\n", getpid(),getppid());
child_pid[11] = fork();
if (child_pid[11] == 0)
{
printf("\nIn child process 11, (pid = %d) and parent is (pid= %d)\n", getpid(),getppid());
child_pid[12] = fork();
if (child_pid[12] == 0)
{
printf("\nIn child process 12, (pid = %d) and parent is (pid= %d)\n", getpid(),getppid());
}else if (child_pid[12] > 0)
{
printf("\nInside parent process 12 with parent id %d", child_pid[12]);
waitpid(child_pid[12], &status12,0);
printf("\nExiting process 12\n");
}
}else if (child_pid[11] > 0)
{
printf("\nInside parent process 11 with parent id %d", child_pid[11]);
waitpid(child_pid[11], &status11,0);
printf("\nExiting process 11\n");
}
}else if (child_pid[3] > 0)
{
printf("\nInside parent process 3 with parent id %d", child_pid[3]);
waitpid(child_pid[3], &status3,0);
printf("\nExiting process 3\n");
/*while ((wpid = wait(&status2)) > 0)
{
printf("\nExit status of 2 (pid = %d) was %d (%s)\n", (int)wpid, status2,
(status2 > 0) ? "still running" : "Ended");exit(0);
}*/
}
}