Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.

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 ©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.
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 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 8: Deadlocks System Model Deadlock Characterization
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.
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 7: 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.
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
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Dr. Kalpakis CMSC 421, Operating Systems Deadlocks.
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.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
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 Objectives To develop a description of deadlocks, which.
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.
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 - 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.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Process Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Outline Deadlocks, dead lock prevention, avoidance.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Presentation transcript:

Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2Ku-Yaw ChangChapter 8 Deadlocks In a multiprogramming environment Several processes may compete for a finite number of resources Several processes may compete for a finite number of resources A process requests resources If no available, the process enter a wait state. If no available, the process enter a wait state. Waiting process may never again change state Waiting process may never again change state Such a situation is called a deadlock A law passed by the Kansas legislature When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone. When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.

3Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

4Ku-Yaw ChangChapter 8 Deadlocks 8.1 System Model A process may utilize a resource in only the following sequence Request Request If this request cannot be granted immediately, the requesting process must wait. Use Use Release ReleaseExamples open and close file system calls open and close file system calls allocate and free memory system calls allocate and free memory system calls

5Ku-Yaw ChangChapter 8 Deadlocks Deadlock State A set of processes is in a deadlock state when every process in the set is waiting for an event that can be caused only by another process in the set. Resources Physical resources Physical resources printers, tape drives, memory space, CPU Logical resources Logical resources files, semaphores, monitors Other types of events Other types of events IPC facilities

6Ku-Yaw ChangChapter 8 Deadlocks Deadlock Examples Example System has 2 tape drives. System has 2 tape drives. P 1 and P 2 each hold one tape drive and each needs another one. P 1 and P 2 each hold one tape drive and each needs another one.Example semaphores A and B, initialized to 1 semaphores A and B, initialized to 1 P 0 P 1 P 0 P 1 wait (A);wait (B); wait (B);wait (A);

7Ku-Yaw ChangChapter 8 Deadlocks 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.

8Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

9Ku-Yaw ChangChapter 8 Deadlocks Necessary Conditions A deadlock situation arise if the following four conditions hold simultaneously Mutual exclusion Mutual exclusion At least one resource must be held in a nonsharable mode Hold and wait Hold and wait Hold at least one resource and wait to acquire additional resources that are currently being held by other processes No preemption No preemption Resources cannot be preempted Circular wait Circular wait P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, P n–1 is waiting for a resource that is held by P n, and P n is waiting for a resource that is held by P 0.

10Ku-Yaw ChangChapter 8 Deadlocks Resource-Allocation Graph System resource-allocation graph A directed graph for describing deadlocks A directed graph for describing deadlocks A set of vertices V Active processes Active processes P = {P 1, P 2, …, P n } Resource types Resource types R = {R 1, R 2, …, R n } A set of edges E A request edge A request edge P i -> R j An assignment edge An assignment edge R j -> P i PiPi PiPi RjRj RjRj 4 instances

11Ku-Yaw ChangChapter 8 Deadlocks Resource Allocation Graph Sets P, R, and E P = {P 1, P 2, P 3 } R = {R 1, R 2, R 3, R 4 } E = {P 1 ->R 1, P 2 ->R 3, R 1 ->P 2, R 2 ->P 2, R 2 ->P 1, R 3 ->P 3 } Resource instances R 1 : one instance R 2 : two instances R 3 : one instance R 4 : three instances

12Ku-Yaw ChangChapter 8 Deadlocks Resource Allocation Graph If the graph contains no cycles No process in the system is deadlocked No process in the system is deadlocked If the graph does contain a cycle A deadlock may exist A deadlock may exist

13Ku-Yaw ChangChapter 8 Deadlocks Resource Allocation Graph P 3 requests an instance of R 2 Two minimal cycles exist P 1 -> R 1 -> P 2 -> R 3 -> P 3 -> R 2 -> P 1 P 2 -> R 3 -> P 3 -> R 2 -> P 2 Processes P 1, P 2, and P 3 are deadlocked

