0% found this document useful (0 votes)
14 views4 pages

Problem Set 1

Uploaded by

jeralax396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Problem Set 1

Uploaded by

jeralax396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CEN301 – Operating System Problem Set 1

Q1) Draw the process hierarchy structurally of the sample code given below without writing the PIDs.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void create_child() {
pid_t pid = fork();
if (pid < 0) { int main() {
perror("fork error"); create_child();
exit(1);
} else if (pid == 0) { create_child2();
exit(0); wait(NULL);
}
} wait(NULL);
void create_child2() { return 0;
pid_t pid = fork();
if (pid < 0) { }
perror("fork error");
exit(1);
} else if (pid == 0) {
create_child();
exit(0);
}
}

Process Hierarchy

Q2) Draw the process hierarchy of the sample code given below. Also write the possible output.
Here are some assumptions:

 The pid of the root process is 10.


 The output of the execlp("ls", "ls", "-l", NULL) system call is: "52 5.11.2024 main.c".
 The output of the execlp("date", "date", NULL, NULL) system call is: "5.11.2024".
#include <stdio.h> int main() {
#include <stdlib.h> pid_t pid1, pid2;
#include <unistd.h> pid1 = fork();
if (pid1 < 0) {
#include <sys/types.h> perror("Fork error");
#include <sys/wait.h> exit(EXIT_FAILURE);
void execute_command(const char *command, const char *arg) { } else if (pid1 == 0) {
if (execlp(command, command, arg, NULL) == -1) { printf("First Child Process - PID: %d, Parent PID: %d\n", getpid(), getppid());
perror("Error executing command"); execute_command("ls", "-l");
exit(EXIT_FAILURE); }
execute_command("date", NULL);
} pid2 = fork();
} if (pid2 < 0) {
perror("Fork error");
exit(EXIT_FAILURE);
} else if (pid2 == 0) {
printf("Second Child Process - PID: %d, Parent PID: %d\n", getpid(), getppid());
}
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
printf("Main Process - PID: %d is exiting.\n", getpid());
return 0;
}
Console Output Process Hierarchy

Q3) Draw the process hierarchy of the sample code given below. Suppose all fork calls are successful.
Just draw the hierarchy structurally without writing the PIDs. Also write the possible output.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int i = 0, counter = 0;
for(; i< 3; i++)
{
fork();
counter++;
}
printf("Final value of the counter : %d\n",counter);
return 0;
}
Console Output Process Hierarchy

Q4) Draw the process hierarchy structurally of the sample code given below without writing the PIDs.
Suppose all fork calls are successful.
void fun() void fun2()
{ {
pid_t pid = fork(); fork();
if(pid == 0) }
{ int main() {
fun2(); fun();
} return 0;
else }
{
fork();
fork();
}
}
Process Hierarchy

Q5) Write a program to create the following process hierarchy. Suppose all fork calls are successful.
Do not check the return status of the fork calls.

Program Code

Q6) Write the output of the sample code given below.


int main() {
int a = 5, b = 4;
a++;
pid_t pid = fork();
if(pid == 0)
a+= 2;
b--;
printf("a: %d, b: %d, a + b =%d\n",a,b, a + b);
return 0;
}

Console Output
Q7) Write the possible output of the sample code given below.
Here are some assumptions:

 The pid of the root process is 10.


 All fork calls are successful.
int run(int pid)
{
printf("Pid :%d, getpid :%d\n",pid, getpid());
fork();
}
int main() {
pid_t pid = fork();
run(pid);
printf("%d exited...\n",getpid());
return 0;
}

Console Output

Q8) Write a program to create the following process hierarchy. Suppose all fork calls are successful.
Do not check the return status of the fork calls.

Program Code

You might also like