HOMEWORK 6 SOLUTION ------------------- Mar 20, 2008 (Version 1) Total Points = 20 = 2 + 6 + 6 + 6 Problem 1 (2 Points) --------- (TO COME) Problem 2 (0 Points) --------- a) The test 'turn == other' places a FIFO ordering on entry into the critical section. Without loss in generality, suppose process 0 is in the critical section while process 1 is busy waiting. If process 0 leaves the critical section, it does so by turning off flag[0]. If process 0 attempts to reenter the critical section before process 1 gets to evaluate the conditional of the while statement, it will have set turn equal to other and busy wait. Meanwhile, as soon as process 1 evaluates the conditional part of the while, it will enter the critical section because process 0's execution of the 'turn = other' statement will release process 1. Thus, one process can not monopolize the critical section; i.e., it can not get ahead of the other process if it is also trying to enter the critical section. b) Since there is no mutual waiting from the explanation in Part a, there can not be any deadlock. Problem 3 (6 Points) --------- (TO COME) Problem 4 (0 Points) --------- a) An odd numbered process i waits for its "children" (processes i-1 and Min(i+2,N-1)) to finish computing 'b' and atomically updating 'a' before it computes its 'b' and updates 'a'. An even numbered process i immediately computes 'b' and atomically updates 'a' and signals when it is done through semaphore X[i]. For N=7, the wait-for structure is: 0 2 4 | | | v v v 1 <-- 3 <-- 5 <-- 6 The even numbered processes (0, 2, 4, ...) update 'a' in parallel followed in order by process 5, then 3, and finally 1. b) Y is used to protect the critical region that updates the shared variable 'a'. X[i] is used to signal when process i is done with its computing. c) Let 'go' be a shared variable that is initially 0 which Process 1 toggles between 0 and 1 when it completes its an iteration. All other processes have a local variable 'local_go' that toggles between 0 and 1 but is initially 0. 'local_go' is toggled when it detects that 'go' is different than its 'local_go'. Shared Variables: int go = 0; ... Process i: int y; int local_go; // not used by process 1 do forever { if (i != 1) local_go = go; ... body ... if (i == 1) { go = (if (go == 0) then 1 else 0); } else while (local_go == go) { ... do nothing ... } } You could use a semaphore structure that implements a barrier for the even numbered processes, but it will probably run slower than the above algorithm. Since the odd numbered processes wait for the even number processes to finish, they don't need any extra synchronization. Problem 5 (6 Points) --------- (TO COME) Problem 6 (6 Points) --------- (TO COME)