14Ku-Yaw ChangChapter 8 Deadlocks Resource Allocation Graph A cycle exists P 1 -> R 1 -> P 3 -> R 2 -> P 1 No deadlock P 4 may release its instance of R 2 That resource can the be allocated to P 3, breaking the cycle

15Ku-Yaw ChangChapter 8 Deadlocks Deadlock Characterization In summary, if a resource-allocation graph Does not have a cycle Does not have a cycle The system is not in a deadlock state Have a cycle Have a cycle The system may or may not be in a deadlock state

16Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

17Ku-Yaw ChangChapter 8 Deadlocks 8.3 Methods for Handling Deadlocks Deal with the deadlock problem in one of three ways Use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock state Use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock state Allow the system to enter a deadlock state, detect it, and recover. Allow the system to enter a deadlock state, detect it, and recover. Ignore the problem altogether, and pretend that deadlocks never occur in the system Ignore the problem altogether, and pretend that deadlocks never occur in the system Deadlocks occurs infrequently (say, once per year)

18Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

19Ku-Yaw ChangChapter 8 Deadlocks 8.4 Deadlock Prevention Ensure at least one of the four necessary conditions cannot hold Mutual Exclusion Mutual Exclusion Hold and Wait Hold and Wait No Preemption No Preemption Circular Wait Circular Wait

20Ku-Yaw ChangChapter 8 Deadlocks Deadlock Prevention Mutual Exclusion Must hold for nonsharable resources ( ex. a printer) Must hold for nonsharable resources ( ex. a printer) We cannot prevent deadlocks by denying the mutual- exclusion condition We cannot prevent deadlocks by denying the mutual- exclusion condition Some resources are intrinsically nonsharable Hold and Wait Whenever a process requests a resource, it does not hold any other resources Whenever a process requests a resource, it does not hold any other resources Request and be allocated all resources before execution Request resources only when the process has none Main disadvantages Main disadvantages Low resource utilization Starvation is possible

21Ku-Yaw ChangChapter 8 Deadlocks Deadlock Prevention No Preemption If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. Preempted resources are added to the list of resources for which the process is waiting. Preempted resources are added to the list of resources for which the process is waiting. Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Applied to resources whose state can be easily saved and restored Applied to resources whose state can be easily saved and restored Such as CPU registers and memory spaces No applied to printers and tape drives

22Ku-Yaw ChangChapter 8 Deadlocks Deadlock Prevention Circular Wait Impose a total ordering of all resource types Impose a total ordering of all resource types A one-to-one function F: R -> N N is the set of natural numbers N is the set of natural numbers require that each process requests resources in an increasing order of enumeration require that each process requests resources in an increasing order of enumeration Initially request any number of instances of a resource type R i Request instances of resource type R j if and only if F(R j )>F(R i ) Example Example F(tape drive) = 1 F(disk drive) = 5 F(printer) = 12

23Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

24Ku-Yaw ChangChapter 8 Deadlocks 8.5 Deadlock Avoidance Possible side effects of preventing deadlocks Low device utilization Low device utilization Reduced system throughput Reduced system throughput Avoid deadlock Require additional information about how resources are to be requested Require additional information about how resources are to be requested Simplest and most useful model Simplest and most useful model Each process declare the maximum number of resources of each type that it may need Possible to ensure that the system will never enter a deadlock state

25Ku-Yaw ChangChapter 8 Deadlocks Safe State If the system can allocate resources to each process in some order and still avoid a deadlock A safe sequence A safe sequence for each P i, the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j, with j < i. for each P i, the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j, with j < i. If P i resource needs are not immediately available, then P i can wait until all P j have finished. If P i resource needs are not immediately available, then P i can wait until all P j have finished. When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate. When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate. When P i terminates, P i+1 can obtain its needed resources, and so on. When P i terminates, P i+1 can obtain its needed resources, and so on. Unsafe : no such sequence exists

26Ku-Yaw ChangChapter 8 Deadlocks Basic Facts If a system is in safe state no deadlocks no deadlocks If a system is in unsafe state possibility of deadlock. possibility of deadlock.Avoidance ensure that a system will never enter an unsafe state. ensure that a system will never enter an unsafe state.

