Lecture 7: Mutual Exclusion 2/16/12. slides adapted from The Art of Multiprocessor Programming, Herlihy and Shavit

Similar documents
Mutual Exclusion. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Programming Paradigms for Concurrency Lecture 2 - Mutual Exclusion

Peterson s Algorithm

Mutual Exclusion. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

What We'll Cover Today

Solution: a lock (a/k/a mutex) public: virtual void unlock() =0;

Mutual Exclusion. 1 Formal problem definitions. Time notion CSE /17/2015. Outline of this lecture:

DD2451 Parallel and Distributed Computing --- FDD3008 Distributed Algorithms

Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast. Synchronization

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

Mutual Exclusion: Classical Algorithms for Locks

10/28/11. DD2451 Overview. DD2451 Overview. FDD 3008 Overview. Course Material

Introduction to Concurrency and Multicore Programming. Slides adapted from Art of Multicore Programming by Herlihy and Shavit

Shared-Memory Computability

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit. Art of Multiprocessor Programming

Concurrent Skip Lists. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Computing

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Coarse-grained and fine-grained locking Niklas Fors

Sequen&al Consistency and Linearizability

CS370 Operating Systems

Unit 6: Indeterminate Computation

Spin Locks and Contention

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Advanced Multiprocessor Programming

Atomicity and Virtualization. Atomicity. Critical section. Edsger s perspective. Virtualizing a resource requires managing concurrent accesses

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

Universality of Consensus. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Process Synchronization

Linked Lists: Locking, Lock-Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrency: Mutual Exclusion and Synchronization

Chapter 6: Process Synchronization

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

Linked Lists: Locking, Lock- Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Programming Paradigms for Concurrency Introduction

CS370 Operating Systems

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Concurrent Objects. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7)

Concurrency: Mutual Exclusion and

DD2451 Parallel and Distributed Computing --- FDD3008 Distributed Algorithms

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Models of concurrency & synchronization algorithms

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

CSCI [4 6] 730 Operating Systems. Example Execution. Process [& Thread] Synchronization. Why does cooperation require synchronization?

Module 6: Process Synchronization

Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 7: Process Synchronization. Background

Dept. of CSE, York Univ. 1

Concurrent Queues, Monitors, and the ABA problem. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Synchronization Principles

Chapter 7: Process Synchronization. Background. Illustration

Process Synchronization

SPIN, PETERSON AND BAKERY LOCKS

Locking Granularity. CS 475, Spring 2019 Concurrent & Distributed Systems. With material from Herlihy & Shavit, Art of Multiprocessor Programming

Part II Process Management Chapter 6: Process Synchronization

Chapter 7: Process Synchronization!

Introduction to Concurrent Programming

10/27/11. Is The Queue Correct? Concurrent Computaton. Objects. A Concurrent FIFO Queue head. A Concurrent FIFO Queue head

Process Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Thread Synchronization: Foundations. Properties. Safety properties. Edsger s perspective. Nothing bad happens

Lecture 3: Synchronization & Deadlocks

Pre- and post- CS protocols. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 7. Other requirements for a mutual exclusion algorithm

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

Lesson 6: Process Synchronization

COMP 322: Principles of Parallel Programming Lecture 11: Parallel Programming Issues (Chapter 6) Fall 2009

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Chapter 6: Synchronization

Synchronization problems with semaphores

PESIT Bangalore South Campus

Thread Synchronization: Too Much Milk

1 The comparison of QC, SC and LIN

DD2451 Parallel and Distributed Computing --- FDD3008 Distributed Algorithms

Review of last lecture. Goals of this lecture. DPHPC Overview. Lock-based queue. Lock-based queue

Chapters 5 and 6 Concurrency

Part III Synchronization Software and Hardware Solutions

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Chapter 6: Process Synchronization

Chapter 6 Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Review of last lecture. Peer Quiz. DPHPC Overview. Goals of this lecture. Lock-based queue

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Process Coordination

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Safety and liveness for critical sections

CS 361 Concurrent programming Drexel University Fall 2004 Lecture 8. Proof by contradiction. Proof of correctness. Proof of mutual exclusion property

Dealing with Issues for Interprocess Communication

Transcription:

Principles of Concurrency and Parallelism Lecture 7: Mutual Exclusion 2/16/12 slides adapted from The Art of Multiprocessor Programming, Herlihy and Shavit

Time Absolute, true and mathematical time, of itself and from its own nature, flows equably without relation to anything external. (I. Newton, 1689) Time is, like, Nature s way of making sure that everything doesn t happen all at once. (Anonymous, circa 1968) time 2

Events An event a 0 of thread A is Instantaneous No simultaneous events (break ties) a 0 time 3

