Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld Chapter
Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld Chapter 2 Mutual Exclusion using atomic registers: Basic Topics Version: November 2007 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 1
Synchronization Algorithms and Concurrent Programming ISBN: 0131972596, 1 st edition A note on the use of these ppt slides: I am making these slides freely available to all (faculty, students, readers). They are in Power. Point form so you can add, modify, and delete slides and slide content to suit your needs. They obviously represent a lot of work on my part. In return for use, I only ask the following: q That you mention their source, after all, I would like people to use my book! q That you note that they are adapted from (or perhaps identical to) my slides, and note my copyright of this material. Thanks and enjoy! Gadi Taubenfeld All material copyright 2007 Gadi Taubenfeld, All Rights Reserved To get the most updated version of these slides go to: http: //www. faculty. idc. ac. il/gadi/book. htm Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 2
Chapter 2 Mutual Exclusion using atomic registers: Basic Topics 2. 1 Algorithms for Two Processes 2. 2 Tournament Algorithms 2. 3 A Fast Mutual Exclusion Algorithms 2. 4 Starvation-free Algorithms 2. 5 Tight Space bounds 2. 6 Automatic Discovery of algorithms Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 3
The mutual exclusion problem remainder code entry code critical section exit code The problem is to design the entry and exit code in a way that guarantees that the mutual exclusion and deadlock-freedom properties are satisfied. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 4
The mutual exclusion problem q Mutual Exclusion: No two processes are in their critical sections at the same time. q Deadlock-freedom: If a process is trying to enter its critical section, then some process, not necessarily the same one, eventually enters its critical section. q Starvation-freedom: If a process is trying to enter its critical section, then this process must eventually enter its critical section. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 remainder entry code critical section exit code 5
Question: true or false ? Algorithm C q A and B are deadlock-free C is deadlock-free. q A and B are starvation-free C is starvation-free. q A or B satisfies mutual exclusion C satisfies mutual exclusion. remainder entry code of A entry code of B q A is deadlock-free and B is starvation critical section q A is starvation-free and B is deadlock exit code of B -free C is starvation-free. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 exit code of A 6
Atomic read/write registers Proposed solution 1 entry code exit code Thread 0 while (true} { remainder code while (turn = 1) {skip} critical section turn = 1 } Thread 1 while (true} { remainder code while (turn = 0) {skip} critical section turn = 0 } turn 0/1 ümutual exclusion C deadlock-freedom Does it work? Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 7
Proposed solution 2 Thread 0 Thread 1 flag[0] = true flag[1] = true while (flag[1]) {skip} while (flag[0]) {skip} critical section flag[0] = false flag[1] = false flag 0 false 1 false ümutual exclusion C deadlock-freedom Does it work? Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 8
Proposed solution 3 Thread 0 Thread 1 while (flag[1]) {skip} while (flag[0]) {skip} flag[0] = true critical section flag[0] = false flag[1] = true critical section flag[1] = false flag 0 false 1 false C mutual exclusion üDeadlock-freedom Does it work? Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 9
Section 2. 1. 1 Peterson’s algorithm Thread 0 flag[0] = true turn = 1 while (flag[1] and turn = 1) {skip} critical section flag[0] = false Thread 1 flag[1] = true turn = 0 while (flag[0] and turn = 0) {skip} critical section flag[1] = false flag 0 false 1 false turn 0/1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 10
A variant of Peterson’s algorithm Is it correct ? Thread 0 turn = 1 flag[0] = true while (flag[1] and turn = 1) {skip} critical section flag[0] = false Thread 1 turn = 0 flag[1] = true while (flag[0] and turn = 0) {skip} critical section flag[1] = false flag true 0 false 1 false true turn 0/1 0 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 11
Schematic for Peterson’s algorithm Indicate contending b[i] : = true Barrier turn : = i Contention? b[i] = true ? no / maybe yes First to cross the barrier? turn = j ? no critical section yes exit code b[i] = false ? Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 12
Properties of Peterson’s Solution q Satisfies mutual exclusion and starvation-freedom q Memory accesses are assumed to be atomic q Solution for two processes only Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 13
Section 2. 1. 2 Question Using only single-writer registers Replace the multi-writer register turn with two single-writer registers. What is new algorithm? Hint: use Solution #4 for the too-much-milk problem. Answer (Kessels’ Alg. ) turn = 0 turn[0] = turn[1] turn = 1 turn[0] turn[1] Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 14
Solutions for Many Processes How can we use a two-process algorithm to construct an algorithm for many processes? Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 15
Section 2. 2 Tournament Algorithms 1 Chapter 2 2 3 4 5 6 7 8 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 16
Question Assume that the base two-process algorithm satisfies starvation-freedom. Does the tournament algorithm also satisfy starvation-freedom? Yes Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 17
Properties & complexity q Time complexity Fast m Adaptive Fairness m FIFO, . . . Fault-tolerance Local spinning Space complexity Communication primitives m q q q Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 18
Complexity Measures q Counting steps process step complexity m process time complexity m contention-free time complexity q Counting time units m system response time m process response time q Counting communications m distributed shared memory m coherent cashing q Space complexity m Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 19
What is the contention-free time complexity? Thread 0 flag[0] = true turn = 1 while (flag[1] and turn = 1) {skip} critical section flag[0] = false Thread 1 flag[1] = true turn = 0 while (flag[0] and turn = 0) {skip} critical section flag[1] = false 4 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 20
What is the process step complexity? Thread 0 flag[0] = true turn = 1 while (flag[1] and turn = 1) {skip} critical section flag[0] = false Thread 1 flag[1] = true turn = 0 while (flag[0] and turn = 0) {skip} critical section flag[1] = false Theorem: There is no two process mutual exclusion algorithm with an upper bound on the process time/step complexity. [1992] Proof: see Section 3. 2. 5. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 21
What is the system response time? Show a run of Peterson’s algorithm where the system response time is 5 time units. Thread 0 flag[0] = true turn = 1 while (flag[1] and turn = 1) {skip} critical section flag[0] = false 1 Chapter 2 2 3 4 Thread 1 flag[1] = true turn = 0 while (flag[0] and turn = 0) {skip} critical section flag[1] = false 5 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 22
Section 2. 3 Fast Mutual exclusion Algorithm q Mutual exclusion and deadlock-freedom q Starvation of individual processes is possible q fast access in the absence of contention, only 7 accesses to shared memory are needed q With contention m Even if only 2 processes contend, the winner may need to check all the 0(n) shared registers m System response time is of order n time units q n+2 shared registers are used m Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 23
Splitter q At most n-1 can move right n q At most n-1 can move down q At most 1 can win q In solo run 1 win 1 n-1 right n-1 down Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 24
The code of the splitter x=i if y = 1 then go right fi y=1 if x i then go down fi win n win 1 n-1 right n-1 x down y Chapter 2 0 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 25
Notation await (condition) == while (not condition) do skip od Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 26
Fast Mutual exclusion Algorithm code of process i , i {1 , . . . , n} 1 x y 0 b false 2 false n false start: b[i] = true x=i if y 0 then b[i] = false await y = 0 goto start fi y=i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 goto start fi fi critical section y=0 b[i] = false Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 27
Can we switch the order of the last two statements? What is the maximum number of processes that can be in their CS at the same time? start: b[i] = true x=i if y 0 then b[i] = false await y = 0 goto start fi y=i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 goto start fi fi critical section b[i] = false y=0 x Chapter 2 y 0 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 28
Can we replace y=0 in line 12 with. . . ? start: b[i] = true x=i if y 0 then b[i] = false await y = 0 goto start fi y=i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 goto start fi fi critical section y = 0 replace with: if y = i then y = 0 fi b[i] = false Yes Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 29
Can we remove await y=0 in line 4 ? start: b[i] = true x=i if y 0 then b[i] = false remove ? await y = 0 goto start fi y=i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 goto start fi fi critical section y=0 b[i] = false No. Deadlock is possible Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 30
Can we remove await y=0 in line 9 ? start: b[i] = true x=i if y 0 then b[i] = false await y = 0 goto start fi y=i if x i then b[i] = false for j = 1 to n do await b[j] = false od if y i then await y = 0 remove ? goto start fi fi critical section y=0 b[i] = false Yes Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 31
Schematic for the fast algorithm Indicate contending b[i] : = true Contention? y=0? yes Wait until CS is released no The last to cross the barrier! Barrier turn : = i Contention? x i? yes Continue only after it is guaranteed that no one can cross the barrier yes Last to cross the barrier? y=i? no critical section no exit code Chapter 2 Wait until CS is released Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 32
Section 2. 4 Starvation-free Algorithms 4. 1 Basic Definitions (i. e. , FIFO) 4. 2 The Bakery Algorithm 4. 3 The Black-White Bakery Algorithm Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 33
The mutual exclusion problem q Mutual Exclusion q Deadlock-freedom q Starvation-freedom remainder doorway waiting entry code critical section q FIFO exit code Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 34
Section 2. 4. 2 The Bakery Algorithm Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 35
Section 2. 4. 2 The Bakery Algorithm entry remainder 1 2 3 4 5 n 0 0 0 doorway 1 2 3 2 4 waiting 1 2 3 2 4 CS 1 2 2 exit 1 2 2 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 time 36
Implementation 1 code of process i , i {1 , . . . , n} number[i] = 1 + max {number[j] | (1 j n)} for j = 1 to n { await (number[j] = 0) (number[j] > number[i]) } critical section number[i] = 0 number 1 2 3 4 0 0 n 0 0 integer Answer: No! can deadlock Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 37
Implementation 1: deadlock entry remainder 1 2 3 4 5 n 0 0 0 doorway 1 2 2 waiting 1 2 2 CS 1 deadlock exit 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 time 38
Implementation 1 code of process i , i {1 , . . . , n} number[i] = 1 + max {number[j] | (1 j n)} for j = 1 to n { await (number[j] = 0) (number[j] > number[i]) } critical section What if we replace > with ? number[i] = 0 number 1 2 3 4 0 0 n 0 0 integer Answer: does not satisy mutual exclusion Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 39
Implementation 2 code of process i , i {1 , . . . , n} number[i] = 1 + max {number[j] | (1 j n)} for j = 1 to n { await (number[j] = 0) (number[j], j) number[i], i) // lexicographical order } critical section number[i] = 0 number 1 2 3 4 0 0 n 0 0 integer Answer: does not satisfy mutual exclusion Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 40
Implementation 2: no mutual exclusion remainder 1 2 3 4 5 n 0 0 0 entry 2 doorway 0 1 0 2 waiting 1 2 2 CS 1 2 2 exit 1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 time 41
The Bakery Algorithm code of process i , i {1 , . . . , n} choosing[i] = true number[i] = 1 + max {number[j] | (1 j n)} choosing[i] = false for j = 1 to n { await choosing[j] = false await (number[j] = 0) (number[j], j) (number[i], i) } critical section number[i] = 0 choosing number Chapter 2 1 2 3 4 n false false 0 0 0 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 bits integer 42
Is the following version correct? code of process i , i {1 , . . . , n} choosing[i] = true number[i] = 1 + max {number[j] | (1 j n)} choosing[i] = false for j = 1 to n { if i > j { await choosing[j] = false } await (number[j] = 0) (number[j], j) (number[i], i) } critical section number[i] = 0 choosing number Chapter 2 1 2 3 4 n false false 0 0 0 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 bits integer 44
No entry remainder 1 2 3 4 5 n 0 0 0 doorway 0 1 0 2 0 1 waiting 1 2 1 CS 1 2 1 exit 1 Chapter 2 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 time 45
Is the following version correct? code of process i , i {1 , . . . , n} Yes number[i] = -1 number[i] = 1 + max {number[j] | (1 j n) , 0} for j = 1 to n { await number[j] -1 await (number[j] 0) (number[j], j) (number[i], i) } Can we replace with = ? critical section number[i] = 0 choosing number Chapter 2 1 2 3 4 n false false 0 0 0 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 bits integer 46
Computing the Maximum #1 code of process i , i {1 , . . . , n} Correct implementation number[i] = 1 + max {number[j] | (1 j n)} local 1 = 0 for local 2 = 1 to n do local 3 = number[local 2] if local 1 < local 3 then local 1 = local 3 fi od number[i] = 1 + local 1 number Chapter 2 1 0 2 0 3 0 4 0 0 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 n 0 integer 47
Computing the Maximum #2 code of process i , i {1 , . . . , n} Is the following implementation correct ? number[i] = 1 + max {number[j] | (1 j n)} local 1 = i for local 2 = 1 to n do if number[local 1] < number[local 2] then local 1 = local 2 fi od number[i] = 1 + number[local 1] number Chapter 2 1 0 2 0 3 0 4 0 0 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 n 0 integer 48
local 1 No 2 ? 2 3 4 5 n remainder 0 0 0 doorway ? 1 1 1 waiting 1 1 1 CS 1 1 1 entry 1 exit Chapter 2 Passed process 1 Waiting for process 2 time 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 49
Computing the Maximum #3 code of process i , i {1 , . . . , n} Is the following implementation correct ? A difficult question !!! local 1 = i for local 2 = 1 to n do if number[local 1] number[local 2] then local 1 = local 2 fi od number[i] = 1 + number[local 1] This is Problem 2. 39 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 50
Properties of the Bakery Algorithm q Satisfies mutex & FIFO. q The size of number[i] is unbounded. q Safe registers: reads which are concurrent with writes may return arbitrary value. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 51
Section 2. 4. 3 The Black-White Bakery Algorithm Bounding the space of the Bakery Algorithm Bakery (FIFO, unbounded) The Black-White Bakery Algorithm FIFO Bounded space + one bit Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 52
The Black-White Bakery Algorithm color bit entry remainder 1 2 3 4 5 n 0 0 0 doorway 0 1 0 2 waiting 1 2 CS 1 2 exit 1 2 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 time 53
The Black-White Bakery Algorithm Data Structures 1 2 3 4 n choosing bits mycolor number bits {0, 1, . . . , n} color bit Chapter 2 {black, white} Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 54
The Black-White Bakery Algorithm code of process i , i {1 , . . . , n} choosing[i] = true mycolor[i] = color number[i] = 1 + max{number[j] | (1 j n) (mycolor[j] = mycolor[i])} choosing[i] = false for j = 0 to n do await choosing[j] = false if mycolor[j] = mycolor[i] then await (number[j] = 0) (number[j], j) (number[i], i) (mycolor[j] mycolor[i]) else await (number[j] = 0) (mycolor[i] color) (mycolor[j] = mycolor[i]) fi od critical section if mycolor[i] = black then color = white else color = black fi number[i] = 0 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 55
Question What happens if in the algorithm each process chooses its identifier instead of choosing 1 + max ? Would the algorithm be correct? What properties would it satisfy? Answer: Incorrect. Run process i first until it enters its CS; Now run a process with a smaller id until it also enters its CS. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 56
Question Does it matter if we change the order of the last two statements (in the exit code) of the Black-White Bakery Algorithm? Answer: Yes, it matters. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 57
Question Replace the multi-writer bit color with n single-writer bits. Bakery Black-White Bakery Use single-writer bits only! Answer: See page 57. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 58
Tight space bounds for mutual exclusion using atomic registers • A lower bound (Section 2. 5. 1) • An Upper Bound (Section 2. 5. 2) Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 59
A Simple Observation A very simple Lower Bound Theorem: Any deadlock-free mutual exclusion algorithm for n processes using only SWMR registers must use at least n such registers. Proof: Before entering its critical section a process must write at least once … Question: Can we do better using MWMR registers ? Answer: No. (SWMR == Single Writer Multi Reader) Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 60
Tight Space Bounds Section 2. 5. 1 A Lower Bound (very difficult to prove!) Theorem: Any deadlock-free mutual exclusion algorithm for n processes must use at least n shared registers. Proof: (see next few slides) Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 61
Definitions q run – a sequence of events q run x looks like run y to process p q process p is hidden in run x q process p covers register r in run x Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 62
Example run x looks like run y to process p • • • run x p reads 5 from r q writes 6 to r p writes 7 to r q writes 8 to r p reads 8 from r q write 6 to r 1 r 68 • • • run y p reads 5 from r p writes 7 to r q writes 6 to r q reads 6 from r q writes 8 to r p reads 8 from r r 8 The values of the shared registers must also be the same Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 63
Example process p is hidden in run x • • • p reads 5 from r q reads 5 from r p writes 7 to r q writes 8 to r p reads 8 from r q writes 6 from r r writes must be overwritten before any other process has read the value written Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 64
Example process p covers register r in run x • • p reads 5 from r q reads 5 from r p writes 7 to r q writes 8 to r p reads 8 from r p writes 2 to r p covers r at these three points r writes must be overwritten before any other process has read the value written Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 65
Lemma 2. 17 (very simple) run x looks like run y to processes in P P events only x y then, this is also a run z Proof: By induction on the number of events in (z-x) … Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 66
Lemma 2. 18 (very simple) If p is in its critical section in run z then p is not hidden in z. p is in its critical section then, p is not hidden z Proof: Assume to the contrary … by Lemma 2. 17 … Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 67
Lemma 2. 19 Then, for any p there exists y such that x looks like y to p all the processes are hidden y all processes except may p are in their remainders x Proof: By induction on the number of steps of processes other than p in x. . . Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 68
Lemma 2. 19: Example 5 r • • • Chapter 2 w is in its remainder w reads 5 q is in its p reads 5 remainder q reads 5 p writes 7 q writes 8 q reads 8 w writes 3 w reads 3 p write 9 p in its critical section p is in its p exits ü w is hidden remove w ü important: q is still hidden !!! ü q is hidden remove q Formal proof: By induction on the number of steps of processes other than p in x. . . remainder Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 69
Lemma 2. 20 (simple) all the processes are hidden x Then, for every p there exists z only p takes steps z Chapter 2 p covers unique register Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 70
Lemma 2. 20: Proof By Lemma 2. 19: there exists y … By the deadlock-freedom assumption … By Lemma 2. 17 … … p is hidden … imp. by Lemma 2. 18 x looks like y to p all processes except may p are in their remainders all the processes are hidden x p covers unique register z p is in its critical section Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 71
Lemma 2. 21 (main lemma) all the processes are in their remainder x Then, for every set of processes P there exists z only P take steps z P are hidden and cover |P| distinct register Proof: By induction on k the size of P. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 72
Lemma 2. 21 (main lemma) Proof: By induction on k the size of P. q Base: k = 0 is trivial. q Induction hypothesis: Assume that it holds for k 0. q Step: We prove that it holds for k+1. m m m let P be a set of processes where |P|= k; let p be a process not in P. We show that: only P+p take steps z Chapter 2 P+p are hidden and cover |P+p| distinct register Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 73
Lemma 2. 21 each process in PBy takes the induction hypothesis: one step first; all in their remainders ; . . . only P take steps x W 1 W 2 Wi y 1 y 2 . . . yi only p takes steps yj . . . P are hidden and P are hidden? Yes cover |P| distinct P cover W 1 ? Yes zregisters z 2 p covers w 1? z. Yes 1 i p is hidden? Maybe p covers a unique register w 1 W 1 (Lemma 2. 20) This Chapter 2 Wj w 1 w 2 wi completes the lower bound . . . equals P are hidden? Yes P cover Wj ? Yes proof p covers wj? Yes p is hidden? Yes! Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 . . . zj wj 74
Question What breaks in the lower bound proof if we change the definition of “process p is hidden in run x” as follows: • • • p reads 5 from r q reads 5 to r p writes 7 to r q writes 8 to r p reads 8 from r p writes 8 to r r Chapter 2 this write does not change the value of r! 8 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 75
Tight Space Bounds Section 2. 5. 2 An Upper Bound Theorem: There is a deadlock-free mutual exclusion algorithm for n processes that uses n shared bits. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 76
The One-Bit Algorithm code of process i , i {1 , . . . , n} p 3 writes false true 1 b false 2 false 3 false true pn writes false true 4 false true n false true bits critical section Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 77
The One-Bit Algorithm code of process i , i {1 , . . . , n} repeat b[i] = true; j = 1; while (b[i] = true) and (j < i) do if b[j] = true then b[i] = false; await b[j] =false fi j = j+1 od until b[i] = true for j = i+1 to n do await b[j] = false od critical section b[i] = false b Chapter 2 1 2 3 4 false n false Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 bits 78
Question Does the correctness of the One-bit algorithm depend on the order in which the bits are read in the loops ? Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 79
Properties of the One-Bit Algorithm • • • Satisfies mutual exclusion and deadlock-freedom Starvation is possible It is not fast It is not symmetric It uses only n shared bits and hence it is space optimal • Would it work with safe bits? Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 80
Section 2. 5. 3 Question Computing with Infinitely Many Process Does it follow from the lower bound that there is no solution when the number of processes is not known in advance or when the number is infinite? No. It follows that in such a case infinitely many registers are needed. A simple algorithm for infinitely many processes is presented in Section 2. 5. 3. Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 81
Section 2. 5. 3 Computing with Infinitely Many Process: The algorithm 1 2 3 4 5 6 7 8 9 owner 0 0 01 01 0 0 0 0 … other 0 1 01 0 0 0 0 … loser 0 0 01 0 0 0 0 … winner 0 01 0 0 0 0 … process 4 runs alone … process 4 is the winner process 3 runs. . . process 3 lost process 6 runs. . . process 3 lost 10 This is just one game, we need infinitely many … Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 82
Section 2. 6 Automatic Discovery of Algorithms How do we invent algorithms? eureka This is one way See next slide for another way. . . (maybe they are the same. . . ) Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 83
Automatic Discovery Algorithm Generator Chapter 2 algorithms Algorithm Verifier correct algorithms Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 84
System Overview Algorithm Generator algorithms verification results Algorithm Verifier correct algorithms User Interface parameters (# of: processes, bits, lines of code) Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 85
Results User-defined parameters Results Shared Entry Exit Complex Starvation Tested bits conditions freedomalgorithms 2 2 3 3 4 4 4 5 6 6 7 4 6 4 5 6 5 5 1 1 1 1 1 Yes Yes Correct appx. alg. running hours 7, 196, 536, 269 0 216 846, 712, 059 66 39 25, 221, 389 105 0. 4 1, 838, 128, 995 10 47 129, 542, 873 480 1 129, 190, 403 56 1 *900, 000 80 12 *22, 000 106 0. 4 *70, 000 96 1 * This run was stopped after few solutions were found. Not all algorithms were teste Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 86
Next Chapter In Chapter 3 we will consider more advanced solutions to the mutual exclusion problem using atomic registers. -- Gadi Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 87
- Slides: 86