Exercises for TDDD82, Processes I January 26, 2018 1. Forking processes (based on [1] Excercise 3.4) Using the program shown below, what will be the output at Line A? #include <sys/types.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> int value = 5; int main() { pid_t pid; pid = fork(); if (pid == 0) { /* child */ value += 15; } else if (pid > 0) { /* parent */ wait(NULL); printf("PARENT: value %d \n", value); /* Line A */ exit(0); } } 2. Which techniques could be used in the program of Exercise 1 in order to get value 20 in the printed output? 1
3. (a) The following pseudo code uses the semaphore operations “wait” and “signal” with the aim of achieving mutual exclusion of a shared re- source. Does this implementation achieve the desire result. If so, explain why. If not, present a trace from the execution of the pro- gram where an undesired behaviour occurs. Process P1 Process P2 while(true) { while(true) { signal(mutex) wait(mutex) Critical_section_1 Critical_section_2 signal(mutex) signal(mutex) Non_critical_section_1 Non_critical_section_2 } } (b) Explain what happens in the system i. In case process P1 crashes while it is not in the critical section (Non_critical_section_1). ii. In case process P2 crashes within the critical section. 2
4. Mutual Exclusion (based on [1] Excercise 6.1) The first known correct software solution to the critical-section problem for two processes was developed by Dekker. The two processes P 0 and P 1 , share the following variables: boolean flag[2]; /* initially false */ int turn; /* initially 0 or 1 */ The structure of process P i ( i = 0 , 1 ) is shown below. It uses j � = i ∈ { 0 , 1 } to refer to the other process’ variables. Sketch a proof that the algorithm satisfies all three requirements for the critical-section problem. 1: do { 2: flag[i] = TRUE; 3: 4: while (flag[j]) { 5: if (turn == j) { 6: flag[i] = FALSE; 7: 8: while (turn == j) 9: ; //do nothing 10: 11: flag[i] = TRUE; 12: } 13: } 14: 15: //critical section 16: 17: turn = j; 18: flag[i] = FALSE; 19: 20: //remainder section 21: 22: } while (TRUE); 5. Deadlock avoidance (Banker’s algorithm). Given the following snapshot of a system: Allocation Max Available ---------- --- --------- A B C A B C A B C P0 0 1 2 1 2 3 2 3 2 P1 1 0 3 4 1 5 P2 2 1 0 3 3 4 P3 4 1 1 7 2 5 P4 1 0 0 3 1 1 3
a) Is the system in a safe state? b) If process P 2 requests the resources (1, 2, 0), can the request be imme- diately granted? c) If, instead, process P 3 requests the resources (1, 1, 1), can the request be immediately granted? References [1] Abraham Silberschatz, Greg Gagne, and Peter Baer Galvin. Operating System Concepts . John Wiley & Sons, seventh edition, 2005. 4
Recommend
More recommend