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

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

Concurrency: Deadlock 1. Magee/Kramer 2 nd Edition

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

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

Chapter 6. Deadlock. DM519 Concurrent Programming

Concurrency 6 - Deadlock

SFDV3006 Concurrent Programming

Deadlocks. Deadlock in Resource Sharing Environment. CIT 595 Spring Recap Example. Representing Deadlock

3C03 Concurrency: Starvation and Deadlocks

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

Deadlock and Starvation

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

The deadlock problem

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

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

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

5. Liveness and Guarded Methods

Concurrency: Deadlock and Starvation. Chapter 6

THE QUEEN S UNIVERSITY OF BELFAST

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

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

9/30/2014. CS341: Operating System High Level Construct: Monitor Deadlock Conditions Prevention, Avoidance Detection and Recovery

Last Class: Synchronization Problems!

CSCC 69H3 2/1/13. Today: Deadlock. Remember example from last week? Example of Deadlock. Example: dining philosophers: Not just an OS Problem!

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

More on Synchronization and Deadlock

Lecture 9: Introduction to Monitors

OPERATING SYSTEMS. Deadlocks

ECE519 Advanced Operating Systems

Lecture 10: Introduction to Semaphores

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

Concurrency: Deadlock and Starvation. Chapter 6

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

Process Synchronization

The Deadlock Problem (1)

Concurrency: State Models & Design Patterns

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

CSCE Operating Systems Deadlock. Qiang Zeng, Ph.D. Fall 2018

Last Class: Monitors. Real-world Examples

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

Concurrency: Principles of Deadlock. Processes and resources. Concurrency and deadlocks. Operating Systems Fall Processes need resources to run

Deadlock Prevention. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks

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

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

System Model. Types of resources Reusable Resources Consumable Resources

6. Concurrency: Deadlock

Liveness properties. Deadlock

Deadlock. A Bit More on Synchronization. The Deadlock Problem. Deadlock Characterization. Operating Systems 2/7/2005. CSC 256/456 - Spring

Back to synchronization

Deadlock. Concepts to discuss. A System Model. Deadlock Characterization. Deadlock: Dining-Philosophers Example. Deadlock: Bridge Crossing Example

Deadlocks. Dr. Yingwu Zhu

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

Yet another synchronization problem

CMPT 300 Introduction to Operating Systems

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

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

The Deadlock Problem. A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,

Outline. Deadlock. Examples, Conditions, Strategies. Deadlock Prevention. Deadlock Avoidance. Banker s algorithm

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems. Lecture3.2 - Deadlock Management. Golestan University. Hossein Momeni

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,! Silberschatz, Galvin and Gagne 2009!

Deadlock and Starvation

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Chapter 7: Deadlocks. Operating System Concepts with Java 8 th Edition

Deadlock Risk Management

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

CS370 Operating Systems

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

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

Process Management And Synchronization

Chapter 7: Deadlocks

Lesson 6: Process Synchronization

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

CSE 306/506 Operating Systems Deadlock. YoungMin Kwon

JOINT PROGRESS WITH DEADLOCK RESOURCE CATEGORIES AND DEADLOCK

DEADLOCK CS 409, FALL 2013 DEADLOCK AND STARVATION/1

Operating Systems. Operating Systems Sina Meraji U of T

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

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Deadlocks. Thomas Plagemann. With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

Chapter 6 Concurrency: Deadlock and Starvation

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

CS370 Operating Systems

CS450/550 Operating Systems

The Deadlock Problem

CSE 4/521 Introduction to Operating Systems

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

! What is a deadlock? ! What causes a deadlock? ! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

Operating Systems. Deadlocks

Chapter 8: Deadlocks. Bridge Crossing Example. The Deadlock Problem

Lecture 7 Deadlocks (chapter 7)

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

The Deadlock Lecture

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

Transcription:

COMP 150-CCP Concurrent Programming Lecture 12: Deadlock Dr. Richard S. Hall rickhall@cs.tufts.edu Concurrent programming February 28, 2008

Scenario Process 1 gets the lock for object A and wants to lock object B A B Process 2 gets the lock for object C and wants the lock for object A C Process 3 gets the lock for object B and wants the lock for object C What happens next?

