3C03 Concurrency: Starvation and Deadlocks

Similar documents
Communications Software Engineering Condition Synchronization and Liveness

SFDV3006 Concurrent Programming

Concurrency: Deadlock 1. Magee/Kramer 2 nd Edition

Chapter 6. Deadlock Concurrency: Deadlock. Magee/Kramer 2 nd Edition

Deadlock. INF2140 Parallel Programming: Lecture 6. March 07, INF2140 Parallel Programming: Lecture 6 Deadlock

Deadlock. Concepts: Models: Practice: Aim: deadlock avoidance - to design systems where deadlock cannot occur. Chapter 6. Deadlock

Concurrency 6 - Deadlock

Chapter 6. Deadlock. DM519 Concurrent Programming

Concurrency: State Models & Design Patterns

5. Liveness and Guarded Methods

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

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

C340 Concurrency: Semaphores and Monitors. Goals

Chapter 9. Labelled Transition Systems. System Composition. specifications. implementations.

$ Program Verification

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

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

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

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

i219 Software Design Methodology 12. Case study 1 Dining philosopher problem Kazuhiro Ogata (JAIST) Outline of lecture

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

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

THE QUEEN S UNIVERSITY OF BELFAST

Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CS 361 Concurrent programming Drexel University Spring 2000 Lecture 14. The dining philosophers problem

Concurrency in Object Oriented Programs 3. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

CSE 332: Locks and Deadlocks. Richard Anderson, Steve Seitz Winter 2014

Concurrency in Object Oriented

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

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

Only one thread can own a specific monitor

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Lecture 27: Safety and Liveness Properties, Java Synchronizers, Dining Philosophers Problem

Semaphores (by Dijkstra)

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

7 - Safety & Liveness Properties

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

The Drinking Philosophers Problem-1

Object Oriented Programming and Design in Java. Session 18 Instructor: Bert Huang

Process Synchronization

Two Types of Semaphores

Synchronized Methods of Old Versions of Java

CS370 Operating Systems

Concurrency: State Models & Design Patterns

Interprocess Communication By: Kaushik Vaghani

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

Dining Philosophers, Semaphores

Synchronization Principles

The Dining Philosophers with Pthreads

The dining philosophers problem. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 13

Safety & Liveness Properties

CONCURRENT PROGRAMMING - EXAM

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Last Class: Synchronization Problems!

Process Synchronization. studykorner.org

CSE 4/521 Introduction to Operating Systems

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

Lesson 6: Process Synchronization

Starvation and Deadlock

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

Shared Objects & Mutual Exclusion

Concurrent Programming. Concurrent Programming. Today. Comp 104: Operating Systems Concepts 05/01/2017. Concurrent Programming & Threads

Synchronization: Going Deeper

Monitors & Condition Synchronization

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

Process Management And Synchronization

THREADS & CONCURRENCY

5 Classical IPC Problems

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1

Synchronization Principles II

CS370 Operating Systems

Concurrency in Java Prof. Stephen A. Edwards

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Principles of Software Construction: Concurrency, Part 2

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Process Synchronization

COMP 322: Fundamentals of Parallel Programming. Lecture 30: Java Synchronizers, Dining Philosophers Problem

EECS 482 Introduction to Operating Systems

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

CSCI 5828: Foundations of Software Engineering

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

1 /10 2 /15 3 /20 4 /15 total /60. CSE 120 Fall 2007 Midterm Examination. Name. Student ID

Process Synchronization

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

Threads Chate Patanothai

Process Synchronization(2)

Verification of Java programs using networks of finite automata with discrete data.

Synchronization in Concurrent Programming. Amit Gupta

Models of concurrency & synchronization algorithms

safety & liveness properties Safety and Liveness Safety - property specification 7.1 Safety Safety properties Safety properties Concepts: execution

The deadlock problem

Lecture 10: Introduction to Semaphores

Dealing with Issues for Interprocess Communication

Distributed Real-Time Control Systems. Lecture 21 Condition Variables Typical ConcurrencyProblems

Process Synchronization

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

Transcription:

3C03 Concurrency: Starvation and Deadlocks Wolfgang Emmerich 1 Goals Reader/Writer problem Starvation Dining Philosophers Problem Deadlocks Liveness Analysis using LTS 2 1