27Ku-Yaw ChangChapter 8 Deadlocks An Example A system with 12 magnetic tape drives and 3 processes: P 0, P 1, and P 2 The sequence satisfies the safety condition. The sequence satisfies the safety condition. Maximum Needs Current Needs P0P0P0P0105 P1P1P1P142 P2P2P2P292

28Ku-Yaw ChangChapter 8 Deadlocks An Example Process P 2 requests and is allocated 1 more tape drive The system is no longer in a safe state. The system is no longer in a safe state. If a process requests a resource that is currently available, it may still have to wait. Maximum Needs Current Needs P0P0P0P0105 P1P1P1P142 P2P2P2P293

29Ku-Yaw ChangChapter 8 Deadlocks Resource-Allocation Graph Algorithm Resource-allocation graph with only one instance of each resource type Used for deadlock avoidance Used for deadlock avoidance Claim edge P i -> R j (represented by a dashed line) Process P j may request resource R j Process P j may request resource R j Converts to request edge when a process requests a resource. Converts to request edge when a process requests a resource. When a resource is released by a process, assignment edge reconverts to a claim edge. When a resource is released by a process, assignment edge reconverts to a claim edge. Resources must be claimed a priori in the system. Resources must be claimed a priori in the system.

30Ku-Yaw ChangChapter 8 Deadlocks Resource-Allocation Graph For Deadlock Avoidance

31Ku-Yaw ChangChapter 8 Deadlocks Unsafe State In Resource-Allocation Graph

32Ku-Yaw ChangChapter 8 Deadlocks Banker’s Algorithm Multiple instances resource-allocation graph allocation is not applicable resource-allocation graph allocation is not applicable A new process must declare the maximum number of instances of each resource type that it may need. Cannot exceed the total number of resources in the system Cannot exceed the total number of resources in the system Determine whether the allocation of these resources will leave the system in a safe state If yes, the resources are allocated If yes, the resources are allocated If not, the process must wait until some other process releases enough resources If not, the process must wait until some other process releases enough resources

33Ku-Yaw ChangChapter 8 Deadlocks Data Structures for the Banker’s Algorithm Let n = number of processes, and m = number of resources types. 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 ]. Need [ i, j ] = Max[ i, j ] – Allocation [ i, j ].

34Ku-Yaw ChangChapter 8 Deadlocks Notation for the Banker’s Algorithm Let X and Y be vectors of length n. X ≤ Y if and only if X[ i ] ≤ Y[ i ] for all i =1,2, …, n X ≤ Y if and only if X[ i ] ≤ Y[ i ] for all i =1,2, …, n For example if X = (1, 7, 3, 2) and Y = (0, 3, 2, 1), then Y ≤ X if X = (1, 7, 3, 2) and Y = (0, 3, 2, 1), then Y ≤ X

35Ku-Yaw ChangChapter 8 Deadlocks Safety Algorithm Find out whether or not the system is in a safe state 1.Let Work and Finish be vectors of length m and n, respectively. Initialize: Work := Available Finish [ i ] := false for i = 1, 2, 3, …, 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.

36Ku-Yaw ChangChapter 8 Deadlocks Resource-Request Algorithm Request i [ j ] = k Process P i wants k instances of resource type R j. Process P i wants k instances of resource type R j. 1.If Request i ≤ Need i, go to step 2. Otherwise, raise an error condition, since the process has exceeded its maximum claim. 2.If Request i ≤ Available, go to step 3. Otherwise P i must wait, since the 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 safe -> the resources are allocated to P i. If unsafe -> P i must wait, and the old resource-allocation state is restored. If unsafe -> P i must wait, and the old resource-allocation state is restored.

37Ku-Yaw ChangChapter 8 Deadlocks An Illustrative Example Five processes P 0 through P 4 P 0 through P 4 Snapshot at time T 0 : Allocation M a x Available Need A B CA B C A B CA B C P P P P P satisfies the safety criteria. satisfies the safety criteria. Three resource types A : 10 instances A : 10 instances B : 5 instances B : 5 instances C : 7 instances C : 7 instances

