Tree of Processes



Write a program to create a process tree that is L levels high, print information about processes as they are created and destroyed. Here is the detailed information:

  1. Your program should read two arguments from command line - the number of levels of the tree (L) and number of children of each internal node of the process tree (N). For example, the following command
    		$ ./process_tree 4 3
    should invoke your program "process_tree" with L = 4 and N = 3.
  2. Then your program should print the following information:
    		(pid): Process starting
    		(pid): Parent's id = (ppid)
    		(pid): Level in the tree = (value_of_L_from_command_line)
    		(pid): Creating (value_of_N_from_command_line) children at Level (L-1)
    In the above output, you should replace pid by the process id of the current process and ppid by the process id of the parent process.
  3. Next, if the Level L is greater than 1, your program should create N child processes using fork(), and wait for all of the children to complete using the wait() system call.
  4. Once all the child processes (if any) have terminated, your program should quit by printing.
    		(pid): Terminating at Level (L).
    Parent must not quit before ALL child processes terminate.
    Also, parent must call wait() ONLY AFTER CREATING ALL CHILDREN. (Think why?)
  5. What should each child process do while the parent waits? Recursion! Each of the child processes should use the exec() system call to run exactly the same program image as the parent. The only difference should be that the command-line argument received by the child proceses from the parent (via the exec() system call) should be L-1 for height and N for number of children.
    (NOTE: Recursion in this step can also be done without using exec(), via straightforward function calls. But you are REQUIRED to use exec() to start the child program image and pass arguments to it. Learning about exec() is one of the goals of this assignment.)

Grading Guidelines

	This is how we will grade your assignment during the demo. So please prioritize your work accordingly.

	20 - Correctly creating a tree of processes with arbitrary L and N using fork() and exec().

	20 - Correctly terminating the tree for arbitrary L and N using wait()/waitpid().

	20 - Recursion is implemented using exec() (and NOT using usual function calls)

	10 - Parent process wait()s for child processes ONLY after it creates ALL child processes.

	10 - Parent process does not terminate before ALL child processes terminate.

	10 - Error Handling
		Most important part here is to make sure that you check and handle the errors 
		returned by ALL systems calls used in your program. Also check for other common 
		error conditions in your program. But don't go overboard with error checking. 
		We will NOT try to intentionally break your code with bad input that's irrelevant 
		to the assignment's goal.


	10 - Makefile, Compilation without errors, README 

	Total points = 100


  1. The entire program is likely very small, no more than a few tens of lines of code at most, if you think through it carefully.
  2. Use manpages to check the usage details of different system calls.
  3. Do this assignment as a regular user, NOT as root, to avoid corrupting your system accidentally.
  4. Use the command "kill -9 (pid)" to kill one process. Use "killall (program_name)" to kill all processes starting with a certain name. Use "kill -9 -1" to kill all processes owned by the current user (do this ONLY as a normal user, NEVER as root).
  5. Remember to terminate *all* the processes you create. If implemented incorrectly, you can easily end up writing a program that forks indefinitely. The system administrators won't like it, especially on remote.cs, and they may disable your account. Use machines in G-7/Q-22 labs, or your own personal computer.