1 Deadlock.  System Model  Process must request a resource before using  Process must release the resource when done  Deadlock  A set of processes.

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.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 12: Deadlocks (Chapter 7)
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Lecture 7: Deadlocks, Deadlock Risk Management. Lecture 7 / Page 2AE4B33OSS Silberschatz, Galvin and Gagne ©2005 Contents The Concept of Deadlock Resource-Allocation.
Chapter 6 Concurrency: Deadlock and Starvation
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.
Chapter 7. Deadlocks.
1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
03/07/2007CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
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.
03/03/2004CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
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.
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention, Avoidance, and Detection Recovering from Deadlock Combined Approach.
1 Deadlock Definition of deadlock Condition for its occurrence Solutions for avoiding and breaking deadlock –Deadlock Prevention –Deadlock Avoidance –Deadlock.
Chapter 7 Deadlocks. 7.2 Modified By Dr. Khaled Wassif Operating System Concepts – 7 th Edition Silberschatz, Galvin and Gagne ©2005 Chapter 7: 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.
Chapter 7: Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process.
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.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Dr. Kalpakis CMSC 421, Operating Systems Deadlocks.
Deadlocks System Model RAG Deadlock Characterization
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks System Model Deadlock Characterization Methods.
Lecture 12 Handling Deadlock – Prevention, avoidance and detection.
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.
Operating Systems Unit VI Deadlocks and Protection Department of Computer Science Engineering and Information Technology.
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.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
國立台灣大學 資訊工程學系 Chapter 7: Deadlocks. 資工系網媒所 NEWS 實驗室 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes.
Chap 7 Deadlocks. Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks To.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
Deadlocks Introduction to Operating Systems: Module 7.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 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.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
CSE Operating System Principles Deadlocks. CSE – Operating System Principles2 Overview System Model Deadlock Characterization Methods for.
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
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Process Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Outline Deadlocks, dead lock prevention, avoidance.
Deadlock Prevention Restrain the ways request can be made.
Deadlocks Session - 14.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Presentation transcript:

1 Deadlock

 System Model  Process must request a resource before using  Process must release the resource when done  Deadlock  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 process in the set.

Bridge Crossing Example  Traffic only in one direction.  Each section of a bridge can be viewed as a resource.  If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback).  Several cars may have to be backed up if a deadlock occurs.  Starvation is possible.

Deadlock Characterization  Necessary but not sufficient conditions  Mutual exclusion  Hold and wait  No preemption  Required condition  Circular wait - a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain  Unresolvable circular wait is the definition of deadlock!  All four conditions must hold for deadlock

Circular Wait Resource B Resource A Process P1 Requests Process P2 Held by Requests Held By

Describing Deadlock  Deadlocks can be described using resource allocation graph  Vertices  Active processes{P1, P2, … }  Resources{R1, R2, … }  Edges  A directed edge from Pi to Rj  Process Pi requested an instance of resource Rj  A directed edge from Rj to Pi  Resource Rj has been allocated to process Pi  Process are circles, Resources are rectangles

Resource Allocation Graph P1P2P3 R1R2 R4 R3

So?  If a graph contains no cycles, then no process in the system is deadlocked  If the graph contains a cycle, deadlock MAY exist  If each resource has only one instance, then a cycle implies deadlock

Deadlock? P1P2P3 R1R2 R4 R3 Is there a cycle? Is there deadlock?

Deadlock  Reusable Resources  Can only be used by one process at a time. After use, can be reassigned to another process (printer, memory, files, etc.)  Deadlock can occur with two processes copying from disk to tape  Deadlock can occur with memory allocation when there is 200K available, both processes want 80K, then make second request for 70K  Consumable Resources  Can be created or destroyed (signals, messages)  No fixed limit on # of resources  Can deadlock if both waiting for a message from the other

Examples of Deadlock  Deadlock occurs if receive blocks … u 200K bytes is available for allocation… Process 1Process 2 Request 80K bytes… …Request 70K bytes Request 60K bytes… …Request 80K bytes Process 1Process 2 Receive(P2)… …Receive(P1) Send(P2)… …Send(P1)

Handling Deadlock  Deadlock Prevention  Eliminate one of the 4 conditions  Deadlock Avoidance  Deadlock possible  Make appropriate dynamic choices based on current state  Deadlock Detection  Take some action to recover

Deadlock Prevention  Can prevent deadlock by forbidding one of the 4 conditions  Forbid Mutual Exclusion  Generally not reasonable  Multiple read-only access but only exclusive access for writes  Forbid Hold and Wait  Ask for all resources at one time  May block a process for a long time when it can be making progress  Wait for resources it doesn ’ t yet need  May tie up unneeded resources  May hold resources that are not needed immediately  May not know what to request  Especially true for interactive processes  May request resources because they are needed in some instances, but not always