Deadlock Concept When all processes in a system are waiting to acquire a shared resource (i.e., all of the processes are blocked), then the system is deadlocked. When a system is deadlocked, it is not possible to execute any actions or make any progress Each process is waiting for a resource to be released, but no process can make progress to release a held resource Deadlock is a serious issue in concurrent systems The goal is to design systems that are free from deadlock

Possibility of Deadlock Serially reusable resources The processes involved share resources which they use under mutual exclusion Incremental acquisition Processes hold on to resources already allocated to them while waiting to acquire additional resources No preemption Once acquired by a process, resources cannot be preempted (forcibly withdrawn) but are only released voluntarily

Existence of Deadlock Serially reusable resources Incremental acquisition No preemption Wait-for cycle A circular chain (or cycle) of processes exists such that each process holds a resource which its successor in the cycle is waiting to acquire Referred to as the four necessary and sufficient conditions for deadlock

Dealing With Deadlock Ignore the problem altogether Prevention Avoidance Detection and recovery

Deadlock Prevention (1/2) Eliminate one of the four necessary conditions Serially reusable resources Not possible to eliminate this, since access to some resources requires mutual exclusion Incremental acquisition Require that all resources are requested at one time Process may block for a long time waiting for all resources Resources may not be used for a long time Not always possible to know all resources in advance

Deadlock Prevention (2/2) No preemption Process must release resource and request again May preempt a process to require it releases its resources Wait-for cycle Define a linear ordering of resource types

Deadlock Avoidance A decision is made dynamically whether the current resource allocation request will, if granted, potentially lead to a deadlock Requires knowledge of future thread resource requests Do not grant an incremental resource request to a thread if this allocation might lead to deadlock e.g., Banker's algorithm Only helps us when allocating with resources, will not help when deadlock occurs due to trying to acquire synchronized locks

Deadlock Detection & Recovery Let deadlock occur Try to detect when a deadlock occurs When a deadlock is detected, take some action to recover from it, e.g., Abort/stop all deadlocked threads Cannot really do this in Java Even if you could, you would likely leave the system in an inconsistent state unless you had some sort of rollback mechanism Try interrupting threads Threads would have to understand that an interrupt meant to let go Would not help if they were blocked trying to acquire a synchronized lock

Primitive Deadlock Analysis In an LTS graph deadlock is easily visible as a state with no outgoing arcs. In FSP we can achieve deadlock using the STOP process: MOVE = (north->(south->move north->stop)). Using the LTSA, we can find the deadlock through safety analysis: Trace to DEADLOCK: north north

Parallel Process Deadlock Analysis In real systems, deadlock may arise from the parallel composition of interacting processes. p:p q:q printer scanner printer scanner SYS printer: RESOURCE get put scanner: RESOURCE get put Deadlock trace? Avoidance? RESOURCE = (lock->unlock->resource). P = (printer.lock->scanner.lock ->copy->scanner.unlock ->printer.unlock->p). Q = (scanner.lock->printer.lock ->copy->printer.unlock ->scanner.unlock->q). SYS = (p:p q:q {p,q}::printer:resource {p,q}::scanner:resource ).

Deadlock Prevention One potential technique for preventing deadlock: If processes share different classes of resources, such as printers and scanners, a general purpose deadlock preventing strategy is to order the resource classes so every process acquires them in the same order. RESOURCE = (lock->unlock->resource). P = (scanner.lock->printer.lock ->copy->printer.unlock ->scanner.unlock->p). Q = (scanner.lock->printer.lock ->copy->printer.unlock ->scanner.unlock->q). SYS = (p:p q:q {p,q}::printer:resource {p,q}::scanner:resource).

Deadlock Prevention Another potential technique for preventing deadlock: It is also possible to use time-out values to prevent deadlock. P = (printer.lock->getscanner), GETSCANNER = (scanner.lock->copy->scanner.unlock ->printer.unlock->p timeout->printer.unlock->p ). Q = (scanner.lock-> GETPRINTER), GETPRINTER = (printer.lock->copy->printer.unlock ->scanner.unlock->q timeout->scanner.unlock->q ). Deadlock? Progress?

Deadlock in Java Similar example in Java but without using locks: // Global space Semaphore scanner = new Semaphore(1); Semaphore printer = new Semaphore(1); // Thread 1 public void run() { printer.down(); } scanner.down(); // do work here scanner.up(); printer.up(); Get printer Get scanner Try scanner Try printer // Thread 2 public void run() { scanner.down(); } printer.down(); // do work here printer.up(); scanner.up();

