Professional Documents
Culture Documents
Tutorial 2
Tutorial 2
Exercise 1
1. Assuming no system call fails, what does each of the following programs do? Illustrate the
execution of each of them (by drawing the process creation tree) and specify the displayed
outputs.
a. Program 1
int main() {
pid_t pid;
int x = 1;
pid = fork();
if (pid == 0) {
printf("Child: x = %d\n", ++x);
exit(0);
}
printf("Parent: x = %d\n", --x);
exit(0);
}
b. Program 2
int main() {
fork();
fork();
fork();
printf("Hello!\n");
exit(0);
}
c. Program 3
int main() {
int i;
for (i=0; i<2; i++)
fork();
printf("Hello!\n");
exit(0);
}
d. Program 4
int main() {
if(fork())
fork();
printf("Hello!\n");
1/4
exit(0);
}
e. Program 5
int main() {
if(fork() == 0)
if(fork())
printf("Hello!\n");
return 0;
}
f. Program 6
fork(); fork(); if (fork()) { fork() }
Exercise 2
Consider the following program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define N ...
void main () {
int i;
pid_t pid, ppid, npid;
pid = getpid();
printf("Beginning of process number: %d \n", pid);
for (i = 1; i < N; i++) {
npid = fork();
pid = getpid();
ppid = getppid();
printf("The process is %d, the parent is: %d", pid,ppid);
}
printf("\t \t \t End of process number: %d \n", pid);
}
1. How many processes are created for N=2, N=3, N=4, N = x? Provide the process tree.
2/4
Exercise 3:
Consider the following set of processes:
P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2
Exercise 4
Let's consider three (03) processes P1, P2, and P3 described as follows:
1. Give the execution graph of the following processes using the algorithm:
a. FIFO
b. SJF
c. SRTF
d. Round Robin (quantum = 3)
e. Round Robin (quantum = 2)
NB: x(y)z means that the process does x ms of calculation, then y ms of I/O, and finally z ms
of calculation.
Exercise 5:
In a system with a single disk, 4 processes have the following behavior:
P1 100 40(50)30(40)20
P2 99 30(80)80(20)10
3/4
P3 98 40(40)10
P4 97 80
Give the graph (process, time) showing the execution of these processes following the
preemptive priority algorithm in the following two cases (the processes which are waiting for
the disk are put in a queue managed by a FIFO policy) :
1. The 4 processes are launched at the same time with fixed priority.
2. The 4 processes are launched at the same time, but their priority is variable (the priority
changes). Each time any process leaves the blocked state (end of I/O operation), its priority is
recalculated according to the following formula:
New Priority = Starting Priority - (CPU time used since start) /10
The priority of processes does not change until the next time a process exits the blocked state.
4/4