Anna Savarin HW#2 Question 1. Pg. 273, Exercise 7.1. Q: The first known correct software solution to the critical-section problem for two threads was developed by Dekker; it is shown in Figure 7.43. The two threads, T0 and T1, coordinate activity sharing an object of class Dek- ker. Show that the algorithm satisfies all three requirements for the critical section problem. A: The three criteria referred to by the book are mutual exclusion, progress, and bounded waiting. (1) Mutual exclusion is achieved by the use of two variables, flag and turn. If T0 has its flag set to true, and T1's flag is false, then T0 has no problem entering its critical section. If both flags are true, then the turn will be checked to see who is up next, T0 or T1. If T1 is up next, then T0 will wait its turn; otherwise, T0 will go while T1 waits. So, in the possible situation where both processes are interested in entering the critical section, the process whose turn it is will go and, once it is done, the turn will be handed to the other process who will now have the entrance to the critical section. (2) This solution guarantees progress because all possible flag-turn combinations allow the interested processes to proceed. In the case of both processes with their flags being true, the turn variable will determine who gets to go and the turn variable can only equal to one value at a time. Once a process comp- letes its turn, it will give the turn to the other process, allowing it to go. The only way for the processes to not enter their critical section is when they are not interested. (3) Bounded waiting is guaranteed in the following way. If T1 has just exited its critical section and attempts to gain entry again while T0's flag is true, then, upon T1's exit, the turn will be given to T0. In this case, T0 will have the turn and the flag, which will cause T0 to proceed and T1 to clear its flag and wait. Question 2. Q: Peterson's solution to MUTEX was for two processes. (a) Generalize Peterson's solution to an arbitrary number of processes. Please write the solution in a conceptual formulation. (b) Show that your solution achieves mutual exclusion. (c) Show that no process can be locked out in your solution. (d) Show that your solution is fair. A: (a) For processes 0 through n: Loop Forever Process i wants = true; turn = ( i + 1 ) % n; (in other words, give the turn to the neighbor) while ( neighbor wants and it is neighbor's turn ); keep giving turn to the next process after neighbor and waiting if the process wants to go until turn comes back to you Critical Section only if you checked on everyone and made sure that everyone who wanted to go before you went; hold on to your turn while in CS Process i wants = false; turn released; Non-Critical Section The generalization here is the following. Assign turns to processes 0 through n consecutively and let them go if they want to go and it is their turn. Otherwise, proceed to your critical section but HOLD ON TO YOUR TURN. It is analogous to keeping, say, a token in your pocket that says, "It is my turn and I will do what I have to do with this token in my pocket. I will pass on this token once I am done." (b) This solution achieves mutual exclusion because the process that has the turn will go. All other processes have to wait for the turn to come to them and will not be able to enter the critical section without the turn "token." (c) A process can only block itself while it waits for its neighbors to take their turn. The turn is guaranteed to come back to the process and allow the process to go since it will want to go and have the turn allow it to do so. (d) No single process will have to wait longer than any other assuming all processes want to go. All processes will have to let everyone go once before proceeding. The waiting is bounded, however, to at most the amount of time it takes everyone else to skip you.