Deadlock in Java Why does deadlock happen in this example? Because of how semaphores are implemented The down() method of each semaphore will wait() until the semaphore value is non-zero Since the two threads acquire the semaphores in the opposite order, it is possible to interleave their instructions such that one thread gets the scanner and one gets the printer and then they both must wait for each other to finish which will never happen This scenario can be resolved just like the model, use resource acquisition ordering It might also be possible to interrupt the threads here, since wait() is interruptible

Dining Philosophers Example Five philosophers sit around a circular table. Each philosopher spends his life alternately thinking and eating. In the center of the table is a large bowl of spaghetti. A philosopher needs two forks to eat a helping of spaghetti. 4 3 3 4 2 0 2 1 1 0 One fork is placed between each pair of philosophers and they agree that each will only use the fork to his immediate right and left.

Dining Philosophers Structure Diagram Each FORK is a shared resource with get and put actions When hungry, each PHIL must first get his right fork and then his left fork before he can start eating.

Dining Philosopher Model FORK = (acquire->release->fork). PHIL = (hungry->right.acquire->left.acquire ->eat->right.release->left.release ->think->phil). Table of philosophers DINERS(N=5) = forall[i:0..n-1] (phil[i]:phil {phil[i].left,phil[((i-1)+n)%n].right}::fork ). Can this system deadlock?

Dining Philosopher Analysis Trace to DEADLOCK: phil.0.hungry phil.0.right.acquire phil.1.hungry phil.1.right.acquire phil.2.hungry phil.2.right.acquire phil.3.hungry phil.3.right.acquire phil.4.hungry phil.4.right.acquire This is the situation where all the philosophers become hungry at the same time, sit down at the table and each philosopher picks up the fork to his right. The system can make no further progress since each philosopher is waiting for a fork held by his neighbor, i.e., a wait-for cycle exists!

Dining Philosophers Implementation Deadlock is easily detected in our model. How easy is it to detect a potential deadlock in an implementation? \/ \ \/

Dining Philosophers Implementation Philosophers Forks Active entities, implement as threads Shared passive entities, implement as monitors

Dining Philosophers Implementation public class Fork { private boolean taken = false; private int identity; public Fork(int id) { identity = id; } taken encodes the state of the fork public synchronized void acquire() throws InterruptedException { while (taken) { wait(); } taken = true; } } public synchronized void release() { taken = false; notifyall(); }

Dining Philosophers Implementation class Philosopher extends Thread {... public void run() { try { while (true) { // No forks. setstate(thinking); Thread.sleep(50 * identity); // Get right fork. right.acquire(); setstate(gotright); Thread.sleep(50 * identity); // Get left fork. left.acquire(); setstate(gotboth); Thread.sleep(50 * identity); // Release forks. right.release(); left.release(); } } catch (InterruptedException ex) { } } }

Dining Philosophers Implementation Code to create the philosopher threads and fork monitors in Main: forks = new Fork[N]; phils = new Philosopher[N]; for (int i = 0; i < N; i++) { forks[i] = new Fork(i); } for (int i = 0; i < N; i++) { phils[i] = new Philosopher( this, i, forks[(i - 1 + N) % N], forks[i]); new Thread(phils[i]).start(); }

Deadlock in Implementation Deadlock is possible in the implementation, but it might not be as easy to spot. The program can run for a long time without deadlocking. \ \ \ \ \

Fixing the Model We can fix the implementation by eliminating the wait-for cycle... How? Introduce an asymmetry into our definition of philosophers. Use the identity of each philosopher to make even numbered philosophers get their left forks first, odd their right first. Other strategies? PHIL(I=0) = (when (I%2==0) hungry ->left.acquire->right.acquire ->eat ->left.release->right.release ->think->phil when (I%2==1) hungry ->right.acquire->left.acquire ->eat ->left.release->right.release ->think->phil ).

Deadlock in Real Systems Important deadlock prevention heuristics from this lecture Ordered acquisition of different resources types Asymmetric acquisition of same resource types Both of these heuristics apply to real world systems, but following them alone will not always lead to deadlock free systems Sometimes wait-for cycles result from unexpected dependencies and circumstances of the system and environment's implementation Requires testing and diligence