SYNCHRONIZATION USING SHARED VARIABLES
5.5 The Bakery Algorithm for N Processes
it will find it false because turn1 is greater than turn0. So,p0 will enter its critical section.
Suppose that p0 and p1 begin execution of their entry protocols at about the same time and each completes their first assignment statement; so, each turn variable is 1. Suppose further that, say, p0 completes its second assignment statement and evaluates its condition before p1 executes its second assign- ment statement; so, turn0 is 2 and turn1 is 1. Then, p0 will wait until p1 continues execution and sets turn1 to 3. Note how one process setting its turn variable to 1 forces the other process to wait until the first process has finished picking its value for the turn variable.
Supposep0 and p1, as in the previous case, complete their first assignment statements, which set each turn variable to 1. Suppose now that execution of the second assignment statements is interleaved at the machine instruction level, similar to what was discussed for the Race program in Section 4.1.
That is, the following execution ordering can occur:
1 p0 reads turn1’s value (1) 2 p1readsturn0’s value (1)
3 p0 adds 1 and stores the result (2) into turn0 4 p1 adds 1 and stores the result (2) intoturn1
Although the turn variables have equal values (2), p1 will defer to p0 because its condition uses >= whereas p0’s uses >.
5.5 The Bakery Algorithm for N Processes
The following solution generalizes the two-process solution in the previous section.
50 Synchronization Using Shared Variables
The overall structure is similar to the previous solution. Each process sets its turn variable to be one more than the maximum of the others. As in the previous solution, more than one process can pick the same value for its turn.
Here again, the higher numbered process will defer to the lower numbered process. Specifically, the code uses a loop to check whether to defer to each other process. This checking uses a comparison method that performs a pairwise
“greater than” operator between pairs of (turn, process index). It orders pairs by the value of turns, but within pairs with equal turns, it orders the pairs by process index.
The name of the bakery algorithm is motivated by how each entering cus- tomer (process) in a bakery takes a number just higher than the numbers already taken by waiting customers. As noted above, though, if two customers enter the bakery at about the same time, then they might both pick the same number, so one needs to defer to the other. Another algorithm, the ticket algorithm, is mo- tivated by the ticket machines that are often used in bakeries, which dispense a sequence of increasing numbers to customers. However, it is more complicated to write or it requires the use of a special hardware instruction.
Exercises
5.1 Give all possible outputs from the GeneralForm program assuming that the entry and exit protocols are empty and using the given values for S andN.
Exercises 51 5.2 Consider a variant of the GeneralForm program (with empty entry and
exit protocols) in which N is 3, S is 1, and the critical section code is
Give all possible outputs.
5.3
5.4
5.5
5.6 5.7 5.8
5.9
Give a solution to the N process critical section problem in which, as suggested in Section 5.1, process 0 executes its critical sectionS times in a row, then process 1 executes its critical sectionS times in a row, etc.
Run the code for the BadCSFlag program several times to see whether a race condition actually occurs. It may or may not depending on im- plementation factors. If it does not, then modify the code to, in effect, force a race condition to occur. Do so by placing one or more calls to Thread.sleep in the code. (Hint: first rewrite the code in the style of theRace program in Section 4.1.)
Gather evidence that the processes in the SoSoCSTurns program actually alternate execution of their critical sections. First, add print statements to the program and run the modified program. Then, place several calls toThread.sleep in the code and verify that the output still shows the desired execution order.
Consider again the SoSoCSTurns program. What effect, if any, does initializingturn to 1 instead of 0 have?
Rewrite the SoSoCSTurns program using a family of two processes specified in a single quantified process.
Suppose each process in the SoSoCSTurns program is changed to output x’s value after its for loop. Give all possible output values from this modified program.
Gather evidence that the Bakery2 program executes as it should. Insert print statements whenever a process enters or leaves its critical section, and place a call toThread.sleep within each process’s critical section.
Then, examine the output of the program to verify that it does not show that two processes were in their critical sections at the same time.
Repeat the previous exercise for theBakeryN program.
Consider the Bakery2 program. What effect, if any, does each of the following have?
(a) initializing turn0 and turn1 to 1 instead of 0.
5.10 5.11
52 Synchronization Using Shared Variables (b) initializing turn0 to 1 instead of 0 (but still initializing turn1 to 0).
5.12
5.13
5.14 5.15
Consider the effect of deleting the assignments turn0 =.1 and turn1 = 1 from the Bakery2 program. Does this modified program still provide mutual exclusion? If so, give a convincing argument; if not, give a specific execution ordering showing how both processes can be executing in their critical sections at the same time.
Consider the values taken on by the turn variables in the Bakery2 pro- gram. Are these values bounded or can they become arbitrarily large?
If they are bounded, give a specific bound and explain why that bound holds; otherwise, give a specific execution ordering showing how they can grow arbitrarily large. Give answers for when the program executes S sessions and for when the program executes infinitely.
Consider the code for BakeryN when N is 2. Show that it really is equivalent to the code for Bakery2.
Consider the code for BakeryN. What effect, if any, does eliminating the if statement (but keeping its while loop) have? Explain your answer.
Chapter 6