Reader / Writer Problem Monitors and Java s synchronize statement guarantee mutual access to objects / methods Often it is ok for multiple readers to access the object concurrently Properties required: Demo: Reader/Writer 3 Read/Write Monitor class ReadWrite { private protected int readers = 0; private protected boolean writing = false; // Invariant: (readers>=0 and!writing) or // (readers==0 and writing) synchronized public void acquireread() { while (writing) { wait(); ++readers; synchronized public void releaseread() { --readers; if(readers==0) notify(); synchronized public void acquirewrite() { while (readers>0 writing) { wait(); writing = true; synchronized public void releasewrite() { writing = false; notifyall(); Starvation 4 2

Writer Starvation NotifyAll awakes both readers and writers Program relies on Java having a fair scheduling strategy When readers continually read resource: Writer never gets chance to write. This is an example of starvation. Solution: Avoid writer starvation by making readers defer if there is a writer waiting 5 Read/Write Monitor (Version 2) class ReadWrite { // as before private int waitingw = 0;// # waiting Writers synchronized public void acquireread() { while (writing waitingw>0) { wait(); ++readers; synchronized public void releaseread() { synchronized public void acquirewrite() { while (readers>0 writing) { ++waitingw; try{ wait(); --waitingw; writing = true; synchronized public void releasewrite() { Demo: Reader/Writer v2 6 3

Reader Starvation If there is always a waiting writer: Readers starve Solution: Alternating preference between readers and writers To do so: Another boolean attribute readersturn in Monitor that indicates whose turn it is readersturn is set by releasewrite() and cleared by releaseread() 7 Read/Write Monitor (Version 3) class ReadWrite { // as before private boolean readersturn = false; synchronized public void acquireread() { while(writing (waitingw>0 &&!readersturn)) { wait(); ++readers; synchronized public void releaseread() { --readers; readersturn=false; if(readers==0) notifyall(); synchronized public void acquirewrite() { synchronized public void releasewrite() { writing=false; readersturn=true; notifyall(); Demo: Reader/Writer v3 8 4

Deadlocks Process is in a deadlock if it is blocked waiting for a condition that will never become true Process is in a livelock if it is spinning while waiting for a condition that will never become true (busy wait deadlock) Both happen if concurrent processes and threads are mutually waiting for each other Example: Dining philosophers 9 Dining Philosopher Problem 5 Philosophers sit around table They think or eat 3 2 2 Eat with 2 chopsticks Only 5 chopsticks available Each philosopher only uses sticks to her left and right 4 3 4 0 0 1 1 10 5

FSP Model of Dining Philosophers PHIL=(hungry->left.get->right.get->eating-> left.put->right.put->thinking->phil). FORK = (left.get-> left.put -> FORK right.get->right.put -> FORK). COLLEGE(N=5)= (phil[0..n-1]:phil fork[0..n-1]:fork) /{phil[i:0..n-1].left/fork[i].left, phil[i:0..n-1].right/fork[((i-1)+n)%n].right. LTSA 11 Dining Philosophers in Java class Philosopher extends Thread { int identity; Chopstick left; Chopstick right; Philosopher(Chopstick left,chopstick right){ this.left = left; this.right = right; public void run() { while (true) { try { sleep( ); // thinking right.get(); left.get(); // hungry sleep( ) ; // eating right.put(); left.put(); catch (InterruptedException e) { 12 6

Chopstick Monitor class Chopstick { boolean taken=false; synchronized void put() { taken=false; notify(); synchronized void get() throws InterruptedException { while (taken) wait(); taken=true; 13 Applet for Diners for (int i =0; i<n; ++I) // create Chopsticks stick[i] = new Chopstick(); for (int i =0; i<n; ++i){ // create Philosophers phil[i]=new Philosopher( stick[(i-1+n)%n],stick[i]); phil[i].start(); Demo: Diners 14 7

Deadlock in Dining Philosopher If each philosopher has acquired her left chopstick the threads are mutually waiting for each other Potential for deadlock exists independent of thinking and eating times Only probability is increased if these times become shorter 15 Analysing cause of Deadlock We can use LTS for deadlock analysis A dead state in the composed LTS is one that does not have outgoing transitions Are these dead states reachable? Use of reachability analysis Traces to dead states helps understanding the causes of a deadlock LTSA 16 8

Deadlock Avoidance Deadlock in dining philosophers can be avoided if one philosopher picks up sticks in reverse order (right before left). Demo: Deadlock free Diners What is the problem with this solution? Are there other solutions? Deadlock can also be avoided if there is always one philosopher who thinks 17 Deadlock Free Model PHIL=(hungry->left.get->right.get->eating-> left.put->right.put->thinking->phil). ODDPHIL=(hungry->right.get->left.get->eating-> left.put->right.put->thinking->oddphil). FORK = (left.get-> left.put -> FORK right.get->right.put -> FORK). COLLEGE(N=5)= (phil[0..n-2]:phil phil.4:oddphil fork[0..n-1]:fork) /{phil[i:0..n-1].left/fork[i].left, phil[i:0..n-1].right/fork[((i- 1)+N)%N].right. 18 9

Summary Reader / Writer Problem Starvation Avoidance of Starvation Dining Philosophers Problem Deadlocks and Livelocks Deadlock Avoidance Next Session: Safety 19 10