Download presentation

Presentation is loading. Please wait.

Published byAmya Munk Modified about 1 year ago

1
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem

2
2 The mutual exclusion problem (Dijkstra, 1965) We need to devise a protocol that guarantees mutually exclusive access by processes to a shared resource (such as a file, printer, or code that must be performed in isolation)

3
3 The problem model (reads/writes) Shared-memory multiprocessor: multiple processes Processes can apply atomic reads and writes to shared registers Completely asynchronous

4
4 Mutex: formal definition loop forever Remainder code Entry code Critical section (CS) Exit code end loop Remainder code Entry code CS Exit code

5
5 Mutex Requirements Mutual exclusion: No two processes are at their CS at the same time. Deadlock-freedom: If a process is trying to enter its critical section, then some process eventually enters its critical section. Starvation-freedom (optional): If a process is trying to enter its critical section, then this process must eventually enter its critical section. Assumption: processes do not fail-stop while performing the entry, CS, or exit code.

6
6 Candidate algorithm 1. Program for process 0 1.await turn=0 2.CS of process 0 3.turn:=1 Program for process 1 1.await turn=1 2.CS of process 1 3.turn:=0 initially: turn=0 Does algorithm1 satisfy mutex? Does it satisfy deadlock-freedom? Yes No

7
7 Candidate algorithm 2. Program for both processes 1.await lock=0 2.lock:=1 3.CS 4.lock:=0 initially: lock=0 Does algorithm2 satisfy mutex? Does it satisfy deadlock-freedom? No Yes

8
8 Candidate algorithm 3. initially: flag[0]=false, flag[1]=false Does algorithm3 satisfy mutex? Does it satisfy deadlock-freedom? Program for process 0 1.flag[0]:=true 2.await flag[1]=false 3.CS of process 0 4.flag[0]:=false Program for process 1 1.flag[1]:=true 2.await flag[0]=false 3.CS of process 1 4.flag[1]:=false No Yes

9
9 Peterson’s 2-process algorithm (Peterson, 1981) initially: b[0]=false, b[1]=false, turn=0 or 1 Program for process 0 1.b[0]:=true 2.turn:=0 3.await (b[1]=false or turn=1) 4.CS 5.b[0]:=false Program for process 1 1.b[1]:=true 2.turn:=1 3.await (b[0]=false or turn=0) 4.CS 5.b[1]:=false