Deadlock Prevention (continued … )  Forbid No Preemption  Take resources away from waiting processes (require it to be released)  Only feasible if state can be saved  Examples: CPU, Memory  Not usable for Files, Printer, etc.  Forbid Circular Wait  Define a linear order on items  If it needs resources 3, 15, 6, 9, and 4 then must request in the order 3, 4, 6, 9, 15  Cannot have circular wait because a process cannot have 9 and request 5  May not be easy to define the order (files)  May force an awkward order of requesting resources  May have to request resource 3 in order to request 4, even when 4 is needed now but 3 is not needed until much later

Deadlock Avoidance  Allow general requests, but make choices to avoid deadlock  Assume we know the maximum requests (claims) for each process  Process must state it needs a max of 5 A objects, 3 B objects, 2 C objects.  Do not need to use its max claims  Ok to set max=5 and only use 3  Can make requests at any time and in any order  Process Initiation Denial  Track current allocations  Assume all processes may make maximum requests at the same time  Only start process if it can ’ t result in deadlock regardless of allocations

Resource Allocation Denial  Safe State – We can finish all processes by some scheduling sequence  Example: Finish P1, P4, P2, P5, P3  Banker ’ s Algorithm (Dijkstra)  Reject a request if it exceeds the processes ’ declared maximum claims  Grant a request if the new state would be safe  Determining if a state is safe  Find any process P i for which we can meet it ’ s maximum requests  Don't forget already allocated resources  Mark P i as “ done ”, add its resources to available resource pool  State is safe if we can mark all processes as “ done ”  Block a process if the resources are not currently available or the new state is not safe

Avoidance Example  Are we deadlocked? ABC 936 ABC 112 ABC P1P1 322 P2P2 613 P3P3 314 P4P4 422 Claim 200 P4P4 112 P3P3 115 P2P2 P1P1 001 CBA Allocation Available Resource 024 P4P4 301 P3P3 201 P2P2 P1P1 222 CBA C - A u P 1 wants 1 more A and 1 more C. Is it ok (safe state)? 200 P4P4 112 P3P3 115 P2P2 P1P1 001 CBA Allocation u Are we in a safe state?

Deadlock Detection  Avoidance methods tend to limit access to resources  Instead, grant arbitrary requests and note when deadlock happens  Can vary how often we check  Early detection vs. overhead of checks  Algorithm  Preparation:  Create table of process requests, current allocations  Note available resources  Mark processes with no resources  Mark any process whose requests can be met (requests  available resources)  Include resources from that process as ‘ available ’ (this process can finish)  If multiple processes available, pick any  If any processes cannot be marked, they are part of a deadlock

Banker’s Algorithm  Multiple instances.  Each process must a priori claim maximum use.  When a process requests a resource it may have to wait.  When a process gets all its resources it must return them in a finite amount of time.

Data Structures for the Banker’s Algorithm  Available: Vector of length m. If available [j] = k, there are k instances of resource type R j available.  Max: n x m matrix. If Max [i,j] = k, then process P i may request at most k instances of resource type R j.  Allocation: n x m matrix. If Allocation[i,j] = k then P i is currently allocated k instances of R j.  Need: n x m matrix. If Need[i,j] = k, then P i may need k more instances of R j to complete its task. Need [i,j] = Max[i,j] – Allocation [i,j]. m = number of resources types. n = number of processes.

Safety Algorithm 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 1,2, …, n. 2.Find an i such that both: (a) Finish [i] = false (b) Need i  Work If no such i exists, go to step 4. 3.Work = Work + Allocation i Finish[i] = true go to step 2. 4.If Finish [i] == true for all i, then the system is in a safe state.

Resource-Request Algorithm for Process P i Request = request vector for process P i. If Request i [j] = k then process P i wants k instances of resource type R j. 1.If Request i  Need i go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim. 2.If Request i  Available, go to step 3. Otherwise P i must wait, since resources are not available. 3.Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available - Request i ; Allocation i = Allocation i + Request i ; Need i = Need i – Request i;; If safe  the resources are allocated to P i. If unsafe  P i must wait, and the old resource- allocation state is restored

Example of Banker’s Algorithm  5 processes P 0 through P 4 ; 3 resource types A (10 instances), B (5instances), and C (7 instances).  Snapshot at time T 0 : Allocation Max Available A B C A B C A B C P P P P P

 The content of the matrix. Need is defined to be Max – Allocation. AllocationNeed Available A B C A B C A B C P P P P P The system is in a safe state since the sequence satisfies safety criteria.

