Deadlock CSE451 Andrew Whitaker. Review: What Can Go Wrong With Threads? Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks Adapted by Donghui Zhang from the original version by Silberschatz et al.
Advertisements

ICS Principles of Operating Systems Lectures 8 and 9 - Deadlocks Prof. Dmitri V. Kalashnikov dvk ics.uci.edu Slides © Prof. Nalini Venkatasubramanian.
Deadlock and Starvation
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Deadlocks  (How to Detect Them and Avoid Them) A:
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
Modified from Silberschatz, Galvin and Gagne Lecture 13 Chapter 7: Deadlocks.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
Chapter 7.1: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Bridge Crossing Example Traffic only in one direction. Each section.
Silberschatz, Galvin and Gagne  Operating System Concepts Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for.
CSE 451: Operating Systems Winter 2011 Deadlock Mark Zbikowski Gary Kimura.
CSE 451: Operating Systems Spring 2013 Module 9 Deadlock Ed Lazowska Allen Center 570 © 2013 Gribble, Lazowska, Levy, Zahorjan.
IT 344: Operating Systems Winter 2010 Module 8 Deadlock Chia-Chi Teng CTB 265.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7 – Deadlock (Pgs 283 – 306). Overview  When a set of processes is prevented from completing because each is preventing the other from accessing.
Operating Systems Part III: Process Management (Deadlocks)
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
1 Deadlock. 2 Concurrency Issues Past lectures:  Problem: Safely coordinate access to shared resource  Solutions:  Use semaphores, monitors, locks,
This Time - Deadlock Definition Conditions for deadlocks
Resource Management Reusable  Disk blocks  File descriptors  Semaphores Consumable  Messages  Packets.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
CS333 Intro to Operating Systems Jonathan Walpole.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Deadlock. Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
IT 344: Operating Systems Module 8 Deadlock Chia-Chi Teng CTB 265.
Deadlock. Stepping on each other’s feet - I Thread T 1 b1:= allocate(); b2:= allocate(); …… release(b1); release(b2); Thread T 2 b1:= allocate(); b2:=
Outline Objective for today: Formal treatment of deadlock.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Deadlocks References –text: Tanenbaum ch.3. Deadly Embrace Deadlock definition –A set of process is dead locked if each process in the set is waiting.
Deadlock Operating Systems (CC2011NI) -Mr. Pranoy Man Pradhan.
CSE 451: Operating Systems Spring 2010 Module 9 Deadlock John Zahorjan Allen Center 534.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Process Management Deadlocks.
CSE 120 Principles of Operating
Concurrency: Deadlock and Starvation
CSE 451: Operating Systems Winter 2014 Module 9 Deadlock
CSE 451: Operating Systems Winter 2012 Deadlock
Chapter 7: Deadlocks Source & Copyright: Operating System Concepts, Silberschatz, Galvin and Gagne.
Lecture 19: Deadlock: Conditions, Detection and Avoidance
Deadlock, Illustrated.
Chapter 7: Deadlocks.
CSE 451: Operating Systems Winter 2007 Module 8 Deadlock
CSE 451: Operating Systems Spring 2006 Module 8 Deadlock
Deadlocks Definition A set of processes is in a Deadlock state when every process in the set is waiting for an event that can only be caused by another.
CS 333 Introduction to Operating Systems Class 7 - Deadlock
DPNM Lab. Dept. of CSE, POSTECH
CSE 451: Operating Systems Autumn 2009 Module 9 Deadlock
CSE 451: Operating Systems Spring 2005 Module 8 Deadlock
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
DEADLOCK.
CSE 451: Operating Systems Autumn 2010 Module 9 Deadlock
Lecture 19: Deadlock: Conditions, Detection and Avoidance
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
CSE 451: Operating Systems Winter 2007 Module 8 Deadlock
CSE 451: Operating Systems Spring 2008 Module 9 Deadlock
Introduction to Deadlocks
Deadlocks.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
CSE 451: Operating Systems Winter 2004 Module 8 Deadlock
CSE 451: Operating Systems Winter 2006 Module 8 Deadlock
Presentation transcript:

