# Creating asymmetric tree of process using fork ()



## Faun (Oct 19, 2011)

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>
> ...


----------



## Faun (Oct 20, 2011)

Got to workout the serial code. Will have to use threads to parallelize the branch creation.

Daymn ~


----------



## doomgiver (Oct 21, 2011)

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.


----------



## Faun (Oct 21, 2011)

Finally it's done (fork + threads) 


```
#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
> 
> 
> ...


----------



## doomgiver (Oct 21, 2011)

mind if i modify it and use it for college assignment?


----------



## Garbage (Oct 21, 2011)

doomgiver said:


> printf("i = %d\n", i);
> just before the main loop. this will produce *garbage* (not our esteemed member)


----------



## Faun (Oct 21, 2011)

doomgiver said:


> mind if i modify it and use it for college assignment?



Sure. 

Error handling has to be done. Further optimization regarding redundant variables can be done. Feel free to contribute.


----------



## doomgiver (Oct 21, 2011)

thanks.


----------