10
10 Kessels’ single-writer algorithm (Kessels, 1982) initially: b[0]=false, b[1]=false, turn[0], turn[1]=0 or 1 Program for process 0 1.b[0]:=true 2.local[0]:=turn[1] 3.turn[0]:=local[0] 4.Await (b[1]=false or local[0]<>turn[1] 5.CS 6.b[0]:=false Program for process 1 1.b[1]:=true 2.local[1]:=1-turn[0] 3.turn[1]:=local[1] 4.Await (b[0]=false or local[1]=turn[0] 5.CS 6.b[1]:=false A single-writer register is a register that can be written by a single process only. Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

11
11 Mutual exclusion for n processes: Tournament trees Level 0 Level 1 Level 2 Processes A tree-node is identified by: [level, node#] Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

12
12 Tournament tree based on Peterson’s 2-process alg. Program for process i 1.node:=i 2.For level = 0 to log n-1 do { 3. id:=node mod 2 4. node:= node/2 5. b[level,2node+id]:=true 6. turn[level,node]:=id 7. await (b[level,2node+1-id]=false or turn[level,node]=1-id) 8.} 9.CS 10.for level=log n –1 downto 0 do { 11. node:= i/2 level 12. b[level,2node+id]:=false // id is value set at this node in entry 13.} Variables Per node: b[level, 2node], b[level, 2node+1], turn[level,node] Per process (local): level, node, id.

13
13 The tournament tree using Peterson’s 2-process algorithm satisfies both mutual-exclusion and starvation-freedom.

14
14 Contention-free step complexity The worst-case number of steps for a process to enter the CS when it runs by itself. What’s the contention-free step complexity of Peterson’s tournament tree ? log n Can we do better?

15
15 Lamport’s fast mutual exclusion algorithm Variables Fast-lock, slow-lock initially 0 want[i] initially false Program for process i 1.want[i]:=true 2.fast-lock:=i 3.if slow-lock<>0 then 4. want[i]:=false 5. await slow-lock=0 6. goto 1 7.slow-lock:=i 8.if fast-lock <> i then 9. want[i]:=false 10. for j:=1 to n do await want[j] = false 11. if slow-lock <> i then 12. await slow-lock = goto 1 14.CS 15.slow-lock:=0 16.want[i]:=false

16
16 Schematic for Lamport’s fast mutual exclusion Indicate contention want[i]:=true, fast-lock:=i Is there contention? slow-lock 0? yes Barrier slow-lock:=i CS Wait until CS is released want[i]:=false, await slow-lock:=0 Is there contention? fast-lock i? no EXIT yes Wait until no other process can cross the Barrier Not last to cross Barrier? slow-lock i? yes Wait until CS is released no

17
17 Lamport’s fast mutual exclusion algorithm satisfies both mutual- exclusion and deadlock-freedom.

18
18 First in First Out (FIFO) entry code exit code critical section remainder Mutual Exclusion Deadlock-freedom Starvation-freedom doorway waiting FIFO: if process p is waiting and process q has not yet started the doorway, then q will not enter the CS before p. Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

19
19 Lamport’s bakery algorithm Program for process i 1.choosing[i]:=true 2.number[i]:=max(number[0], …, number[n-1])+1 3.choosing[i]:=false 4.for j:=1 to n(<> i) 5. await choosing[j] = false 6. await number[j]= 0 or (number[j],j) > (number[i],i) 7.CS 8.number[i]:=0 Variables number[i] initially 0 Choosing[i] initialy false

20
20 time Lamport’s Bakery Algorithm doorway 12345n CS exit waiting entry remainder Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

21
21 Implementation 1 code of process i, i {1,..., n} number[i] := 1 + max {number[j] | (1 j n)} for j := 1 to n (<> i) { await (number[j] = 0) (number[j] > number[i]) } critical section number[i] := n numberinteger Answer: No, it can deadlock! Does this implementation work? Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

22
22 time Implementation 1: deadlock doorway 12345n CS exit waiting entry remainder deadlock Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

23
23 number[i] := 1 + max {number[j] | (1 j n)} for j := 1 to n (<> i) { await (number[j] = 0) (number[j],j) > number[i],i) // lexicographical order } critical section number[i] := n numberinteger Answer: It does not satisfy mutual exclusion! Implementation 2 code of process i, i {1,..., n} Does this implementation work? Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

24
24 time Implementation 2: no mutual exclusion doorway 12345n CS exit waiting entry remainder Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

25
25 The Bakery Algorithm code of process i, i {1,..., n} 1: choosing[i] := true 2: number[i] := 1 + max {number[j] | (1 j n)} 3: choosing[i] := false 4: for j := 1 to n do { 5: await choosing[j] = false 6: await (number[j] = 0) (number[j],j) (number[i],i) 7: } 8: critical section 9: number[i] := n choosingbits false numberinteger false Doorway Waiting Bakery

26
26 Computing the maximum code of process i, i {0,..., n-1} local1 := 0 for local2 := 1 to n { local3 := number[local2] if local1 < local3 then {local1 := local3} } number[i] := 1+local n-1 numberchoosing false The correctness of the Bakery algorithm depends on an implicit assumption on the implementation of computing the maximum (statement 2). Below we give a correct implementation. For each process, three additional local registers are used. They are named local1, local2, local3 and their initial values are immaterial. Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

27
27 Question: Computing the maximum code of process i, i {0,..., n-1} local1 := i for local2 := 1 to n { if number[local1] < number[local2] then {local1 := local2} } number[i] := 1+ number[local1] n-1 numberchoosing false Is the following implementation also correct? That is, does the Bakery algorithm solve the mutual exclusion problem when the following implementation is used? Justify your answer. For each process, two additional local registers are used. They are named local1, local2, and their initial values are immaterial. Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

28
time 0000 doorway 12345n CS exit waiting entry remainder 00 ? 1 1 ? local1 2 1 Passed process 1 Waiting for process 2 The 2 nd maximum alg. doesn’t work Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

29
29 Properties of the Bakery algorithm Satisfies Mutual exclusion and first-come-first-served. The size of number[i] is unbounded. –In practice this is not a problem if 64-bit tickets are used There is no need to assume that operations on the same memory location occur in some definite order; it works correctly even when it is allowed for reads which are concurrent with writes to return an arbitrary value.

30
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 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

31
31 time The Black-White Bakery Algorithm doorway 12345n CS exit waiting entry remainder color bit Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

32
32 The Black-White Bakery Algorithm 1234n choosing Data Structures mycolor number color bit bits {0,1,...,n} {black,white} Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

33
33 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 } critical section if mycolor[i] = black then color := white else color := black fi number[i] := 0 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

34
34 A space lower bound for deadlock-free mutex (Burns & Lynch, 1993) How many registers must an n-process deadlock-free mutual exclusion algorithm use if it can only use single-writer registers ? We now prove that the same result holds for multi-reader-multi-writer registers, regardless of their size.

35
35 Some definitions required for the proof Configuration A quiescent configuration Indistinguishable configurations A P-quiescent configuration A covered register An execution

36
36 Example of indistinguishability Execution x is indistinuishable from execution y to process p execution x p reads 5 from r1 q writes 6 to r1 p writes 7 to r1 q writes 8 to r1 p reads 8 from r1 execution y p reads 5 from r1 p writes 7 to r1 q writes 6 to r1 q reads 6 from r1 q writes 8 to r1 p reads 8 from r1 8 r1 8 q writes 6 to r1 6 The values of the shared registers must also be the same Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

37
37 Illustration for Lemma 1 C p i -quiescent, W covered by P Quiescent D C ~ D pipi (by p i ) C1C1 p i in CS (by p i ) D1D1 Q Quiescent (By p j ) R P j in CS Q1Q1 Q ~ Q 1 pjpj (By p j ) Z Both p i, p j in CS! Based on the proof in “Distributed Computing”, by Hagit Attiya & Jennifer Welch

38
38 Illustration for the simple part of Lemma 2 C1C1 {p k,…,p n-1 }-quiescent p 0 …p k-1 cover W p k runs until it covers x ' (p k only) D1D1 Quiescent {p k+1,…,p n-1 }-quiescent W U {x} covered C' 2 D‘ 1 ~ D 1 P-{p k } (by p 0… p k-1 ) C2C2 {p k,…,p n-1 }-quiescent p 0 …p k-1 cover W Based on the proof in “Distributed Computing”, by Hagit Attiya & Jennifer Welch D' 1 p 0… p k-1 write to W and exit x is covered P-{p k } in remainder

39
39 Illustration for the general part of Lemma 2 Based on the proof in “Distributed Computing”, by Hagit Attiya & Jennifer Welch D0D0 D' i 'i'i quiescent D1D1 11 C1C1 11 {p k,…,p n-1 }-quiescent p 0 …p k-1 cover W 1 C2C2 22 {p k,…,p n-1 }-quiescent p 0 …p k-1 cover W 2 CiCi 2 … i {p k,…,p n-1 }-quiescent p 0 …p k-1 cover W i {p k+1,…,p n-1 }-quiescent W U {x} covered C’ j i+1 i+1… j D‘ i ~ D i {p k+1,…,p n-1 }- ii DiDi quiescent CjCj i+1 i+1… j {p k+1,…,p n-1 }-quiescent p 0 …p k-1 cover W i

40
40 A matching upper bound: the one-bit algorithm initially: b[i]:=false Program for process i 1.repeat 2. b[i]:=true; j:=1 3. while (b[i] = true) and (j < i) do 4. if (b[j]=true then 5. b[i]:=false 6. await b[j]=false 7. j:=j+1 8.until b[i]=true 9.for (j:=i+1 to n) do 10. await b[j]=false 11.Critical Section 12.b[i]=false

41
41 Read-Modify-Write (RMW) operations Read-modify-write (w, f) do atomically prev:=w w:=f(prev) return prev Fetch-and-add(w, Δ) do atomically prev:=w w:= prev+Δ return prev Test-and-set(w) do atomically prev:=w w:=1 return prev

42
42 Mutual exclusion using test-and-set Program for process I 1.await test&set(v) = 0 2.Critical Section 3.v:=0 initially: v:=0 Mutual exclusion? Yes Deadlock-freedom? No Starvation-freedom? Yes

43
43 Mutual exclusion using general RMW Program for process I 1.position:=RMW(v, ) 2.repeat 3. queue:=v 4.until queue.first = position.last 5.Critical Section 6.RMW(v, ) initially: v:= How many bits does this algorithm require? Unbounded number, but can be improved to 2 log 2 n

44
44 Lower bound on the number of bits required for mutual exclusion

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google