Problem Set 1
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:
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
Console Output
Q7) Write the possible output of the sample code given below.
Here are some assumptions:
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