Threads A thread A is (formally) a sequence a 0, a 1,... of events Trace model Notation: a 0 a 1 indicates order a 0 a 1 a 2 time 4

Example Thread Events Assign to shared variable Assign to local variable Invoke method Return from method Lots of other things 5

Threads are State Machines a 0 Events are transitions a 3 a 2 a 1 6

States Thread State Program counter Local variables System state Object fields (shared variables) Union of thread states 7

Concurrency Thread A time Thread B time 8

Interleavings Events of two or more threads Interleaved Not necessarily independent (why?) time 9

Intervals An interval A 0 =(a 0,a 1 ) is Time between events a 0 and a 1 a 0 a 1 A 0 time 10

Intervals may Overlap b 0 B 0 b 1 a 0 A 0 a 1 time 11

Intervals may be Disjoint b 0 B 0 b 1 a 0 A 0 a 1 time 12

Precedence Interval A 0 precedes interval B 0 b 0 B 0 b 1 a 0 A 0 a 1 time 13

Precedence Notation: A 0 B 0 Formally, End event of A 0 before start event of B 0 Also called happens before or precedes 14

Precedence Ordering Remark: A 0 B 0 is just like saying 1066 AD 1492 AD, Middle Ages Renaissance, Oh wait, what about this week vs this month? 15

Precedence Ordering Never true that A A If A B then not true that B A If A B & B C then A C Clearly: A B & B A might both be false! 16

Irreflexive: Never true that A A Antisymmetric: Partial Orders (review) If A B then not true that B A Transitive: If A B & B C then A C 17

Total Orders (review) Also Irreflexive Antisymmetric Transitive Except that for every distinct A, B, Either A B or B A 18

