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

Slides:



Advertisements
Similar presentations
Symmetric Multiprocessors: Synchronization and Sequential Consistency.
Advertisements

CS 603 Process Synchronization: The Colored Ticket Algorithm February 13, 2002.
Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black.
Operating Systems Part III: Process Management (Process Synchronization)
Synchronization. How to synchronize processes? – Need to protect access to shared data to avoid problems like race conditions – Typical example: Updating.
1 Chapter 4 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization.
A Completeness theorem for a class of synchronization objects Afek.Y, Weisberger.E, Weisman.H Presented by: Reut Schwartz.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
6.852: Distributed Algorithms Spring, 2008 Class 13.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 4: Mutual Exclusion.
1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.
1 Chapter 3 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization.
Local-Spin Algorithms Multiprocessor synchronization algorithms ( ) Lecturer: Danny Hendler This presentation is based on the book “Synchronization.
THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem.
CPSC 668Set 7: Mutual Exclusion with Read/Write Variables1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
Multiprocess Synchronization Algorithms ( )
CPSC 668Set 6: Mutual Exclusion in Shared Memory1 CPSC 668 Distributed Algorithms and Systems Fall 2006 Prof. Jennifer Welch.
1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.
CPSC 668Set 6: Mutual Exclusion in Shared Memory1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Bakery Algorithm - Proof
CPSC 668Set 8: More Mutex with Read/Write Variables1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
1 Adaptive and Efficient Mutual Exclusion Presented by: By Hagit Attya and Vita Bortnikov Mian Huang.
Concurrency in Distributed Systems: Mutual exclusion.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
The Critical Section Problem
Mutual Exclusion Presented by: Rohan Sen (Distributed Algorithms Ch. 10)
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem.
1 Chapter 10 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization.
DISTRIBUTED ALGORITHMS AND SYSTEMS Spring 2014 Prof. Jennifer Welch CSCE
Mutual Exclusion Using Atomic Registers Lecturer: Netanel Dahan Instructor: Prof. Yehuda Afek B.Sc. Seminar on Distributed Computation Tel-Aviv University.
1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization.
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS Spring 2014 Prof. Jennifer Welch CSCE 668 Set 8: More Mutex with Read/Write Variables 1.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Local-Spin Mutual Exclusion Multiprocessor synchronization algorithms ( ) Lecturer: Danny Hendler This presentation is based on the book “Synchronization.
CPSC 668 DISTRIBUTED ALGORITHMS AND SYSTEMS Fall 2011 Prof. Jennifer Welch CSCE 668 Set 6: Mutual Exclusion in Shared Memory 1.
Hwajung Lee. Mutual Exclusion CS p0 p1 p2 p3 Some applications are:  Resource sharing  Avoiding concurrent update on shared data  Controlling the.
6.852: Distributed Algorithms Spring, 2008 Class 14.
1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.
Bakery Algorithm - Proof
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
O(log n / log log n) RMRs Randomized Mutual Exclusion
Concurrent Distributed Systems
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Lecture 2 Part 2 Process Synchronization
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Multiprocessor Synchronization Algorithms ( )
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
ITEC452 Distributed Computing Lecture 7 Mutual Exclusion
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Distributed Computing
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Process/Thread Synchronization (Part 2)
Syllabus 1. Introduction - History; Views; Concepts; Structure
Presentation transcript:

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

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 The problem model (reads/writes) Shared-memory multiprocessor: multiple processes Processes can apply atomic reads and writes to shared registers Completely asynchronous

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 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 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 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 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 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 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 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 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 The tournament tree using Peterson’s 2-process algorithm satisfies both mutual-exclusion and starvation-freedom.

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 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 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 Lamport’s fast mutual exclusion algorithm satisfies both mutual- exclusion and deadlock-freedom.

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 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 time Lamport’s Bakery Algorithm doorway 12345n CS exit waiting entry remainder Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

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 time Implementation 1: deadlock doorway 12345n CS exit waiting entry remainder deadlock Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

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 time Implementation 2: no mutual exclusion doorway 12345n CS exit waiting entry remainder Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

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 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 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

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 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.

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 time The Black-White Bakery Algorithm doorway 12345n CS exit waiting entry remainder color bit Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

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 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 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 Some definitions required for the proof Configuration A quiescent configuration Indistinguishable configurations A P-quiescent configuration A covered register An execution

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 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 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 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 11 C1C1 11 {p k,…,p n-1 }-quiescent p 0 …p k-1 cover W 1 C2C2 22 {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 }- ii DiDi quiescent CjCj  i+1  i+1…  j {p k+1,…,p n-1 }-quiescent p 0 …p k-1 cover W i

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 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 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 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 Lower bound on the number of bits required for mutual exclusion