Deadlock CSE451 Andrew Whitaker

Review: What Can Go Wrong With Threads? Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never does the right thing” Performance hazards  Program is too slow due to excessive synchronization

A Liveness Hazard: Starvation A thread is ready, but the scheduler does not select it to run  Can be caused by shortest-job-first scheduling

Deadlock Each thread is waiting on a resource held by another thread  So, there is no way to make progress Thread 1 Thread 2Thread 3

Deadlock, Illustrated

Necessary Conditions for Deadlock Mutual exclusion  Resources cannot be shared e.g., buffers, locks Hold and wait (subset property)  A thread must hold a subset of its resource needs And, the thread is waiting for more resources No preemption  Resources cannot be taken away Circular wait  A needs a resource that B has  B has a resource that A has

Resource Allocation Graph with a Deadlock Silberschatz, Galvin and Gagne  2002 P => R: request edge R => P: assignment edge

Cycle Without Deadlock P => R: request edge R => P: assignment edge Silberschatz, Galvin and Gagne  2002 Why is there no deadlock here?

Graph Reduction A graph can be reduced by a thread if all of that thread’s requests can be granted  Eventually, all resources by the reduced thread will be freed Miscellaneous theorems (Holt, Havender):  There are no deadlocked threads iff the graph is completely reducible  The order of reductions is irrelevant (Detail: resources with multiple units)

Approaches to Deadlock 1.Avoid threads 2.Deadlock prevention  Break up one of the four necessary conditions 3.Deadlock avoidance  Stay live even in the presence of the four conditions 4.Detect and recover

Approach #1: Avoid Threads Brain dead solution: avoid deadlock by avoiding threads Example: GUI frameworks  Typically use a single event-dispatch thread Operating System GUI Framework Application mouse click change background color

Approach #2: Prevention Can we eliminate: Mutual exclusion? Hold and wait (subset property)? No Preemption? Circular waiting?

Lock Ordering We can avoid circular waiting by acquiring resources in a fixed order Example: Linux rename operation  Each open file has a semaphore  Both semaphores must be held during file operations  Linux always uses the same order for down operations Semaphore at the lowest memory address goes first

Rename Example Process 1: rename (“foo.txt”,”bar.txt”); Process 2: rename (“bar.txt”,”foo.txt”); down() foo semaphore down() bar semaphore up() bar semaphore up() foo semaphore down() foo semaphore down() bar semaphore up() bar semaphore up() foo semaphore

Approach #3: Avoidance Intuition: the four conditions do not always lead to deadlock  “necessary but not sufficient” We can stay live if we know the resource needs of the processes

Bankers Algorithm Overview Basic idea: ensure that we always have an “escape route”  The resource graph is reducible This can be enforced with the bankers algorithm:  When a request is made Pretend you granted it Pretend all other legal requests were made  Can the graph be reduced? If so, allocate the requested resource If not, block the thread

Deadlock Avoidance in Practice Static provisioning  Ensure that each active process/thread can acquire some minimum set of resources  Use admission control to maintain this invariant This is closely related to the idea of thrashing  Each thread needs some minimum amount of resources to run efficiently

Approach #4: Detection and Recovery Not commonly used  Detection is expensive  Recovery is tricky Possible exception: databases

Deadlock Detection Detection algorithm (sketch)  Keep track of who is waiting for resources  Keep track of who holds resources  Assume that all runnable processes release all their resources Does this unblock a waiting process? If yes, release that process’s resources  If processes are still blocked, we are deadlocked

Recovery Must “back out” of a resource allocation decision  By preemption or by killing the process This is particularly problematic for lock- based deadlocks  System can be in an inconsistent state