Deadlock Questions answered in this lecture: What are the four necessary conditions for deadlock? How can deadlock be prevented? How can deadlock be avoided?

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks.
Deadlock Problem Deadlock: A set of processes, each holding a resource, and each waiting to acquire a resource held by another process in the set. Example:
Deadlocks CS 3100 Deadlocks1. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
1 Wednesday, June 28, 2006 Command, n.: Statement presented by a human and accepted by a computer in such a manner as to make the human feel that he is.
Deadlocks. 2 System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: 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.
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Objectives Understand the Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention, Avoidance, and Detection Recovering from Deadlock Combined Approach.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Operating Systems Part III: Process Management (Deadlocks)
Cosc 4740 Chapter 6, Part 4 Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-6 Deadlocks Department of Computer Science and Software Engineering.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
Deadlocks Silberschatz Ch. 7 and Priority Inversion Problems.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 28 Handling Deadlock.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Dr. Kalpakis CMSC 421, Operating Systems Deadlocks.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Chapter 7 Deadlocks Page 2 Chapter 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Deadlocks System Model RAG Deadlock Characterization
Chapter 7 Deadlocks Chapter 7: Deadlocks 7.1 System Model 7.2 Deadlock Characterization 7.3 Methods for Handling Deadlocks 7.4 Deadlock Prevention 7.5.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks System Model Deadlock Characterization Methods.
Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück.
Mi-Jung Choi Dept. of Computer and Science Silberschatz, Galvin and Gagne ©2006 Operating System Principles Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
Chapter 7: Deadlocks. 7.2CSCI 380 – Operating Systems Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Deadlock. Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
7.1 CSE Department MAITSandeep Tayal 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
CSE Operating System Principles Deadlocks. CSE – Operating System Principles2 Overview System Model Deadlock Characterization Methods for.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Deadlock. Examples You can't get a job without experience; you can't get experience without a job. A set of blocked processes each holding a resource.
Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Process Management Deadlocks.
Chapter 7: Deadlocks Source & Copyright: Operating System Concepts, Silberschatz, Galvin and Gagne.
Operating System: DEADLOCKS
Chapter 7 Deadlocks.
Deadlocks Session - 13.
Chapter 8: Deadlocks Deadlock Characterization
Presentation transcript:

Deadlock Questions answered in this lecture: What are the four necessary conditions for deadlock? How can deadlock be prevented? How can deadlock be avoided? How can deadlock be detected and recovered from? UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department CS 537 Introduction to Operating Systems Andrea C. Arpaci-Dusseau Remzi H. Arpaci-Dusseau

Deadlock: Why does it happen? Informal: Every entity is waiting for resource held by another entity; none release until it gets what it is waiting for

Deadlock Example Two threads access two shared variables, A and B Variable A is protected by lock x, variable B by lock y How to add lock and unlock statements? int A, B; Thread 1 A += 10; B += 20; A += B; A += 30; Thread 2 B += 10; A += 20; A += B; B += 30;

Deadlock Example int A, B; lock_t x, y; Thread 1 lock(x); A += 10; lock(y); B += 20; A += B; unlock(y); A += 30; unlock(x); Thread 2 lock(y); B += 10; lock(x); A += 20; A += B; unlock(x); B += 30; unlock(y); What can go wrong??

Representing Deadlock Two common ways of representing deadlock Vertices: –Threads (or processes) in system –Resources (anything of value, including locks and semaphores) Edges: Indicate thread is waiting for the other T1T2 “waiting for” Wait-For GraphResource-Allocation Graph T1T2 wants held by y x wants held by

Conditions for Deadlock Mutual exclusion Resource can not be shared Requests are delayed until resource is released Hold-and-wait Thread holds one resource while waits for another No preemption Resources are released voluntarily after completion Circular wait Circular dependencies exist in “waits-for” or “resource-allocation” graphs ALL four conditions MUST hold

Handing Deadlock Deadlock prevention Ensure deadlock does not happen Ensure at least one of 4 conditions does not occur Deadlock avoidance Ensure deadlock does not happen Use information about resource requests to dynamically avoid unsafe situations Deadlock detection and recovery Allow deadlocks, but detect when occur Recover and continue Ignore Easiest and most common approach

Deadlock Prevention #1 Approach Ensure 1 of 4 conditions cannot occur Negate each of the 4 conditions No single approach is appropriate (or possible) for all circumstances No mutual exclusion --> Make resource sharable Example: Read-only files

Deadlock Prevention #2 No Hold-and-wait --> Two possibilities 1) Only request resources when have none Release resource before requesting next one Thread 1 lock(x); A += 10; unlock(x); lock(y); B += 20; unlock(y); lock(x); A += 30; unlock(x); Thread 2 lock(y); B += 10; unlock(y); lock(x); A += 20; unlock(x); lock(y); B += 30; unlock(y);

Deadlock Prevention #2 No Hold-and-wait 2) Atomically acquire all resources at once Example #1: Single lock to protect all Thread 1 lock(z); A += 10; B += 20; A += B; A += 30; unlock(z); Thread 2 lock(z); B += 10; A += 20; A += B; B += 30; unlock(z);

Deadlock Prevention #2 No Hold-and-wait 2) Atomically acquire all resources at once Example #2: New primitive to acquire two locks Thread 1 lock(x,y); A += 10; B += 20; A += B; unlock(y); A += 30; unlock(x); Thread 2 lock(x,y); B += 10; A += 20; A += B; unlock(x); B += 30; unlock(y);

Deadlock Prevention #2 Problems w/ acquiring many resources atomically Low resource utilization –Must make pessimistic assumptions about resource usage if (cond1) { lock(x); } if (cond2) { lock(y); } Starvation –If need many resources, others might keep getting one of them

Deadlock Prevention #3 No “no preemption” --> Preempt resources Example: A waiting for something held by B, then take resource away from B and give to A Only works for some resources (e.g., CPU and memory) Not possible if resource cannot be saved and restored –Can’t take away a lock without causing problems

Deadlock Prevention #4 No circular wait --> Impose ordering on resources Give all resources a ranking; must acquire highest ranked first How to change Example? Problems?

Deadlock Avoidance Dijkstra’s Banker’s Algorithm Avoid unsafe states of processes holding resources Unsafe states might lead to deadlock if processes make certain future requests When process requests resource, only give if doesn’t cause unsafe state Problem: Requires processes to specify all possible future resource demands

Banker’s Algorithm Example Scenario: Three processes, P0, P1, and P2 Five available resources, N=5 Each process may need maximum of 4 resources simultaneously Not safe example: P0 has 2, P1 has 1, P2 has 1 Why could this lead to deadlock? Implication: Avoid this state, allow only states with enough resources left to satisfy claim of at least 1 process Claim: Maximum need - currently loaned to this process Example: P0 requests: Allow? P1 requests: Allow? P2 requests: Allow? P0 requests: Allow? P1 requests: Allow? P0 requests: Allow? P0 releases 2 Allow any others now?

Deadlock Detection and Recovery Detection Maintain wait-for graph of requests Run algorithm looking for cycles –When should algorithm be run? Recovery: Terminate deadlock Reboot system (Abort all processes) Abort all deadlocked processes Abort one process in cycle Challenges How to take resource away from process? Undo effects of process (e.g., removing money from account) –Must roll-back state to safe state (checkpoint memory of job) Could starve process if repeatedly abort it