Concurrency 6 - Deadlock

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

Concurrency: Deadlock 1. Magee/Kramer 2 nd Edition

Chapter 6. Deadlock. DM519 Concurrent Programming

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

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

3C03 Concurrency: Starvation and Deadlocks

Concurrency: State Models & Design Patterns

5. Liveness and Guarded Methods

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

CSCI 5828: Foundations of Software Engineering

Monitors & Condition Synchronization

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

Communications Software Engineering Condition Synchronization and Liveness

SFDV3006 Concurrent Programming

Monitors & Condition Synchronization

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors

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

Monitors & Condition Synchronisation

Concurrency in Object Oriented

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

Monitors & Condition Synchronization

C340 Concurrency: Semaphores and Monitors. Goals

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

Only one thread can own a specific monitor

THE QUEEN S UNIVERSITY OF BELFAST

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

Lecture 10: Introduction to Semaphores

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronized Methods of Old Versions of Java

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

Shared Objects & Mutual Exclusion

Two Types of Semaphores

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

Programming Language Concepts: Lecture 13

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

CS18000: Programming I

Problems with Concurrency. February 19, 2014

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

Concurrency in Java Prof. Stephen A. Edwards

CS 3430/5430 Homework 3 Solution Due: Saturday, Feb. 25 (11:59 PM)

Threads Chate Patanothai

Synchronization in Concurrent Programming. Amit Gupta

MCS-378 Intraterm Exam 1 Serial #:

Java Threads. COMP 585 Noteset #2 1

Lecture 9: Introduction to Monitors

Section 13: CSP to OCCAM. Evolution of Software Languages

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

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

Faculty of Computers & Information Computer Science Department

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Programming Language Concepts: Lecture 11

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

THREADS & CONCURRENCY

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

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

Threads. What Is a Thread? Customizing a Thread's run Method. Understanding Threads. Subclassing Thread and Overriding run. Thread Objects.

Concurrent Programming

Monitors; Software Transactional Memory

Homework #3. CS 318/418/618, Fall Handout: 10/09/2017

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

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

Using Diposets to Model Concurrent Systems

Threads and Parallelism in Java

Last Class: Synchronization Problems!

Message Passing Part I

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

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

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

Resource Allocation - Dining Philosophers. Dining Philosophers - Properties. OK? EATING[(i%N)+1]) Impossibility Result for Symmetric Algorithm

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

Monitors; Software Transactional Memory

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

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

THREADS & CONCURRENCY

Multithreading Pearson Education, Inc. All rights reserved.

COMP30112: Concurrency Topics 5.2: Properties

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Multiple Inheritance. Computer object can be viewed as

Model Requirements and JAVA Programs MVP 2 1

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

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Multi-threaded programming in Java

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

CS162 Operating Systems and Systems Programming Lecture 7 Semaphores, Conditional Variables, Deadlocks"

Answer the first question and two further questions. i. action prefix ( -> ) [3 marks]

Process Management And Synchronization

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

What is a thread anyway?

Reintroduction to Concurrency

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

Safety & Liveness Properties

Deadlock Bug Detection Techniques

CS450 OPERATING SYSTEMS FINAL EXAM ANSWER KEY

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

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Transcription:

Concurrency 6 - Deadlock

Monitors & Condition Synchronization - Repetition!!"! "#$! %!#$ &' (&') #&' #&' #

wait(), notify(), and notifyall() - Repetition public final void wait() throws InterruptedException; )! Thread A notify() Monitor data wait() Thread B public final void notify(); public final void notifyall();

The Car Park Example - Repetition * #( #(!#&+'

Condition Synchronization (in Java) - Repetition class CarParkControl { protected int spaces, capacity; } synchronized void arrive() throws Int Exc { while (spaces==0) wait(); --spaces; notify(); } synchronized void depart() throws Int Exc { while (spaces==capacity) wait(); ++spaces; notify(); }

