Creating asymmetric tree of process using fork ()

Faun

Wahahaha~!
Staff member
I am trying to write a pseudocode for this process tree using fork(). A little bit of hint will be very helpful.

Here is my approach, pretty sure that it's not correct. Please see attachment.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(void)
{
pid_t child_pid[19], wpid;
int status = 0;
int i;
//int a[3] = {1, 2, 1};

printf("parent_pid = %d\n", getpid());
printf("i = %d\n", i);

for(i=1;i<=6;i++)
{

if(i==1){
if ((child_pid[1] = fork()) == 0)
{
printf("In child process (pid = %d)\n", getpid());
//printf("i = %d\n", i);
if ((child_pid[7] = fork()) == 0)
{
printf("In child process (pid = %d)\n", getpid());

if ((child_pid[8] = fork()) == 0)
{
printf("In child process (pid = %d)\n", getpid());

if ((child_pid[9] = fork()) == 0)
{
printf("In child process (pid = %d)\n", getpid());
}

}


}


}


}






while ((wpid = wait(&status)) > 0)
{
printf("Exit status of %d was %d (%s)\n", (int)wpid, status,
(status > 0) ? "still running" : "Ended");
}
return 0;
}
 
OP
Faun

Faun

Wahahaha~!
Staff member
Got to workout the serial code. Will have to use threads to parallelize the branch creation.

Daymn ~
 

doomgiver

Warframe
printf("i = %d\n", i);

just before the main loop. this will produce garbage (not our esteemed member)


and you are missing a bracket (the for loop, checked in notepad++)


create a loop of 4 iterations,
make a parent process and have them make threads, as you said.

each iteration will be for a branch and thread for child process


or make 4 threads and then loop for the cheld processes.


ps. your code structure was unreadable to me until i formatted it.
 
OP
Faun

Faun

Wahahaha~!
Staff member
Finally it's done (fork + threads) :grin:

Code:
#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);



		}*/

	}



	



}

OUTPUT:
parent_pid = 7864
Main parent process 0 (pid= 7865) intitiated with Child id 0


Inside parent process 1 with parent id 7869
In child process 1, (pid = 7869) and parent is (pid= 7865)
Inside parent process 1 with parent id 7869
In child process 2, (pid = 7870) and parent is (pid= 7865)

Inside parent process 2 with parent id 7870


In child process 7, (pid = 7871) and parent is (pid= 7869)
Inside parent process 2 with parent id 7870
In child process 3, (pid = 7872) and parent is (pid= 7865)

In child process 10, (pid = 7873) and parent is (pid= 7870)



In child process 8, (pid = 7874) and parent is (pid= 7871)

In child process 11, (pid = 7875) and parent is (pid= 7872)
Inside parent process 10 with parent id 7873
Exiting process 10




In child process 12, (pid = 7877) and parent is (pid= 7875)
In child process, 9 (pid = 7876) and parent is (pid= 7874)
Inside parent process 3 with parent id 7872
Exiting process 2
Inside parent process 12 with parent id 7877
Exiting process 12
Inside parent process 9 with parent id 7876
Exiting process 9
Inside parent process 11 with parent id 7875
Exiting process 11
Inside parent process 8 with parent id 7874
Exiting process 8

Exiting process 3
Inside parent process 7 with parent id 7871
Exiting process 7

Exiting process 1

Inside parent process 0 with parent id 7865
Exiting process 0
 
Last edited:
Top Bottom