CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS

Slides:



Advertisements
Similar presentations
CS 603 Process Synchronization: The Colored Ticket Algorithm February 13, 2002.
Advertisements

Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black.
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.
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 Bernard Chen Spring 2007.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem.
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.
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS Fall 2011 Prof. Jennifer Welch CSCE 668 Self Stabilization 1.
CPSC 668Set 19: Asynchronous Solvability1 CPSC 668 Distributed Algorithms and Systems Fall 2006 Prof. Jennifer Welch.
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 10: Consensus with Byzantine Failures1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
CPSC 668Set 6: Mutual Exclusion in Shared Memory1 CPSC 668 Distributed Algorithms and Systems Fall 2006 Prof. Jennifer Welch.
The Critical-Section Problem
1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
CPSC 668Set 3: Leader Election in Rings1 CPSC 668 Distributed Algorithms and Systems Spring 2008 Prof. Jennifer Welch.
CPSC 668Set 6: Mutual Exclusion in Shared Memory1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
CPSC 668Set 10: Consensus with Byzantine Failures1 CPSC 668 Distributed Algorithms and Systems Fall 2006 Prof. Jennifer Welch.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Bakery Algorithm - Proof
CPSC 668Self Stabilization1 CPSC 668 Distributed Algorithms and Systems Spring 2008 Prof. Jennifer Welch.
CPSC 668Set 8: More Mutex with Read/Write Variables1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
CPSC 411, Fall 2008: Set 2 1 CPSC 311 Analysis of Algorithms Sorting Lower Bound Prof. Jennifer Welch Fall 2009.
Concurrency in Distributed Systems: Mutual exclusion.
The Critical Section Problem
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS Spring 2014 Prof. Jennifer Welch CSCE 668 Set 19: Asynchronous Solvability 1.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 1 (12/01/2006) Instructor: Haifeng YU.
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.
1 Chapter 10 Distributed Algorithms. 2 Chapter Content This and the next two chapters present algorithms designed for loosely-connected distributed systems.
DISTRIBUTED ALGORITHMS AND SYSTEMS Spring 2014 Prof. Jennifer Welch Set 11: Asynchronous Consensus 1.
THIRD PART Algorithms for Concurrent Distributed Systems: The Mutual Exclusion problem.
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.
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS Spring 2014 Prof. Jennifer Welch CSCE 668 Set 3: Leader Election in Rings 1.
CPSC 668 DISTRIBUTED ALGORITHMS AND SYSTEMS Fall 2011 Prof. Jennifer Welch CSCE 668 Set 6: Mutual Exclusion in Shared Memory 1.
6.852: Distributed Algorithms Spring, 2008 Class 14.
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
Bakery Algorithm - Proof
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
CPSC 411 Design and Analysis of Algorithms
Chapter 6-7: Process Synchronization
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
Concurrent Distributed Systems
Lecture 22 Syed Mansoor Sarwar
The Critical-Section Problem
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Lecture 20 Syed Mansoor Sarwar
Sitting on a Fence: Complexity Implications of Memory Reordering
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Multiprocessor Synchronization Algorithms ( )
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
Lecture 21 Syed Mansoor Sarwar
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS
CPSC 411 Design and Analysis of Algorithms
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Syllabus 1. Introduction - History; Views; Concepts; Structure
Presentation transcript:

CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS Set 7: Mutual Exclusion with Read/Write Variables CSCE 668 DISTRIBUTED ALGORITHMS AND SYSTEMS CSCE 668 Fall 2011 Prof. Jennifer Welch

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

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

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 Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

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

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

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

Mutual Exclusion for Bakery Algorithm Lemma (4.6): If pi is in the critical section, then Number[i] > 0. Proof is a straightforward induction. Mutual Exclusion: Suppose pi and pk 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) Contradiction! Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

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 pi be starved processor with smallest pair (Number[i],i). Any processor entering entry section after pi has chosen its number chooses a larger number. Every processor with a smaller number eventually enters CS (not starved) and exits. Thus pi cannot be stuck at Line 5 or 6. Contradiction! Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

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

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

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 p0 always has priority over p1 ; asymmetric code Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

Bounded 2-Processor ME Algorithm with ND Code for p0 's entry section: . W[0] := 1 wait until W[1] = 0 Code for p0 's exit section: W[0] := 0 Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

Bounded 2-Processor ME Algorithm with ND Code for p1 's entry section: W[1] := 0 wait until W[0] = 0 W[1] := 1 . if (W[0] = 1) then goto Line 1 Code for p1 's exit section: Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

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 Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

Bounded 2-Processor ME Algorithm Code for entry section: W[i] := 0 wait until W[1-i] = 0 or Priority = i W[i] := 1 if (Priority = 1-i) then if (W[1-i] = 1) then goto Line 1 else wait until (W[1-i] = 0) Code for exit section: Priority := 1-i Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

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

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 p1 enters remainder for good, then p0 will keep seeing W[1] = 0. So any deadlock would starve both procs. Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

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. p0 and p1 stuck in entry, Priority = 0 p0 not stuck in Line 2, skips line 5, stuck in Line 6 with W[0] = 1 waiting for W[1] to be 0 p1 skips Line 6, stuck at Line 2 with W[1] = 0, waiting for W[0] to be 0 p0 sees W[1] = 0, enters CS Contradiction! Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

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

Bounded n-Processor ME Alg. Can we get a bounded space mutex algorithm for n larger than 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 Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

Tournament Tree 1 2 3 5 6 7 4 p0, p1 p2, p3 p4, p5 p6, p7 Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

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 p0 on right side, play role of p1 When a proc. wins the 2-proc. algorithm associated with the tree root, it enters CS. Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

More on Tournament Tree Alg. Code in book is recursive. pi begins at tree node 2k + i/2, playing role of pi 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 Set 7: Mutual Exclusion with Read/Write Variables CSCE 668

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. Set 7: Mutual Exclusion with Read/Write Variables CSCE 668