CS303- Lab2 - Solutions
CS303- Lab2 - Solutions
1. Exercise 1
a) Run the following three programs and enumerate how many times “Hello Class” was displayed
for each program:
• The process child is duplicating the instructions of its father starting from the fork() instruction.
• The father has two instructions to execute: the fork + the printf
• The child process has one instruction to execute: the printf
➔ “Hello Class” will be displayed twice: once by the father and once by the child process.
• The process child is duplicating the instructions of its father starting from the first fork()
instruction.
• The father has three instructions to execute: the 1st fork + the 2nd fork + the printf
• The 1st child process of the father has two instructions to execute: the 2nd fork + the printf
• The 2nd child process of the father has an only one instruction to execute: the printf
• The 1st child process of the father is also executing a fork; which means, the 1 st child is having a
child itself. This new child is duplicating the executable instructions of its father (which is the 1 st
child) starting from the fork.
• The child of the 1st child has one instruction to execute: the printf.
➔ “Hello Class” will be displayed 4 times: once by the father, once the 1st child, once by the 2nd child,
and once by the new child of the 1st child.
In this program we want to make sure that ONLY the FATHER is allowed to create child processes. We
don’t want the children processes to execute the fork instruction.
Therefore, we control who will be executing the fork instruction and who will not.
if (child_process_1 != 0){
child_process_2 = fork();
}
• If the return value of our 1st fork is zero, then this is currently the child process. The child
process is prohibited from entering and from executing the fork instruction. Thus the 1 st child
will jump to the following instruction: the printf.
• If the return value of our 1st fork is different from zero, then this is currently the father process.
The father will create the 2nd child process after executing the 2nd fork. The father, at the end,
will execute the last instruction: the printf.
• The 2nd child has an only one instruction to execute: the printf.
➔ “Hello Class” will be executed three times: once by the father process, once by the 1st child process,
and once by the 2nd child process.
b) Run the following two programs to display the pid of the child process and the pid of its father
process.
Program 1 Program 2
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h>
int main() { int main() {
pid_t child_process; pid_t child_process;
child_process = fork(); child_process = fork();
if (child_process == 0) if (child_process == 0)
printf("This is the child process with a pid %d printf("This is the child process with a pid %d
and my father process has the pid %d \n", getpid(), and my father process has the pid %d \n", getpid(),
getppid()); getppid());
else if (child_process > 0){ else if (child_process > 0){
printf("This is the father process with a pid %d wait(NULL);
\n", getpid()); printf("This is the father process with a pid %d
} \n", getpid());
else printf("An error has occurred while creating the }
child process. \n"); else printf("An error has occurred while creating the
return 0; child process. \n");
} return 0;
}
In this exercise, we want to make sure that the child process created, is displaying the parent process id
of its actual father.
While running program 1, we will realize that the father’s process ID is 2873, the child’s process ID is
2874. The child is claiming that his father’s process ID is 1995; the latter is different from the actual
process ID of the father.
➔This is explained by the fact that the father’s process is terminating before the child process executes
its instruction. The father process ID is, thus, deleted and the child process is, thus, an orphan.
➔In the OS, all child processes MUST have a parent ID. So, the operating system is randomly
allocating a parent ID to our orphan child process.
➔To make sure that our child process is not given a random parent process ID, we need to make sure
that the father does not terminate before the child. The father MUST wait the child to terminate first.
c) Run the following two programs and explain the order in which the iterations are displayed:
Program 1 Program 2
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h>
➔ The father process creates the child process, continues executing its following instructions, and
terminates.
➔Here we manage who will be waiting whom and we will do some ordering between the processes. The
father must wait for the child to finish before executing the rest of the iterations.
➔The father is waiting for the termination state of the child process to be NULL, to continue executing
the following instructions.
d) Write a C program that creates two children processes. The first child process displays the
list of integers from 1 to 50. The second child displays the list of integers from 51 to 100.