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

Similar documents
Lecture 7: Mutual Exclusion 2/16/12. slides adapted from The Art of Multiprocessor Programming, Herlihy and 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

Mutual Exclusion: Classical Algorithms for Locks

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

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

DD2451 Parallel and Distributed Computing --- FDD3008 Distributed Algorithms

Unit 6: Indeterminate Computation

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

Models of concurrency & synchronization algorithms

1 The comparison of QC, SC and LIN

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

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

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

Concurrency pros and cons. Concurrent Programming Problems. Linked list example. Linked list example. Mutual Exclusion. Concurrency is good for users

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

Constant RMR Transformation to Augment Reader-Writer Locks with Atomic Upgrade/Downgrade Support

Safety and liveness for critical sections

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

Chapter 5 Asynchronous Concurrent Execution

Part III Synchronization Software and Hardware Solutions

Concurrency pros and cons. Concurrent Programming Problems. Mutual Exclusion. Concurrency is good for users

Advanced Multiprocessor Programming

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

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

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

Concurrent Computing

9.5 Equivalence Relations

CHAPTER 8. Copyright Cengage Learning. All rights reserved.

Computing with Infinitely Many Processes under assumptions on concurrency and participation -M.Merritt&G.Taubenfeld. Dean Christakos & Deva Seetharam

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

Thread Synchronization: Too Much Milk

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

CSE 153 Design of Operating Systems

Review. Review. Review. Constructing Reliable Registers From Unreliable Byzantine Components 12/15/08. Space of registers: Transformations:

CSE 486/586 Distributed Systems

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018

Synchronisation algorithms

6.852: Distributed Algorithms Fall, Class 12

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Concurrency: Mutual Exclusion and Synchronization

Synchronization for Concurrent Tasks

Chapter 6: Process Synchronization

Concurrency control CS 417. Distributed Systems CS 417

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

PROCESS SYNCHRONIZATION

PROVING THINGS ABOUT PROGRAMS

Introduction to Linear-Time Temporal Logic. CSE 814 Introduction to LTL

CS 318 Principles of Operating Systems

Do not start the test until instructed to do so!

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

Synchronization I. Jo, Heeseung

THE AUSTRALIAN NATIONAL UNIVERSITY Mid-semester Quiz, Second Semester COMP2310 (Concurrent and Distributed Systems)

Algorithmic Verification. Algorithmic Verification. Model checking. Algorithmic verification. The software crisis (and hardware as well)

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

EXTENDING THE PRIORITY CEILING PROTOCOL USING READ/WRITE AFFECTED SETS MICHAEL A. SQUADRITO A THESIS SUBMITTED IN PARTIAL FULFILLMENT OF THE

Distributed Mutual Exclusion Algorithms

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Fork Sequential Consistency is Blocking

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

Concurrency: Mutual Exclusion and

Mutual Exclusion Algorithms with Constant RMR Complexity and Wait-Free Exit Code

CS 153 Design of Operating Systems Winter 2016

Part II Process Management Chapter 6: Process Synchronization

6.852: Distributed Algorithms Fall, Class 21

Lamport Clocks: Verifying A Directory Cache-Coherence Protocol. Computer Sciences Department

The Java Memory Model

Fork Sequential Consistency is Blocking

Remaining Contemplation Questions

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

Shared Objects. Shared Objects

SPIN, PETERSON AND BAKERY LOCKS

SSC - Concurrency and Multi-threading Advanced topics about liveness

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Dept. of CSE, York Univ. 1

The Drinking Philosophers Problem-1

The Black-White Bakery Algorithm and related bounded-space, adaptive, local-spinning and FIFO algorithms

Process Synchronization(2)

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

arxiv: v8 [cs.dc] 29 Jul 2011

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Concurrency and Synchronization. CS 447 Monday 3:30-5:00 Tuesday 2:00-3:30 Prof. R K Joshi CSE, IIT Bombay

Fault-Tolerance & Paxos

Introducing Shared-Memory Concurrency

Lecture 7: Transactional Memory Intro. Topics: introduction to transactional memory, lazy implementation

Dealing with Issues for Interprocess Communication

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

Fall 2004 CS414 Prelim 1

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002

Wait-Free Regular Storage from Byzantine Components

A DAG-BASED ALGORITHM FOR DISTRIBUTED MUTUAL EXCLUSION ATHESIS MASTER OF SCIENCE

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

IT 540 Operating Systems ECE519 Advanced Operating Systems

k-abortable Objects: Progress under High Contention

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Coordination and Agreement

Transcription:

