You are on page 1of 5

1

Rajshahi University of Engineering & Technology


Department of Computer Science of Engineering

EXPERIMENT NO: 01
NAME OF EXPERIMENT: Creating a process tree using fork() command and Demonstrate the use
of execlp() and execvp() function by replacing a child process’s functionality with a new function.

SUBMITTED TO:

Suhrid Shakhar Ghosh


Assistant Professor
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
RAJSHAHI UNIVERSITY OF ENGINEERING & TECHNOLOGY

SUBMITTED BY:

Md Tanbeer Jubaer
1903071
Department Of Computer Science and Engineering
Rajshahi University Of Engineering and Technology

Monday, 4 December, 2023


2

Problem 1: Creating a process tree using fork() command as figure Problem_1.

Fig: Problem_1
Solution Code: lab.c
#include<stdio.h>
#include<unistd.h>

int main()
{
int pid = fork();
if(pid>0){
printf("Parent Process P1 \n");
printf("PID = %d, PPID = %d \n", getpid(), getppid());

int pid3 = fork();


if(pid3==0)
{
printf("Child Process P3\n");
printf("PID = %d, PPID = %d \n", getpid(), getppid());

int pid4 = fork();


if(pid4==0)
{
printf("Child Process P4\n");
printf("PID = %d, PPID = %d \n", getpid(), getppid());
int pid5 = fork();
if(pid5==0)
{
printf("Child Process P5\n");
printf("PID = %d, PPID = %d \n", getpid(), getppid());
}
}
}

getchar();
}
else if (pid == 0)
{
printf("Child Process P2 \n");
printf("PID = %d, PPID = %d \n", getpid(), getppid());
// getchar();
}
else{
printf("Fork Unsuccessful \n");
}
3

// printf("Hello World");
return 0;
}

Output:

Result Analysis:
In our problem, processes P1, P2, P3, P4, and P5 are created using mul ple fork() calls. P1 is the
parent of P2 and P3, P3 is the parent of P4, and P4 is the parent of P5. The code successfully
establishes these rela onships, as con rmed by the correct Parent Process IDs (PPIDs) in the
output. For instance, P1 (PID 4053) is the parent of P2 and P3, P3 (PID 4055) is the parent of P4,
and P4 (PID 4056) is the parent of P5. The problem is solved accurately based on the expected
process hierarchy.
An important part of the code is to put a getchar() func on in the rst if block this enables the
process not to die and it is alive as a parent of the child processes like P2, P3 etc. But if we do
not put this code here the parent id for process P3, P4 and P5 would be 1(kernel). Because In
Unix-like opera ng systems, when a process's parent process (PPID) terminates, the orphaned
process is adopted by the kernel.
ti
ti
fi
ti
fi
ti
4

Problem 2: Demonstrate the use of execlp() and execvp() func on by replacing a child process’s
func onality with a new func on.
Solution Code: lab2.c
1. #include<stdio.h>
2. #include<unistd.h>
3.
4. int main()
5. {
6. int pid = fork();
7. if(pid>0){
8. printf("Parent Process \n");
9. printf("PID = %d, PPID = %d \n", getpid(), getppid());
10.
11. // to keep the process alive
12. getchar();
13. }
14. else if (pid == 0)
15. {
16. printf("Child Process \n");
17. printf("PID = %d, PPID = %d \n", getpid(), getppid());
18. // getchar();
19.
20. // use of execvp() to overwrite a process.Here help is another
21. // c program that is already compiled in the same folder
22.
23. char* args[] = {"./help", NULL};
24. execvp("./help", args);
25.
26. // Process will be overriden
27.
28. // execlp("python3","python3", "--version", NULL);
29.
30. printf("This Portion will not be executed");
31. }
32. else{
33. printf("Fork Unsuccessful \n");
34. }
35.
36. // printf("Hello World");
37. return 0;
38. }
39.

Solution Code: help.c


#include<stdio.h>
#include<unistd.h>

int main(int argc, char* argv[])


{
printf("This is an External Program. \n");
printf("PID = %d, PPID = %d \n", getpid(), getppid());
return 0;
}
ti
ti
ti
5

Output for execvp:

Figure: output2

Output for execlp:

Figure: Output3

Result Analysis:
In this scenario for output2, two processes are observed, and the output of the child process
di ers from the standard output. Typically, when using the fork() func on, a direct copy of the
parent process is created as the child process. However, in this case, the func onality of the
child process has been modi ed by using the execvp() func on in C. This func on allows the
installa on of a new program in the child process. In the provided example, a c program named
help.c is already compiled with the code given above. We want to override the child process of
lab2.c with this program thats why for surety we printed the process and parent process id’s
here too. Figure 2 clari es that our objec ve is 100% successful.
In the scenario for output3, there will be some changes in code named lab2.c for crea ng this
scenario we need to comment out line 23 and 24 and also remove the comment from line 28.
Now the child process has been modi ed by using the execlp() func on in C. In this case, the
arguments passed include "python3" as the rst argument and "--version" as the second
argument. This con gura on aims to override the current process and display the Python
version installed on the system. We can even use another python program here but for
simplicity we didn’t provide another code because doing it would be very easy. Output3 serves
as evidence that this objec ve has been successfully achieved. To prevent the child from
becoming an orphan, a getchar() func on is used in the parent process just as we did in
problem1.
ff
ti
fi
fi
ti
ti
fi
fi
ti
ti
fi
ti
ti
ti
ti
ti
ti

You might also like