Semaphores - Repetition,!(#(-! #$!#,! #- (&' &./' &'0!!&' &!' ( &'0!!"#

Nested Monitors - Bounded Buffer Model - Repetition $%&1 &##'!! 2 34 Composing potential DEADLOCK States Composed: 28 Transitions: 32 in 60ms Trace to DEADLOCK: get 5( ' '

Deadlock! % # &!' 6# - - # (

Deadlock: 4 Necessary and Sufficient Conditions (&)*!(# +, ' - *!# (( *./0' * *#!)7!-!8 &# (('## 10)* &#'!! (#(*

Wait-For Cycle 9( 92( 3 2 9( 9 (2 9(

6.1 Deadlock Analysis - Primitive Processes :;,%& #'STOP MOVE = (north->(south->move north->stop)). MOVE north north 0 1 2 south #$%&,! 2 34 Trace to DEADLOCK: north north

Deadlock Analysis - Parallel Composition :!)!!! P = (x -> y -> P). Q = (y -> x -> Q). D = (P Q). p:p printer scanner q:q printer scanner SYS printer: RESOURCE get put scanner: RESOURCE get put RESOURCE = (get-> put-> RESOURCE). P = (printer.get-> scanner.get-> copy-> printer.put-> scanner.put-> P). Q = (scanner.get-> printer.get-> copy-> scanner.put-> printer.put-> Q). SYS = (p:p q:q {p,q}::printer:resource {p,q}::scanner:resource). 4

Recall the 4 Conditions (&)*!(# +, ' - *!# (( *./0' * *#!)!-!&# (('## 10)* &#'!! (#(*

Deadlock Analysis Avoidance (#1?) (&)*!(# :7!#<!7!

Deadlock Analysis Avoidance (#2?) +, ' - *!# ( (* 7 8 &!' LOCK = (acquire-> release-> LOCK). P = (scanner_printer.acquire-> printer.get-> scanner.get-> copy-> scanner.put-> printer.put-> scanner_printer.release-> P). 4 3 )5&)4

Deadlock Analysis Avoidance (#3?)./0' * *#!)!-! &#(('## ;&) ' P = (printer.get-> GETSCANNER), GETSCANNER = (scanner.get-> copy-> printer.put -> scanner.put-> P timeout -> printer.put-> P). Q = (scanner.get-> GETPRINTER), GETPRINTER = (printer.get-> copy-> printer.put -> scanner.put-> Q timeout -> scanner.put-> Q). 4 #4

Deadlock Analysis Avoidance (#4?) 10)* &#'!! (#(* * P = (printer.get-> scanner.get-> copy-> printer.put-> scanner.put-> P). Q = (printer.get-> scanner.get-> copy-> printer.put-> scanner.put-> Q). 4 &)5#564

6.2 Dining Philosophers ;!! 2!!!# : (!!! (!! 4 3 3 4 2 0 2 1 1 4!(!!!# (# 0

Dining Philosophers - Model Structure Diagram 2789 (# =#) :,$ FORK lef t right phil[1]: PHIL lef t FORK phil[2]: PHIL right right phil[0]: PHIL FORK lef t lef t FORK right phil[4]: PHIL lef t FORK right phil[3]: PHIL

Dining Philosophers - Model const N = 5 FORK = (get-> put-> FORK). PHIL = (sitdown-> right.get-> left.get-> eat-> right.put-> left.put-> arise-> PHIL). )'4 DINING_PHILOSOPHERS = forall [i:0..n-1] (phil[i]:phil {phil[i].left,phil[((i-1)+n)%n].right}::fork).

Dining Philosophers - Model Analysis Trace to DEADLOCK: phil.0.sitdown phil.0.right.get phil.1.sitdown phil.1.right.get phil.2.sitdown phil.2.right.get phil.3.sitdown phil.3.right.get phil.4.sitdown phil.4.right.get 5(!! # ) (!!!! 5#!!!( # &);0 ) '+

Dining Philosophers # 9(#!! >

Dining Philosophers - Implementation in Java &! ' 7! &! ' Applet Thread display Diners 1 n 1 Philosopher view n Fork display controller PhilCanvas

Dining Philosophers Fork (Monitor) class Fork { private boolean taken = false; private PhilCanvas display; private int identity; } Fork(PhilCanvas disp, int id) { display = disp; identity = id;} synchronized void get() throws Int Exc { while (taken) wait(); taken = true; display.setfork(identity, taken); } synchronized void put() { taken = false; display.setfork(identity, taken); notify(); } taken

Dining Philosophers Philosopher (Thread) class Philosopher extends Thread { public void run() { try { while (true) { view.setphil(identity,view.thinking); sleep(controller.thinktime()); },( } view.setphil(identity,view.hungry); right.get(); view.setphil(identity,view.gotright); sleep(500); // constant sleep! left.get(); view.setphil(identity,view.eating); sleep(controller.eattime()); right.put(); left.put(); } } catch (InterruptedException _) {}

Dining Philosophers Main Applet 5!!1&' &#' 7? for (int i=0; i<n; i++) { fork[i] = new Fork(display, i); }?&#' ( &'1 for (int i=0; i<n; i++) { phil[i] = new Philosopher(this, i, fork[(i-1+n)%n], fork[i]); phil[i].start(); }

Dining Philosophers 5 #) # 5!!! 5@!!@!#

Deadlock-free Philosophers #(-# :;4 :)'')!! A# i!!!! ) PHIL[i:0..N-1] = (when (i%2==0) sitdown-> left.get->...-> PHIL when (i%2==1) sitdown-> right.get->...-> PHIL). 8#4

Maze Example - Shortest Path to STOP (Goal State) =!!!# $%&! $ STOP!+ STOP 0 1 2 north = MAZE 2! 3 4 5 west east # 6 7 8 south! 5MAZE!! eg. MAZE(Start=8) = P[Start], P[0] = (north->stop east->p[1]),...

Maze Example - Shortest Path to STOP (Goal State) GETOUT = MAZE(7).,!!!B> STOP 0 1 2 3 4 5 6 7 8 west north south east Trace to DEADLOCK: east north north west west north

Summary! &!' 6# C,# D: * EF!! 6=-# &#!' % ' 0 * # )';