Process Tree

 

🧠 Experiment: Process Creation Using fork() and Viewing the Process Tree


🎯 Aim:

To create a new process using the fork() system call, print the parent and child process IDs, and use the pstree command to observe the process tree starting from the init (systemd) process.


🧩 Theory:

  • The fork() system call is used to create a new process.

  • The newly created process is called the child process, and the original is the parent process.

  • After a successful fork(), two processes run concurrently:

    • Both execute the same code following the fork()

    • But they receive different return values:

      • 0 in the child process

      • The child’s PID in the parent process

  • The init (or systemd) process is the ancestor of all processes in Linux.

The pstree command displays processes in a hierarchical tree format, showing parent–child relationships.


🧪 Step 1: Write the C Program

Create a file named fork_example.c:

#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; pid = fork(); // create a new process if (pid < 0) { perror("Fork failed"); return 1; } else if (pid == 0) { // Child process printf("Child Process:\n"); printf(" Child PID: %d\n", getpid()); printf(" Parent PID: %d\n", getppid()); } else { // Parent process printf("Parent Process:\n"); printf(" Parent PID: %d\n", getpid()); printf(" Child PID: %d\n", pid); sleep(5); // delay so pstree can show both processes } return 0; }

🧪 Step 2: Compile and Run

gcc fork_example.c -o fork_example ./fork_example

🧾 Sample Output:

Parent Process: Parent PID: 3412 Child PID: 3413 Child Process: Child PID: 3413 Parent PID: 3412

This shows that:

  • The parent has PID 3412

  • The child has PID 3413

  • The child’s parent PID matches the parent’s PID


🧪 Step 3: Observe the Process Tree

While the program is running (especially during the 5-second sleep), run in another terminal:

pstree -p 3412

or, to view the entire tree from the init process:

pstree -p 1

🧾 Sample Output (Simplified):

systemd(1)─┬─bash(3200)─┬─fork_example(3412)─┬─fork_example(3413) │ └─pstree(3420)

Explanation:

  • systemd(1) is the init process (PID 1)

  • bash(3200) is your shell (parent of your program)

  • fork_example(3412) is the parent process created by you

  • fork_example(3413) is the child process created by fork()


🧩 Step 4: Understanding Process Hierarchy

  • Every process (except init/systemd) is a child of another process.

  • The fork() system call duplicates the parent process.

  • Using pstree, you can visualize this relationship easily.


🧩 Step 5: Verify with ps command

You can also verify the parent-child relationship using ps:

ps -ef | grep fork_example

Sample Output:

user 3412 3200 0 11:20 pts/0 00:00:00 ./fork_example user 3413 3412 0 11:20 pts/0 00:00:00 ./fork_example

This shows:

  • The child (3413) has parent 3412

  • The parent (3412) has parent 3200 (bash)


🧩 Step 6: After Process Termination

Once both processes finish, re-running pstree will no longer show them — they were temporary.


🧾 Summary Table

ConceptCommand / Output
Create processfork() system call
Parent PIDgetpid()
Child PIDReturned by fork()
Parent of childgetppid()
Show process treepstree -p
Show parent-child relationship`ps -ef

Result:

  • A new process was successfully created using the fork() system call.

  • The parent and child PIDs were displayed.

  • The pstree command was used to visualize the process hierarchy, confirming that both parent and child originate from the init (systemd) process.


🧠 Extension Exercise (for students):

Modify the program to:

  1. Have the child execute another command using execlp() (e.g., ls).

  2. Observe the change in process tree.

  3. Notice that the child replaces its code with the new program.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command