The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Similar documents
CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

CMSC 330: Organization of Programming Languages

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

CMSC 330: Organization of Programming Languages

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

CMSC 132: Object-Oriented Programming II. Threads in Java

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronized Methods of Old Versions of Java

Synchronization in Java

Chapter 32 Multithreading and Parallel Programming

Only one thread can own a specific monitor

Chapter 6: Process Synchronization

CMSC 330: Organization of Programming Languages. Concurrency & Multiprocessing

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

CMSC 433 Programming Language Technologies and Paradigms. Synchronization

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

CS193k, Stanford Handout #8. Threads 3

Java ReentrantLock (Part 1)

Synchronization in Concurrent Programming. Amit Gupta

COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Models of concurrency & synchronization algorithms

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

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Advanced Concepts of Programming

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

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

G52CON: Concepts of Concurrency

CS 10: Problem solving via Object Oriented Programming Winter 2017

Threads Questions Important Questions

Process Management And Synchronization

Process Synchronization

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

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

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Chapter 6: Process Synchronization

Concurrency: Deadlock and Starvation. Chapter 6

CMSC 433 Programming Language Technologies and Paradigms Fall Locks & Conditions

Introduction to Java Threads

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

Dealing with Issues for Interprocess Communication

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Process Synchronization

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Lesson 6: Process Synchronization

Threads, Concurrency, and Parallelism

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Interprocess Communication By: Kaushik Vaghani

Programming Languages

CS11 Java. Fall Lecture 7

Chapter 7: Process Synchronization!

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Java Threads. COMP 585 Noteset #2 1

Synchronization Principles

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

PROCESS SYNCHRONIZATION

Process Synchronization

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

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

CMSC 132: Object-Oriented Programming II

CS3502 OPERATING SYSTEMS

Concurrency in Java Prof. Stephen A. Edwards

CSE332: Data Abstractions Lecture 25: Deadlocks and Additional Concurrency Issues. Tyler Robison Summer 2010

Lecture 10 State dependency

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

Concurrency. Chapter 5

RACE CONDITIONS AND SYNCHRONIZATION

SFDV3006 Concurrent Programming

5. Liveness and Guarded Methods

CHAPTER 6: PROCESS SYNCHRONIZATION

CS 111. Operating Systems Peter Reiher

Concurrent Programming using Threads

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

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

The Java Memory Model

Concurrency COMS W4115. Prof. Stephen A. Edwards Spring 2002 Columbia University Department of Computer Science

Implementing Coroutines. Faking Coroutines in Java

Dr.-Ing. Michael Eichberg

Synchronization problems with semaphores

3C03 Concurrency: Starvation and Deadlocks

Concurrent Programming Lecture 10

Java Programming Lecture 23

Process Synchronization

What's wrong with Semaphores?

Chapter 6: Process Synchronization. Module 6: Process Synchronization

The New Java Technology Memory Model

Transcription:

The Dining Philosophers Problem CMSC 0: Organization of Programming Languages Threads Classic Concurrency Problems Philosophers either eat or think They must have two forks to eat Can only use forks on either side of their plate Avoid deadlock and starvation! CMSC 0 Bad Dining Philosophers Solution Philosophers all pick up the left fork first Deadlock! all are holding the left fork and waiting for the right fork Bad Dining Philosophers Solution Philosophers all pick up the left fork first Philosophers put down a fork after waiting for minutes, then wait minutes before picking it up again Starvation! CMSC 0 CMSC 0 Dining Philosophers Solution you try! Dining Philosophers Example Number the philosophers Start by giving the fork to the philosopher with lower number. Initially, all forks are dirty. When a philosopher wants both forks, he sends a message to his neighbors When a philosopher with a fork receives a message if his fork is clean, he keeps it, otherwise he cleans it and gives it up. After a philosopher eats, his forks are dirty. If a philosopher had requested his fork, he cleans it and sends it. Each philosopher begins with the forks shown. All are dirty. CMSC 0 CMSC 0 6