Example P 1 Request (1,0,2) (Cont.)  Check that Request  Available (that is, (1,0,2)  (3,3,2)  true. AllocationNeedAvailable A B CA B CA B C P P P P P  Executing safety algorithm shows that sequence satisfies safety requirement.  Can request for (3,3,0) by P 4 be granted?  Can request for (0,2,0) by P 0 be granted?

Deadlock Detection  Allow system to enter deadlock state  Detection algorithm  Recovery scheme

Single Instance of Each Resource Type  Maintain wait-for graph  Nodes are processes.  P i  P j if P i is waiting for P j.  Periodically invoke an algorithm that searches for a cycle in the graph.  An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph.

Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph

Several Instances of a Resource Type  Available: A vector of length m indicates the number of available resources of each type.  Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process.  Request: An n x m matrix indicates the current request of each process. If Request [i j ] = k, then process P i is requesting k more instances of resource type. R j.

Detection Algorithm 1.Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available (b)For i = 1,2, …, n, if Allocation i  0, then Finish[i] = false;otherwise, Finish[i] = true. 2.Find an index i such that both: (a)Finish[i] == false (b)Request i  Work If no such i exists, go to step 4.

Detection Algorithm (Cont.) 3.Work = Work + Allocation i Finish[i] = true go to step 2. 4.If Finish[i] == false, for some i, 1  i  n, then the system is in deadlock state. Moreover, if Finish[i] == false, then P i is deadlocked. Algorithm requires an order of O(m x n 2) operations to detect whether the system is in deadlocked state.

Example of Detection Algorithm  Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C (6 instances).  Snapshot at time T 0 : Allocation Request Available A B C A B C A B C P P P P P  Sequence will result in Finish[i] = true for all i.

Example (Cont.)  P 2 requests an additional instance of type C. Request A B C P P P P P  State of system?  Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests.  Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4.

Detection-Algorithm Usage  When, and how often, to invoke depends on:  How often a deadlock is likely to occur?  How many processes will need to be rolled back?  If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

1 E 0 D 000 CBA Temporary Available Detection Example 1 ABCDE RequestAllocation Available Resource ABCDE P1P P2P P3P P4P ABCDE P1P P2P P3P P4P ABCDE  Mark P 4 (not holding anything someone else wants) u Mark P 3, new available: u Cannot mark P 1 or P 2, so we are deadlocked

000 CBA Temporary Available Detection Example 2  Are we deadlocked? ABC 000 ABC P1P1 000 P2P2 202 P3P3 000 P4P4 100 P5P5 002 Requests Available ABC P1P1 010 P2P2 200 P3P3 303 P4P4 211 P5P5 002 AllocationResource ABC

0 E 0 D 000 CBA Temporary Available Detection Example 3 ABCDE RequestAllocation Available Resource ABCDE P1P P2P P3P P4P P5P ABCDE P1P P2P P3P P4P P5P ABCDE u Cannot mark P 1 thru P 5, so we are deadlocked

0 E 0 D 000 CBA Temporary Available Detection Example 4 ABCDE RequestAllocation Available Resource ABCDE P1P P2P P3P P4P P5P ABCDE P1P P2P P3P P4P P5P ABCDE u Mark P 4, (nothing allocated) u Mark P , P 1, P 3, P

Deadlock Detection Questions?  Does it really work?  How often do you run it?  How often is deadlock likely to occur?  How expensive is it?

Deadlock Recovery  Several possible approaches  Abort all deadlocked processes  Simple but common  Back up processes to a previously saved checkpoint, then restart  Assumes we have checkpoints and a rollback mechanism  Runs risk of repeating deadlock  Assumes that the deadlock has enough timing dependencies it won ’ t happen  Selectively abort processes until deadlock broken  Preempt resources until deadlock broken  Must roll back process to checkpoint prior to acquiring key resource

Deadlock recovery  Process Termination  Kill them all  One at a time  Consider priority  Time computing  Who has most resources  Resource Preemption  Who gets preempted  Do you consider process rollback and starvation

Mixed Strategy  May group resources into classes, have a different deadlock strategy for each class  Swap Space  Prevent Deadlocks by requiring all space to be allocated at once  Avoidance also possible  Tapes/Files  Avoidance can be effective here  Prevention by ordering resources also possible  Main Memory  Preemption a good approach  Internal Resources (channels, etc.)  Prevention by ordering resources  Can use linear ordering between classes