Question: Challenge Task: Process Tree Creator Example Flow User Input: If the user inputs n = 2 , the expected process tree structure would be: Descriptive

Challenge Task: Process Tree Creator Example Flow
User Input:
If the user inputs n=2, the expected process tree structure would be:
Descriptive Steps to Implement the Solution
Prompt for Input:
Start by prompting the user to enter an integer n for the depth of the process tree.
Create a Recursive Function:
Define a recursive function, create_process_tree (int depth), that will
handle the creation of processes.
If depth is equal to n, this indicates a leaf process, where you will print the PID
and PPID and execute the command using execl ().
Forking Processes:
For non-leaf processes (where depthn=2, the output should look similar to:
Enter the depth of the process tree: 2
Process PID: 1234, Parent PID: 5678
Process PID: 1235, Parent PID: 1234
Process PID: 1236, Parent PID: 1234
Note: The actual PIDs will vary with each execution.
Task Overview
In this challenge, you will create a C program that constructs a binary tree of processes based on
a user-specified depth. Each process will print its own Process ID (PID) along with its Parent
Process ID (PPID). The leaf processes will execute a simple command, such as is or date.
Objectives
Demonstrate your understanding of process creation and management using system calls
in C.
Utilize fork(), getpid(), and getppid() for process handling.
Implement execl () for executing commands in leaf processes.
Ensure proper synchronization and error handling.
Requirements
Input:
The program should prompt the user to input an integer n, representing the depth
of the binary tree (where depth =0 is the root process).
Process Creation:
Use fork () to create child processes.
Each non-leaf process should create two child processes.
Process Information:
Each process must print:
Process PID: [PID], Parent PID: [PPID]
Use getpid() to obtain the current process's PID and getppid() to obtain its
parent's PID.
Leaf Processes:
Leaf processes (those at depth n) should execute either:
Is (to list directory contents) or
date (to display the current date and time).
Use execl () for executing these commands.
Synchronization:
Ensure that all non-leaf processes wait for their child processes to complete
using wait ().
Error Handling:
Implement error handling for all system calls, including fork(), execl (),
and wait ().
Challenge Task: Process Tree Creator Example Flow

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!