CSE 539 03/17/2015 Mutual Exclusion Lecture 15 Scribe: Son Dinh Outline of this lecture: 1. Formal problem definitions 2. Solution for 2 threads 3. Solution for n threads 4. Inherent costs of mutual exclusion 1 Formal problem definitions In this lecture, we study some simple, classical mutual exclusion algorithms for coordinating concurrent threads. Even though these algorithms are unlikely to be used in practice, they are worth studying because they help developing our understanding of the correctness issues of synchronization. This knowledge are useful when we reason about more advanced and practical mutual exclusion algorithms. Time notion In concurrent computation, there is really not a sense of time (i.e., a global clock) that all threads in the computation can read and use to relate to each other. Instead, we will think of the world in terms of the ordering among events, and will use time just as a tool for explaining this ordering. In other words, there is a notion of time but it is local and not global. A thread is a a state machine, and its state transitions are called events. Examples of events are assignment of a shared variable, assignment of a local variable, invocation of a method, return from a method, and so on. Events are instantaneous Events are never simultaneous A thread contains a sequence of events a 0, a 1,... We denote the j-th occurrence of an event a i by a j i. We denote an event a precedes another event b by a b, which means a occurs before b. For 2 events a 0 and a 1, such that a 0 a 1, an interval (a 0, a 1 ) is the duration between a 0 and a 1. Intervals may overlap: 1

or they may be disjoint: Interval A 0 = (a 0, a 1 ) precedes interval B 0 = (b 0, b 1 ), denoted by A 0 B 0 if a 1 b 0. There are some properties of events ordering: Irreflexive: never true that A A Anti-symmetric: if A B then not true that B A Transitive: if A B and B C then A C But A B and B A might both be false! Mutual exclusion and critical section Critical section: a block of code that access shared modifiable data or resource that should be operated on by only one thread at a time. Mutual exclusion: a property that ensures that a critical section is only executed by a thread at a time. We consider the following implementation of Counter class, which is correct in a single-threaded execution, but incorrect in multi-threaded execution. 2

We can enforce mutual exclusion for a Counter object with a Lock object satisfies the following interface: Then the updated code for Counter class using Lock object is: Before accessing the critical section coded inside try{} block, thread must acquire lock first. The code for unlock part is put in finally{} to ensure the lock is released even if an exception is thrown in critical section. Let CSi k denotes thread i s k-th critical section execution and CSj m denotes thread j s m-th critical section execution, then mutual exclusion property guarantees that either CSi k CSj m or CSj m CSi k. Regarding to locking algorithms, a good algorithm should have the following properties: Mutual exclusion: as discussed. This is a safety guarantee. 3

Deadlock freedom: this guarantees that system as a whole makes progress. (i.e. If some thread calls lock() and never acquires the lock, then other threads must complete lock() and unlock() calls infinitely often.) Starvation freedom: this guarantees that individual thread makes progress. (i.e. If some thread calls lock(), it will eventually return). Thus, it implies deadlock freedom. Having basic definitions and notions, we next look at algorithms for synchronizing 2 threads. 2 Solution for 2 threads In this section, we consider 2 implementations of the Lock interface. LockOne algorithm The algorithm for LockOne class is as follows: In this algorithm, threads are indexed as 0 or 1 and its ID is obtained by calling T hreadid.get(). We will prove that LockOne satisfies mutual exclusion property. Before going to the proof, we use write A (x = ν) to denote the event in which thread A assigns value ν to variable x, and read A (ν == x) to denote the event in which thread A reads ν from variable x. Lemma 1 The LockOne algorithm satisfies mutual exclusion property. Proof. By contradiction. Suppose not, then there exists 2 critical sections, CS j A and CSk B, of thread A and B respectively, such that CS j A CSk B and CSk B CSj A. For each thread, we consider the last execution of the lock() method before it enters its j-th (k-th) critical section. 4

From the code, the order of events is: write A (flag[a] = true) read A (flag[b] == false) CS A write B (flag[b] = true) read B (flag[a] == false) CS B Since once flag[b] is set to true it remains true (until thread B call unlock()), read A (flag[b] == false) write B (flag[b] = true). Thus, it follows that: write A (flag[a] = true) read A (flag[b] == false) write B (flag[b] = true) read B (flag[a] == false). Hence, write A (flag[a] = true) read B (flag[a] == false), which is impossible without a write to flag[a] in between those 2 events. Contradiction. The LockOne algorithm can still cause deadlock if both events write A (flag[a] = true) and write B (flag[b] = true) happen before while statements. In this case, both thread wait forever. LockTwo algorithm The LockT wo algorithm is shown below: Similar to LockOne algorithm, LockT wo algorithm also satisfies mutual exclusion. We can prove this claim in a similar manner to the proof of LockOne. Lemma 2 The LockT wo algorithm satisfies mutual exclusion. Proof. Again we prove by contradiction. Suppose not, then there exists 2 critical sections, CS j A and CSB k, of thread A and B respectively, such that CSj A CSk B and CSk B CSj A. For each thread, we consider the last execution of the lock() method before it enters its j-th (k-th) critical section. From the code, the order of events is: write A (victim = A) read A (victim == B) CS A write B (victim = B) read B (victim == A) CS B 5