Dining Philosophers Example Dining Philosophers Example Philosopher sends a message to philosopher that he wants his fork. Their shared fork is dirty, so philosopher cleans it and sends it. Philosopher eats! While he is eating philosopher requests their shared fork. Philosopher is done eating, so his forks become dirty. CMSC 0 7 CMSC 0 8 Dining Philosophers Example Philosophers Implementation Needs Philosopher is done eating, so he honors philosopher s request and cleans the fork and sends it. Philosopher eats! Wait until notified about something by another philosopher stay hungry until you have two forks hold onto your fork until your neighbor needs it Send a message to a philosopher and have it processed at a later time multiple philosophers can send messages to one when philosopher done eating he should process all and here s another problem with these needs CMSC 0 9 CMSC 0 0 Producer/Consumer Problem Suppose we are communicating with a shared variable E.g., some kind of a fixed size buffer holding messages One thread produces input to the buffer One thread consumes data from the buffer Rules: producer can t add input to the buffer if it s full consumer can t take input from the buffer if it s empty Producer / Consumer Idea If the buffer is partially full, producer or consumer can run: producer c b a consumer If the buffer is empty, only the producer can run: producer If the buffer is full, only the consumer can run: e d c b a consumer CMSC 0 CMSC 0

Pseudocode Solution How can we solve this problem using one thread for the producer and one for the consumer? no deadlock no data races you try! Conditions (Java.) interface Lock { Condition newcondition();... interface Condition { void await(); void signalall();... Condition created from a Lock await called with lock held Releases the lock (on the fork or buffer) But not any other locks held by this thread Adds this thread to wait set for lock Blocks the thread Condition... when philosopher is waiting for a fork or consumer is waiting for non empty buffer wait set CMSC 0 CMSC 0 Conditions (Java.) interface Lock { Condition newcondition();... interface Condition { void await(); void signalall();... Producer/Consumer Example Lock lock = new ReentrantLock(); Condition ready = lock.newcondition(); boolean bufferready = false; Object buffer; Condition created from a Lock when philosopher is done eating or when buffer is non empty: signallall called with lock held Resumes all threads on lock s wait set Those threads must reacquire lock before continuing (This is part of the function; you don t need to do it explicitly) Condition... wait set void produce(object o) { while (bufferready){ ready.await(); buffer = o; bufferready = true; ready.signalall(); Object consume() { while (!bufferready){ ready.await(); Object o = buffer; bufferready = false; ready.signalall(); CMSC 0 CMSC 0 6 Use This Design This is the right solution to the problem Tempting to try to just use locks directly Very hard to get right Problems with other approaches often very subtle here are a few bad solutions Broken Producer/Consumer Example Lock lock = new ReentrantLock(); boolean valueready = false; Object value; void produce(object o) { while (valueready); value = o; valueready = true; Object consume() { while (!valueready); Object o = value; valueready = false; Threads wait with lock held no way to make progress CMSC 0 7 CMSC 0 8

Broken Producer/Consumer Example Lock lock = new ReentrantLock(); boolean valueready = false; Object value; void produce(object o) { while (valueready); value = o; valueready = true; Object consume() { while (!valueready); Object o = value; valueready = false; valueready accessed without a lock held race condition CMSC 0 9 Broken Producer/Consumer Example Lock lock = new ReentrantLock(); Condition ready = lock.newcondition(); boolean valueready = false; Object value; void produce(object o) { Object consume() { if (valueready) if (!valueready) ready.await(); ready.await(); value = o; Object o = value; valueready = true; valueready = false; ready.signalall(); ready.signalall(); what if there are multiple producers or consumers? CMSC 0 0 Why was it broken? Suppose you have consumers, producer Producer starts. valueready set to true. Both consumers exit while loop and try to aquire lock. One consumer gets the lock and consumes the input. The next consumer is still able to get the lock. ERROR! More on the Condition Interface interface Condition { void await(); boolean await (long time, TimeUnit unit); void signal(); void signalall();... away(t, u) waits for time t and then gives up Result indicates whether woken by signal or timeout signal() wakes up only one waiting thread Tricky to use correctly Have all waiters be equal, handle exceptions correctly Highly recommended to just use signalall() CMSC 0 CMSC 0 Await and SignalAll Gotcha s await must be in a loop Don t assume that when wait returns conditions are met Avoid holding other locks when waiting awaitonly gives up locks on the object you wait on Wait and NotifyAll (Java.) Recall that in Java., use synchronize on object to get associated lock object o o s lock o s wait set Objects also have an associated wait set CMSC 0 CMSC 0

Wait and NotifyAll (cont d) Producer/Consumer in Java. o.wait() (same as await) Must hold lock associated with o Release that lock And no other locks Adds this thread to wait set for lock Blocks the thread o.notifyall() (same as signalall) Must hold lock associated with o Resumes all threads on lock s wait set Those threads must reacquire lock before continuing (This is part of the function; you don t need to do it explicitly) public class ProducerConsumer { private boolean valueready = false; private Object value; synchronized void produce(object o) { while (valueready) wait(); value = o; valueready = true; notifyall(); synchronized Object consume() { while (!valueready) wait(); valueready = false; Object o = value; notifyall(); return o; synchronizes on lock for this waits using lock and wait set for this CMSC 0 CMSC 0 6 Aspects of Synchronization Atomicity Locking to obtain mutual exclusion What we most often think about Visibility Ensuring that changes to object fields made in one thread are seen in other threads Ordering Ensuring that you aren t surprised by the order in which statements are executed Quiz Can this result in i=0 and j=0? CMSC 0 7 CMSC 0 8 Doesn t Seem Possible... How Can This Happen? Compiler can reorder statements Or keep values in registers Processor can reorder them On multi-processor, values not synchronized in global memory so the data change may not be visible to all threads yet But this can happen! CMSC 0 9 CMSC 0 0

When Are Actions Visible? Must be the same lock Forcing Visibility of Actions All writes from thread that holds lock M are visible to next thread that acquires lock M Must be the same lock Use synchronization to enforce visibility and ordering As well as mutual exclusion CMSC 0 CMSC 0 Volatile Fields Fields which are visible immediately across all threads If you are going to access a shared field without using synchronization It needs to be volatile Example uses A one-writer/many-reader value Simple control flags: volatile boolean done = false; Keeping track of a recent value of something Misusing Volatile Incrementing a volatile field can cause a data race (just as for any other field) A volatile reference to an object isn t the same as having the fields of that object be volatile No way to make elements of an array volatile Can t keep two volatile fields in sync Don t use for this course CMSC 0 CMSC 0 Guidelines for Programming w/threads Synchronize access to shared data Don t hold multiple locks at a time Could cause deadlock Hold a lock for as little time as possible Reduces blocking waiting for locks While holding a lock, don t call a method you don t understand E.g., a method provided by someone else, especially if you can t be sure what it locks Corollary: document which locks a method acquires Thread Cancellation Example scenarios: want to cancel thread Whose processing the user no longer needs (i.e., she has hit the cancel button) That computes a partial result and other threads have encountered errors, etc. Java used to have Thread.kill() But it and Thread.stop() are deprecated Use Thread.interrupt() instead CMSC 0 CMSC 0 6 6

Thread.interrupt() Tries to wake up a thread Sets the thread s interrupted flag Flag can be tested by calling interrupted() method Clears the interrupt flag isinterrupted() method Does not clear the interrupt flag Won t disturb the thread if it is working Not asynchronous! Cancellation Example public class CancellableReader extends Thread { private FileInputStream datafile; public void run() { try { while (!Thread.interrupted()) { try { int c = datafile.read(); if (c == -) break; else process(c); catch (IOException ex) { break; finally { // cleanup here What if the thread is blocked on a lock or wait set, or sleeping when interrupted? This could acquire locks, be on a wait set, etc. CMSC 0 7 CMSC 0 8 InterruptedException Exception thrown if interrupted on certain ops wait, await, sleep, join, and lockinterruptibly Also thrown if call one of these with interrupt flag set Not thrown when blocked on. lock or I/O class Object { void wait() throws IE;... interface Lock { void lock(); void lockinterruptibly() throws IE;... interface Condition { void await() throws IE; void signalall();... CMSC 0 9 Responses to Interruption Early Return Clean up and exit without producing errors May require rollback or recovery Callers can poll cancellation status to find out why an action was not carried out Continuation (i.e., ignore interruption) When it is too dangerous to stop When partial actions cannot be backed out When it doesn t matter CMSC 0 0 Responses to Interruption (cont d) Re-throw InterruptedException When callers must be alerted on method return Throw a general failure exception When interruption is a reason method may fail In general Must reset invariants before cancelling E.g., close file descriptors, notify other waiters, etc. Handling InterruptedException synchronized (this) { while (!ready) { try { wait(); catch (InterruptedException e) { // make shared state acceptable notifyall(); // cancel processing return; // do whatever CMSC 0 CMSC 0 7

Why No Thread.kill()? What if the thread is holding a lock when it is killed? The system could Free the lock, but the data structure it is protecting might be now inconsistent Keep the lock, but this could lead to deadlock A thread needs to perform its own cleanup Use InterruptedException and isinterrupted() to discover when it should cancel CMSC 0 8