Implementing a Counter public class Counter { private long value; public long getandincrement() { temp = value; value = temp + 1; return temp; Make these steps indivisible using locks 19

Locks (Mutual Exclusion) public interface Lock { public void lock(); public void unlock(); 20

Locks (Mutual Exclusion) public interface Lock { public void lock(); public void unlock(); acquire lock release lock 21

Using Locks public class Counter { private long value; private Lock lock; public long getandincrement() { lock.lock(); try { int temp = value; value = value + 1; finally { lock.unlock(); return temp; 22

Mutual Exclusion Let CS k i execution And CS m j Then either or be thread i s k-th critical section be j s m-th execution CS i k CS j m CS j m CS i k 23

Deadlock-Free If some thread calls lock() And never returns Then other threads must complete lock() and unlock() calls infinitely often System as a whole makes progress Even if individuals starve 24

Starvation-Free If some thread calls lock() It will eventually return Individual threads make progress 25

Two-Thread Conventions class implements Lock { // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; 26

Two-Thread Conventions class implements Lock { // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; Henceforth: i is current thread, j is other thread 27

LockOne class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) {

LockOne class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) { Each thread has flag

LockOne class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) { Set my flag

LockOne class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { flag[i] = true; while (flag[j]) { Wait for other flag to become false

LockOne Satisfies Mutual Exclusion Assume CS A j overlaps CS B k Consider each thread's last (j-th and k- th) read and write in the lock() method before entering Derive a contradiction 32

Deadlock Freedom LockOne Fails deadlock-freedom Concurrent execution can deadlock flag[i] = true; flag[j] = true; while (flag[j]){ while (flag[i]){ Sequential executions OK 33

LockTwo public class LockTwo implements Lock { private int victim; public void lock() { victim = i; while (victim == i) {; public void unlock() { 34

LockTwo Claims Satisfies mutual exclusion If thread i in CS Then victim == j Cannot be both 0 and 1 Not deadlock free Sequential execution deadlocks Concurrent execution does not public void LockTwo() { victim = i; while (victim == i) {; 35

Peterson s Algorithm public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {; public void unlock() { flag[i] = false; 36

Deadlock Free public void lock() { while (flag[j] && victim == i) {; Thread blocked only at while loop only if other s flag is true only if it is the victim Solo: other s flag is false Both: one or the other not the victim CS390C: Art of Principles Multiprocessor of Concurrency Programming 37 and Parallelism

Starvation Free Thread i blocked only if j repeatedly reenters so that flag[j] == true and victim == i When j re-enters it sets victim to j. So i gets in public void lock() { flag[i] = true; victim = i; while (flag[j] && victim == i) {; public void unlock() { flag[i] = false; 38

Bakery Algorithm: Generalizing to n Threads Provides First-Come-First-Served fairness locks have two parts: doorway: bounded number of steps waiting: potentially unbounded number of steps whenever a thread A finishes its doorway before thread B starts its doorway, A cannot be overtaken by B How? Take a number Wait until lower numbers have been served Lexicographic order (a,i) > (b,j) If a > b, or a = b and i > j 39

Bakery Algorithm class Bakery implements Lock { boolean[] flag; Label[] label; public Bakery (int n) { flag = new boolean[n]; label = new Label[n]; for (int i = 0; i < n; i++) { flag[i] = false; label[i] = 0; 40

Bakery Algorithm class Bakery implements Lock { boolean[] flag; Label[] label; 2 6 0 n-1 public Bakery (int n) { flag = new boolean[n]; f f t f f t f f label = new Label[n]; 0 0 4 0 0 5 0 0 for (int i = 0; i < n; i++) { flag[i] = false; label[i] = 0; CS 41

Bakery Algorithm class Bakery implements Lock { public void lock() { flag[i] = true; label[i] = max(label[0],,label[n-1])+1; while ( k flag[k] && (label[i],i) > (label[k],k)); 42

Bakery Algorithm class Bakery implements Lock { Doorway public void lock() { flag[i] = true; label[i] = max(label[0],,label[n-1])+1; while ( k flag[k] && (label[i],i) > (label[k],k)); 43

Bakery Algorithm class Bakery implements Lock { public void lock() { flag[i] = true; label[i] = max(label[0],,label[n-1])+1; I m interested while ( k flag[k] && (label[i],i) > (label[k],k)); 44

Bakery Algorithm Take increasing label (read labels in some arbitrary order) class Bakery implements Lock { public void lock() { flag[i] = true; label[i] = max(label[0],,label[n-1])+1; while ( k flag[k] && (label[i],i) > (label[k],k)); 45

Bakery Algorithm Someone is interested class Bakery implements Lock { public void lock() { flag[i] = true; label[i] = max(label[0],,label[n-1])+1; while ( k flag[k] && (label[i],i) > (label[k],k)); 46

Bakery Algorithm class Bakery implements Lock { boolean flag[n]; int label[n]; Someone is interested public void lock() { flag[i] = true; label[i] = max(label[0],,label[n-1])+1; while ( k flag[k] && (label[i],i) > (label[k],k)); whose (label,i) in lexicographic order is lower 47

Bakery Algorithm class Bakery implements Lock { public void unlock() { flag[i] = false; 48

Bakery Algorithm class Bakery implements Lock { No longer interested public void unlock() { flag[i] = false; 49

Bakery Algorithm class Bakery implements Lock { No longer interested public void unlock() { flag[i] = false; labels are always increasing 49

Timestamps Label variable is really a timestamp Need ability to Read others timestamps Compare them Generate a later timestamp Can we do this without overflow? 50

The Good News One can construct a Wait-free (no mutual exclusion) Concurrent Timestamping system That never overflows 51

One can construct a Wait-free (no mutual exclusion) Concurrent Timestamping system That never overflows The Good News Bad This part is hard 52

Deep Philosophical Question The Bakery Algorithm is Succinct, Elegant, and Fair. Q: So why isn t it practical? A: Well, you have to read N distinct variables 53

Shared Memory Shared read/write memory locations called Registers (historical reasons) Come in different flavors Multi-Reader-Single-Writer (Flag[]) Multi-Reader-Multi-Writer (Victim[]) Not that interesting: SRMW and SRSW 54

Bad News Theorem At least N MRMW multi-reader/multiwriter registers are needed to solve deadlock-free mutual exclusion. (So multiple writers don t help) 55

Theorem (For 2 Threads) Theorem: Deadlock-free mutual exclusion for 2 threads requires at least 2 multi-reader multi-writer registers Proof: assume one register suffices and derive a contradiction 56

Two Thread Execution A B Write(R) R CS CS Threads run, reading and writing R Deadlock free so at least one gets in 57

Covering State for One Register Always Exists B Write(R) In any protocol B has to write to the register before entering CS, so stop it just before 58

Proof: Assume Cover of 1 A B Write(R) CS A runs, possibly writes to the register, enters CS 59

Proof: Assume Cover of 1 A B CS Write(R) CS B Runs, first obliterating any trace of A, then also enters the critical section 60

Proof: Assume Cover of 1 A B CS Write(R) CS B Runs, first obliterating any trace of A, then also enters the critical section 60

This work is licensed under a Creative Commons Attribution- ShareAlike 2.5 License. You are free: to Share to copy, distribute and transmit the work to Remix to adapt the work Under the following conditions: Attribution. You must attribute the work to The Art of Multiprocessor Programming (but not in any way that suggests that the authors endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to http://creativecommons.org/licenses/by-sa/3.0/. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights. 61