38Ku-Yaw ChangChapter 8 Deadlocks An Illustrative Example P 1 requests One instance of resource type A One instance of resource type A Two instances of resource type C Two instances of resource type C Request 1 = (1, 0, 2) ≤ Available (3, 3, 2) Request 1 = (1, 0, 2) ≤ Available (3, 3, 2) Snapshot at time T 0 : AllocationNeed Available A B CA B C A B C P P P P P satisfies the safety criteria. satisfies the safety criteria. Grant the request of process P 1. Grant the request of process P 1.

39Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

40Ku-Yaw ChangChapter 8 Deadlocks 8.6 Deadlock Detection Allow the system to enter deadlock state An algorithm that examines the state of the system to determine whether a deadlock has occurred An algorithm that examines the state of the system to determine whether a deadlock has occurred An algorithm to recover from the deadlock An algorithm to recover from the deadlock A detection-and-recovery scheme requires overhead Run-time costs Run-time costs Potential losses Potential losses

41Ku-Yaw ChangChapter 8 Deadlocks Single Instance of Each Resource Type A wait-for graph : obtained from resource-allocation graph Remove the nodes of type resource Remove the nodes of type resource Collapse the appropriate edges Collapse the appropriate edges

42Ku-Yaw ChangChapter 8 Deadlocks Single Instance of Each Resource Type A deadlock exists in the system if and only if the wait-for graph contains a cycle. To detect deadlocks Maintain the wait-for graph Maintain the wait-for graph Periodically invoke an algorithm that searches for a cycle in the graph Periodically invoke an algorithm that searches for a cycle in the graph

43Ku-Yaw ChangChapter 8 Deadlocks Several Instances of a Resource Type The algorithm employs several time-varying data structures Available Available A vector of length m indicates the number of available resources of each type. Allocation Allocation An n x m matrix defines the number of resources of each type currently allocated to each process. Request 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.

44Ku-Yaw ChangChapter 8 Deadlocks 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.

45Ku-Yaw ChangChapter 8 Deadlocks 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.

46Ku-Yaw ChangChapter 8 Deadlocks 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 : AllocationRequestAvailable A B C A B C A B C P P P P P Sequence will result in Finish[i] = true for all i.

47Ku-Yaw ChangChapter 8 Deadlocks Example (Cont.) P 2 requests an additional instance of type C. Request A B C P P P P P P P P P P State of system? Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests. 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. Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4.

48Ku-Yaw ChangChapter 8 Deadlocks Detection-Algorithm Usage When, and how often, to invoke depends on: How often a deadlock is likely to occur? How often a deadlock is likely to occur? How many processes will need to be rolled back? How many processes will need to be rolled back? one for each disjoint cycle The detection algorithm can be invoked When a request cannot be granted immediately When a request cannot be granted immediately Identify the specific process that caused the deadlock At arbitrary points in time At arbitrary points in time Not be able to tell which of the many deadlocked processes “caused” the deadlock

49Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

50Ku-Yaw ChangChapter 8 Deadlocks Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. In which order should we choose to abort? Priority of the process. Priority of the process. How long process has computed, and how much longer to completion. How long process has computed, and how much longer to completion. Resources the process has used. Resources the process has used. Resources process needs to complete. Resources process needs to complete. How many processes will need to be terminated. How many processes will need to be terminated. Is process interactive or batch? Is process interactive or batch?

51Ku-Yaw ChangChapter 8 Deadlocks Resource Preemption Three issues Selecting a victim Selecting a victim To minimize cost Number of resources Number of resources Amount of time Amount of time Rollback Rollback Return to some safe state, restart process for that state The simplest solution is a total rollback Starvation Starvation Same process may always be picked as victim, include number of rollback in cost factor Include the number of rollbacks in the cost factor Include the number of rollbacks in the cost factor

52Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

53Ku-Yaw ChangChapter 8 Deadlocks Summary P.266

54Ku-Yaw ChangChapter 8 Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock 8.Summary 9.Exercises

55Ku-Yaw ChangChapter 8 Deadlocks Exercises

The End