Operating system: Full name: Trịnh Mạnh Hùng Student id: 1952740
Operating system: Full name: Trịnh Mạnh Hùng Student id: 1952740
Lab 2
Full name : Trịnh Mạnh Hùng
Student id: 1952740
3. Exercise
3.1. Questions:
3.1.1. What the output will be at LINE A? Does this value not change for every process
execution? Explain your answer.
The output at LINE A is "PID of child process = 276", which is the value of child process's
id. We can get the value of current process by calling getpid(). This ID changes over every process
execution because though we keep launching our program from the same parent process, this
parent process creates a new child process which will have a new PID.
Because the global variable "value" is initialized by 19 and the parent process does not change the
value variable at all.
Although "value" is increased by 15 in the child process, it does not change the "value" of parent
process at all since OS allocate different data and memory for these two processes.
3.1.3. Remove line B from the program. Observe your display result onto the screen and give
your remark.
When we remove the line B from the program, the display result will usually be different in order.
In this case, the parent process runs faster.
But when we use "wait(NULL)" in parent process, it will wait for the child process finishes and
runs right after that.
Code for ex 3.1:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
#include <sys/wait.h>
int main() {
pid_t pid;
pid = fork();
if(pid == 0) { /*child process*/
value += 15;
printf("PID of child process = %d \n", getpid()); /*line A*/
return 0;
}
else if(pid > 0) {
wait(NULL); /*line B*/
printf("PARENT: value = %d \n",value); /*line C*/
return 0;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int arr[1005];
if(pid > 0) {
if(arr[i] % 2 == 0) count2++;
bol1 = 1;
}
else {
bol2 = 1;
if(arr[i] % 3 == 0) count3++;
}
}
if(bol1) printf("Parent: %d\n", count2);
if(bol2) printf("Child: %d\n", count3);
return 0;
}
Problem 2 (3 points): The relationship between processes could be represented by a tree. When a
process uses fork system call to create another process then the new process is a child of this
process. This process is the parent of the new process. For examples, if process A uses two fork
system calls to create two new processes B and C then we could display their relationship by a tree
in figure 3.1. B is a child process of A. A is the parent of both B and C.
Write a program that uses fork system calls to create processes whose relationship is similar to the
one showed in Figure 3.2. Note: If a process has multiple children then its children must be created
from left to right. For example, process A must creates B first then create C and finally D.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/wait.h>
pid_t B, C, D, E, F, G, I;
//printf("Tier 1:\n");
B = fork();
int i = 0;
fflush(stdout);
if(I == 0) {
//printf(" Tier 4:\n");
printf(" Process E(id = %d) spawns process I (id =
%d)\n", getppid(), getpid());
}
else wait(NULL);
}
else {
//F
F = fork();
if(F == 0) printf(" Process B(id = %d) spawns process F (id =
%d)\n", getppid(), getpid());
else wait(NULL);
}
}
else {
C = fork();
if(C == 0) {
}
else {
//wait(NULL);
D = fork();