Since thread A reads B from victim (in the read event of thread s event sequence), it follows that thread B writes B to victim before thread A reads from victim. Thus, write A (victim = A) write B (victim == B) read A (victim == B). After that victim == B, and event read B (victim == A) of thread B cannot occur. Contradiction. Similar to the LockOne algorithm, the LockT wo algorithm can also suffer from deadlock. Peterson s algorithm Peterson s algorithm combines ideas from LockOne and LockT wo algorithm to satisfy both mutual exclusion and starvation-free requirements (thus, also satisfy deadlock-free requirement). The algorithm is as follows: Lemma 3 The P eterson algorithm satisfies mutual exclusion. Proof. By contradiction. Suppose not, we consider the executions of lock() by 2 threads, A and B. From the code, the order of events is: write A (flag[a] = true) write A (victim = A) read A (flag[b]) reada ( victim) CS A write B (flag[b] = true) write B (victim = B) read B (flag[a]) read B (victim) CS B Without loss of generality, assume that A was the last thread to write to victim. write B (victim = B) write A (victim == A) Thus, thread A read victim == A in its sequence of events. Since thread A still entered its critical section, it must be true that thread A reads flag[b] == false. Thus it follows that, write B (flag[b] = true) write B (victim = B) write A (victim == A) read A (flag[b] == false) This is impossible because there is no other write false to flag[b] between write B (flag[b] = true) and read A (flag[b] == false). Contradiction. 6

Lemma 4 The P eterson algorithm is starvation-free. Proof. By contradiction. Without loss of generality, suppose thread A waits forever in the lock() method. Particularly, it runs while() forever waiting for either f lag[b] becomes f alse or victim becomes B. If thread B also got stuck in its while() loop, then it must read B from victim. But since victim cannot be both A and B, the hypothesis that thread B also got stuck in its lock() method is impossible. Thus, thread B must be able to enter its critical section. Then after thread B finishes its critical section and calls unlock() method, f lag[b] becomes f alse, and this triggers thread A to enter its own critical section. Hence, thread A must not wait forever at its while() loop. Because starvation-free implies deadlock-free, it follows that P eterson s algorithm is deadlockfree. Corollary 5 The Peterson s algorithm is deadlock-free. 3 Solution for n threads In this section, we discuss a solution for synchronizing n threads, named Lamport sbakery algorithm. The algorithm is shown in the figure below: In this algorithm, a f lag[a] indicates whether thread A wants to enter its critical section, and label[a] is a number indicating the order in which thread A is going to enter its critical section, the smaller the number is, the sooner it enters its critical section. When a thread calls lock(), it updates its label to a value of 1 larger than the current maximum among all labels. There is a 7

chance that 2 threads read the same maximum label at the same time, and thus update their label to the same value. To break tie in this case, the while() loop in the lock() method orders threads using pairs (label[i], i); in which (label[i], i) > (label[k], k) if and only if label[i] > label[k] or label[i] == label[k] and i > k. In thread i s while() loop, it waits until there is no other thread with raised flag and smaller value of its pair (label[k], k). Lemma 6 The Bakery algorithm satisfies mutual exclusion. Proof. Suppose not. Let A and B be two threads which have overlapping critical sections. Let labeling A and labeling B be the labels of A and B right before they enter the critical sections. Without loss of generality, suppose (label[a], A) < (label[b], B). Since labels of A and B do not change until the next call to the lock() method, when thread B successfully passed its test in the while() loop, it must have read flag[a] == false. This means thread B must have read flag[a] == false before thread A updated flag[a] to true. Thus, the sequence of events is: labeling B read B (flag[a] == false) write A (flag[a] = true) labeling A. It means that label[b] < label[a], which contradicts to the hypothesis. Contradiction. Lemma 7 The Bakery algorithm is deadlock-free. Proof. There always exist some thread A with smallest pair (label[a], A), and it never has to wait for any other thread. Lemma 8 The Bakery algorithm is first-come-first-served. Proof. Suppose thread A s doorway precedes thread B s doorway, D A D B. Then label[a] must be smaller than label[b]. Thus, thread B must have been locked while thread A s flag[a] == true. Since Lamport sbakery algorithm is both deadlock-free and first-come-first-served, it is also starvation-free. One concern with this algorithm is that the label can overflow. However, if we use 64-bit integer for labeling, it will be unlikely to happen. 4 Inherent costs of mutual exclusion Although the Lamport sbakery algorithm is succinct, elegant, and fair, it is not used in practice because of its inherent cost: it has to read n labels in the lock() method. Shared read/write memory locations are called Registers, and can be classified as: Multi-Reader Single-Writer (MRSW) (as flag[] in the Bakery algorithm) Multi-Reader Multi-Writer (MRMW) (as victim in the Peterson s algorithm) The others: SRMW and SRSW are not of interested to us 8

There is a theorem states that: At least n MRSW registers are needed to solve deadlock-free mutual exclusion. The Bakery algorithm uses 2n such MRSW registers, thus its bound is pretty tight. There is also another theorem states that: At least n MRMW registers are needed to solve deadlock-free mutual exclusion. This means multiple-writer registers don t help to reduce the bound. Hence, because of the expensive costs inherently associate with the software solutions to the mutual exclusion problem, we need stronger hardware primitives for this problem. 9