Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 668Set 7: Mutual Exclusion with Read/Write Variables1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.

Similar presentations


Presentation on theme: "CPSC 668Set 7: Mutual Exclusion with Read/Write Variables1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch."— Presentation transcript:

1 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch

2 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables2 Read/Write Shared Variables In one atomic step a processor can –read the variable or –write the variable –but not both!

3 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables3 Bakery Algorithm An algorithm for –no lockout –mutual exclusion using 2n shared read/write variables –booleans Choosing[i] : initially false, written by p i and read by others –integers Number[i] : initially 0, written by p i and read by others

4 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables4 Bakery Algorithm Code for entry section: Choosing[i] := true Number[i] := max{Number[0], …, Number[n-1]} + 1 Choosing[i] := false for j := 0 to n-1 (except i) do wait until Choosing[j] = false wait until Number[j] = 0 or (Number[j],j) > (Number[i],i) endfor Code for exit section: Number[i] := 0

5 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables5 Bakery Algorithm Provides Mutual Exclusion Lemma (4.5): If p i is in the critical section and Number[k] ≠ 0 (k ≠ i), then (Number[k],k) > (Number[i],i). Proof: Consider two cases: p i in CS and Number[k] ≠ 0 p i 's most recent read of Number[k]; Case 1: returns 0 Case 2: (Number[k],k) > (Number[i],i)

6 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables6 Mutual Exclusion Case 1 p i in CS and Number[k] ≠ 0 p i 's most recent read of Number[k], returns 0. So p k is in remainder or choosing number. p i 's most recent read of Choosing[k], returns false. So p k is not in middle of choosing number. p i 's most recent write to Number[i] So p k chooses number in this interval, sees p i 's number, and chooses a larger one.

7 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables7 Mutual Exclusion Case 2 Is proved using arguments similar to those for Case 1.

8 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables8 Mutual Exclusion for Bakery Algorithm Lemma (4.6): If p i is in the critical section, then Number[i] > 0. –Proof is a straightforward induction. Mutual Exclusion: Suppose p i and p k are simultaneously in CS. –By Lemma 4.6, both have Number > 0. –By Lemma 4.5, (Number[k],k) > (Number[i],i) and (Number[i],i) > (Number[k],k)

9 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables9 No Lockout for Bakery Algorithm Assume in contradiction there is a starved processor. Starved processors are stuck at Line 5 or 6 (wait stmts), not while choosing a number. Let p i be starved processor with smallest (Number[i],i). Any processor entering entry section after p i has chosen its number chooses a larger number. Every processor with a smaller number eventually enters CS (not starved) and exits. Thus p i cannot be stuck at Line 5 or 6.

10 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables10 Space Complexity of Bakery Algorithm Number of shared variables is 2n Choosing variables are variable Number variables are unbounded Is it possible for an algorithm to use less shared space?

11 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables11 Bounded 2-Processor ME Algorithm Uses 3 binary shared read/write variables: W[0] : initially 0, written by p 0 and read by p 1 W[1] : initially 0, written by p 1 and read by p 0 Priority : initially 0, written and read by both

12 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables12 Bounded 2-Processor ME Algorithm with ND Start with a bounded algorithm for 2 processors with ND, then extend to NL, then extend to n processors. Some ideas used in 2-processor algorithm: –each processor has a shared boolean W[i] indicating if it wants the CS –p 0 always has priority over p 1 ; asymmetric code

13 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables13 Bounded 2-Processor ME Algorithm with ND Code for p 0 's entry section: 1. 2. 3W[0] := 1 4. 5. 6wait until W[1] = 0 Code for p 0 's exit section: 7. 8W[0] := 0

14 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables14 Bounded 2-Processor ME Algorithm with ND Code for p 1 's entry section: 1W[1] := 0 2wait until W[0] = 0 3W[1] := 1 4. 5 if (W[0] = 1) then goto Line 1 6. Code for p 1 's exit section: 7. 8W[1] := 0

