Lab Assignment, Exams, Universities, Lab Work, Programming Questions
Lab Assignment, Exams, Universities, Lab Work, Programming Questions
Problem Statement:
Experiment with programs, processes, memory allocation and manipulation for processes in UNIX.
Assignment Objectives:
Students will be able to differentiate programs and processes, also able to learn how to create processes and
able to explore the implication of process inheritance.
2. Construct the process tree diagram and also find the number of processes along with output of the
following code snippet.
int main(void){ Process tree, # of processes and output
printf("hello\n");
fork(); there are a hello
printf("hello\n"); total of 1 + 2 + hello
fork(); 4 + 8 = 15 hello
processes. hello
printf("hello\n");
hello
fork(); hello
printf("hello\n"); hello
return 0; hello
}
/* Any formula for
nuber of outputs?
If so, state.*/
3. Run the following code on your machine and write the output. Suggest a way to avoid the mismatch
of machine output w.r.t. dry run output.
#include<stdio.h> Output, Reason and Suggestion
#include<unistd.h> When fork() is called,
int main(void) A the process splits into
To avoid mismatches between
P two: the parent machine output and dry run output,
{ A process and the child you can introduce newline
printf("A"); P process. Both characters (\n) in your printf
processes continue statements. This helps in flushing
fork(); executing the code
from the point of the the output buffer, making it more
printf("P\n"); fork() call. predictable.
return 0;
}
4. Draw the process tree and write the output of the following code snippet.
Process tree and output
Able to
Able to
int main() Able to
Able to
{ Able to
fork() && fork(); Able to
Able to
printf("Able to\n"); Able to
return 0;
}
5. Draw the process tree and write the output of the following code snippet.
Process tree and output
Got!!!
Got!!!
int main() Got!!!
{ Got!!!
Got!!!
fork(); Got!!!
fork() && fork(); Got!!!
fork(); Got!!!
Got!!!
printf("Got!!!\n");
return 0;
}
6. Draw the process tree and write the output of the following code snippet.
Process tree and output
doing!
doing!
doing!
doing!
doing!
doing!
doing!
int main() doing!
{ doing!
fork();
fork() + fork();
fork();
printf("doing!\n");
return 0;
}
7. Draw the process tree and write the output of the following code snippet.
int main(){
fork(); Process tree Output
fork() || fork();
fork(); Really!!!
printf("Really!!!\n"); Really!!!
Really!!!
return 0; Really!!!
} Really!!!
Really!!!
Really!!!
Remark Really!!!
There are a total of 8 lines in the
output, corresponding to the 8
processes in the process tree. The
logical OR (||) ensures that the
second fork() is executed only in the
child process created by the first
fork(), leading to a total of 8
processes.
8. Draw the process tree and write the output of the following code snippet.
int main(){ Process tree Output
fork();
fork()&& fork()||fork(); guess
fork(); guess
printf("guess\n"); guess
guess
return 0; guess
} guess
guess
guess
Remark
There are a total of 8 lines in the output,
corresponding to the 8 processes in the
process tree. The logical AND (&&) and
logical OR (||) operators determine the
flow of execution, but the printf statement
is executed by all processes.
9. Draw the process tree and write the output of the following code snippet.
int main(){ Process tree Output
fork()&&fork();
fork()||fork(); Hi
Hi
printf("Hi\n"); Hi
return 0; Hi
} Hi
Hi
Hi
Hi
Remark
There are a total of 8 lines in the output,
corresponding to the 8 processes in the
process tree. The logical AND (&&) and
logical OR (||) operators determine the
flow of execution, but the printf statement
is executed by all processes.
10. Construct the process tree diagram and also find the number of processes along with output of the
following code snippet.
int main(){ Process tree, # of processes and output
int pid,pid2;
pid=fork(); So, there are I
C
if(pid){ a total of 1 + I
pid2=fork(); 2+2=5 I
printf("I\n"); processes. I
}
else{
printf("C\n");
pid2=fork();
}
return 0;
}
11. Construct the process tree diagram and also find the number of processes along with output of the
following code snippet.
int
main(void){
Process tree, # of processes and output
pid_t childpid; So, there are i:1
int i, n=3; a total of 1 + i:2
for(i=1;i<n;i++){ 2+4+8= i:3
childpid=fork(); 15
if(childpid==-1) processes.
break;
}
printf("i:%d\n",i);
return 0;
}
12. Draw the process tree because of the following code snippet and state number of times x=0 as well
as x6=0will be displayed.
pid_t Process tree # of x=0 # of x6=0
add(pid_t a, pid_t b){
return a+b; Number of Number of
} times x=0 : 2 times x≠0 : 1
int main(void){
(in the child (in the parent
processes). process).
pid_t x=10;
printf("%d\n",x);
x=add(fork(),fork());
printf("%d\n",x);
return 0;}
13. Determine the total number of displayed for the given code snippet.
int main(void){
int x[]={10,20,fork(),fork()+fork()}; # of display # of Processes
int len=sizeof(x)/sizeof(int); Number of times The fourth element in the
for(int i=0;i<len;i++) displayed: 7 times (in array involves two fork()
each of the 7 processes). calls, creating three
fprintf(stderr," %d ",x[i]); additional processes. So,
printf("\n"); there are a total of 4 + 3 =
7 processes.
return 0;
}
14. Determine the number of process(s) will be created when the below program becomes process and
also write the output.
void show(){
if(fork()==0)
# of processes Output
printf("1\n"); There are a total of 7 1
if(fork()==0) processes created. 2
printf("2\n"); 3
if(fork()==0)
printf("3\n");
}
int main(void){
show();
return 0;
}
15. Draw the process tree of the following code snippet. Also give a count of processes and the output of
the following code. Can the code segment generate fan of processes.
int main(void){ Process tree, # of processes and output
if(fork()==0)
printf("1\n"); Using fork() in if
statements 1
else if(fork()==0) yields 16 2
printf("2\n"); processes. The 3
else if(fork()==0) printf outputs 1- 4
5 based on
printf("3\n"); conditions, with 5
else if(fork()==0) the final else 5
printf("4\n");
statement 5
exclusive to the 5
else parent process.
printf("5\n");
return 0;
}
16. Find the output of the code segment showing the corresponding process tree.
int main(){
Process tree Output
pid_t p1,p2; done
p2=0;
p1=fork();
if (p1 == 0)
p2 = fork();
if (p2 > 0)
fork();
printf("done\n");
return 0;
}
17. Find the number of direct children to the main process, the total number of processes and the output.
int main(){pid_t c1=1,c2=1;
c1=fork(); # of direct children to main process Total processes Output
if(c1!=0) Number of direct children Total number of 1
c2=fork(); to the main process: 1 processes: 3
if(c2==0){
fork();printf("1\n");
}
return 0;}
19. Find the output of the code segment showing the corresponding process tree.
int main(){
if(fork()){
if(!fork()){
Process tree Output
fork();
printf("S "); DTAS
}
else{
printf("T ");
}
}
else{
printf("D ");
}
printf("A ");
return 0;
}
20. Calculate the number of processes the following code snippet will generate.
int main(){int i;
Process tree Output
for(i=0;i<12;i++){ There is no specific output
if(i%3==0){ statement in the code, but we can
note that each process continues
fork(); the execution from the point where
} the fork occurred. If you had an
output statement, it would be
} printed by each process.
return 0;
}
21. State the possible values of x for the given code snippet;
Process tree Output
25
int x;
int a[2]={10,20};
x=5+a[fork() || fork()];
printf("%d ",x);
22. Suppose four user-defined exit handlers X, Y, P, and Q are installed in the order X then Y then P then
Q using atexit() function in a C program. Exit hadler X is designed to display 1, Y is designed to
display 2, P is designed to display 3, and Q to display 4. State the order of their display, when the
program is going to terminate after calling return 0/exit(0).
Choice
(A) 4, 3, 2 1 (C) 1, 2, 4, 3 (C) 1, 2, 4, 3
23. You know that the ps utility in UNIX reports a snapshot of the current processes. Determine the state
code of the given program, that became a process.
int main(void){
fprintf(stderr,"PID=%ld\n",(long)getpid());
while(1);
return 0;
}
Choice
(A) R (C) T (A) R
(B) S (D) Z
24. Find the process state code of the given program, that became a process using the Unix utility ps. As
you know ps displays information about a selection of the active processes.
int main(void){
fprintf(stderr,"PID=%ld\n",(long)getpid());
while(1)
sleep(1);
return 0;
}
Choice
(A) R (C) T (B) S
(B) S (D) Z
25. Develop a C code to create the following process tree. Display the process ID, parent ID and return
value of fork() for each process.
OBSERVARIONS:
• Use ps utility to verify the is-a-parent relationship?
• Are ypu gettig any orphan process case?
• Are you getting any ZOMBIE case?
Code here
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid1, pid2, pid3, pid4, pid5, pid6;
printf("Initial process:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), 0);
pid1 = fork();
if (pid1 < 0) {
perror("fork");
exit(1);
} else if (pid1 == 0) {
printf("Process 1:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid1);
pid4 = fork();
if (pid4 < 0) {
perror("fork");
exit(1);
} else if (pid4 == 0) {
printf("Process 4:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid4);
exit(0);
}
pid5 = fork();
if (pid5 < 0) {
perror("fork");
exit(1);
} else if (pid5 == 0) {
printf("Process 5:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid5);
exit(0);
}
waitpid(pid4, NULL, 0);
waitpid(pid5, NULL, 0);
exit(0);
}
pid2 = fork();
if (pid2 < 0) {
perror("fork");
exit(1);
} else if (pid2 == 0) {
printf("Process 2:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid2);
exit(0);
}
pid3 = fork();
if (pid3 < 0) {
perror("fork");
exit(1);
} else if (pid3 == 0) {
printf("Process 3:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid3);
pid6 = fork();
if (pid6 < 0) {
perror("fork");
exit(1);
} else if (pid6 == 0) {
printf("Process 6:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid6);
exit(0);
}
waitpid(pid6, NULL, 0);
exit(0);
}
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
waitpid(pid3, NULL, 0);
printf("All child processes have terminated.\n");
return 0;
}
26. Create two different user-defined functions to generate the following process hierarchy shown in
Figure-(a) and Figure-(b). Finally all the processes display their process ID and parent ID.
Figure-(a) Figure-(b)
Code here
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void figureA() {
pid_t child1, child2, child3;
if((child1 = fork()) == 0) {
printf("Process ID: %d, Parent ID: %d\n", getpid(), getppid());
exit(0);
}
if((child2 = fork()) == 0) {
printf("Process ID: %d, Parent ID: %d\n", getpid(), getppid());
exit(0);
}
if((child3 = fork()) == 0) {
printf("Process ID: %d, Parent ID: %d\n", getpid(), getppid());
exit(0);
}
Code here
void figureB() {
pid_t child1;
if((child1 = fork()) == 0) {
pid_t grandChild;
if((child1 = fork()) == 0) {
printf("Process ID:% d, ParentID:% d \n ",getpid (),getppid ());
exit ( 0 );
}
int main() {
printf("\nFigure-(a)\n");
figureA();
printf("\nFigure-(b)\n");
figureB();
return 0;
}
29. Write a MulThree.c program to multiply three numbers and display the output. Now write another
C program PracticeExecl.c, which will fork a child process and the child process will execute
the file MulThree.c and generate the output. The parent process will wait till the termination of
the child and the parent process will print the process ID and exit status of the child.
Code here
// MulThree.c // PracticeExecl.c
#include <stdio.h> #include <stdio.h>
int main() { #include <stdlib.h>
int num1 = 2, num2 = 3, num3 = 4; #include <sys/types.h>
int result = num1 * num2 * num3; #include <sys/wait.h>
printf("Result: %d\n", result); #include <unistd.h>
return 0;
} int main() {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
After implementing the code, we'll get execl("./MulThree", "MulThree",
the following result : NULL);
perror("execl");
Result: 24 exit(EXIT_FAILURE);
Child process (PID 12345) terminated } else { // Parent process
with exit status: 0 int status;
waitpid(pid, &status, 0);
The MulThree.c program calculates the if (WIFEXITED(status)) {
product of 2, 3, and 4, which is 24. printf("Child process (PID %d)
terminated with exit status: %d\n", pid,
The PracticeExecl.c program forks a WEXITSTATUS(status));
child process, and the child process } else {
executes MulThree.c.
printf("Child process (PID %d)
The parent process waits for the child terminated abnormally\n", pid);
to terminate and prints the process ID }
and exit status of the child. }
The exit status of 0 indicates successful return 0;
execution. }
Note: The actual process ID (PID) will
be different in your case.
30. You know the usages of the command grep. Implement the working of grep -n pattern
filename in a child process forked from the parent process using execl system call. The par-
ent process will wait till the termination of the child and the parent process will print the process ID
and exit status of the child.
Code here
// grep_example.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <pattern> <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
execl("/bin/grep", "grep", "-n", argv[1], argv[2], NULL);
perror("execl");
exit(EXIT_FAILURE);
} else { // Parent process
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
printf("Child process (PID %d) terminated with exit status: %d\n", pid, WEXITSTATUS
(status));
} else {
printf("Child process (PID %d) terminated abnormally\n", pid);
}
}
return 0;
}
31. Implement the above question using execv, execlp, execvp, execle, execve system calls.