15 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables15 Discussion of 2-Processor ND Algorithm Satisfies mutual exclusion: processors use W variables to make sure of this Satisfies no deadlock (exercise) But unfair (lockout) Fix by having the processors alternate having the priority: –shared variable Priority, read and written by both

16 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables16 Bounded 2-Processor ME Algorithm Code for entry section: 1W[i] := 0 2wait until W[1-i] = 0 or Priority = i 3W[i] := 1 4if (Priority = 1-i) then 5 if (W[1-i] = 1) then goto Line 1 6else wait until (W[1-i] = 0) Code for exit section: 7Priority := 1-i 8W[i] := 0

17 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables17 Analysis of Bounded 2-Processor Algorithm Mutual Exclusion: Suppose in contradiction p 0 and p 1 are simultaneously in CS. W[0] = W[1] = 1, both procs in CS p 1 's most recent write of 1 to W[1] (Line 3) p 0 's most recent write of 1 to W[0] (Line 3) p 0 's most recent read of W[1] (Line 6): must return 1, not 0!

18 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables18 No-Deadlock for 2-Proc. Alg. Useful for showing no-lockout. If one proc. ever enters remainder for good, other one cannot be starved. –Ex: If p 1 enters remainder for good, then p 0 will keep seeing W[1] = 0. So any deadlock would starve both procs.

19 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables19 No-Deadlock for 2-Proc. Alg. Suppose in contradiction there is deadlock. W.l.o.g., suppose Priority gets stuck at 0 after both processors are stuck in their entry sections. p 0 and p 1 stuck in entry, Priority = 0 p 0 not stuck in Line 2, skips line 5, stuck in Line 6 with W[0] = 1 waiting for W[1] to be 0 p 0 sees W[1] = 0, enters CS p 1 skips Line 6, stuck at Line 2 with W[1] = 0, waiting for W[0] to be 0

20 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables20 No-Lockout for 2-Proc. Alg. Suppose in contradiction p 0 is starved. Since there is no deadlock, p 1 enters CS infinitely often. The first time p 1 executes Line 7 in exit section after p 0 is stuck in entry, Priority gets stuck at 0. p 1 at Line 7; Priority = 0 forever after p 0 stuck in entry p 0 stuck at Line 6 with W[0] = 1, waiting for W[1] to be 0 p 1 enters entry, gets stuck at Line 2, waiting for W[0] to be 0

21 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables21 Bounded n-Processor ME Alg. Can we get a bounded space mutex algorithm for n > 2 processors? Yes! Based on the notion of a tournament tree: complete binary tree with n-1 nodes –tree is conceptual only! does not represent message passing channels A copy of the 2-proc. algorithm is associated with each tree node –includes separate copies of the 3 shared variables

22 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables22 Tournament Tree 1 23 4 567 p 0, p 1 p 2, p 3 p 4, p 5 p 6, p 7

23 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables23 Tournament Tree ME Algorithm Each proc. begins entry section at a specific leaf (two procs per leaf) A proc. proceeds to next level in tree by winning the 2-proc. competition for current tree node: –on left side, play role of p 0 –on right side, play role of p 1 When a proc. wins the 2-proc. algorithm associated with the tree root, it enters CS.

24 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables24 More on Tournament Tree Alg. Code in book is recursive. p i begins at tree node 2 k +  i/2 , playing role of p i mod 2, where k =  log n  -1. After winning node v, "critical section" for node v is –entry code for all nodes on path from v 's parent  v/2  to root –real critical section –exit code for all nodes on path from root to  v/2 

25 CPSC 668Set 7: Mutual Exclusion with Read/Write Variables25 Tournament Tree Analysis Correctness: based on correctness of 2- processor algorithm and tournament structure: –projection of an admissible execution of tournament alg. onto a particular tree node gives an admissible execution of 2-proc. alg. –ME for tournament alg. follows from ME for 2-proc. alg. at tree root. –NL for tournament alg. follows from NL for the 2- proc. algs. at all nodes of tree Space Complexity: 3n boolean read/write shared variables.


Download ppt "CPSC 668Set 7: Mutual Exclusion with Read/Write Variables1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch."

Similar presentations


Ads by Google