Resource Allocation, Deadlock and Banker’s Algorithm 60-330 Supplementary Notes Dr. R. D. Kent Last modified: Dec. 11, 2006.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Deadlock 2.
Chapter 7: Deadlocks.
Chapter 81 Deadlock is:  A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.  Example.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks (Continuation). 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
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:
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 30 Handling Deadlock.
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 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.
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.
02/19/2008CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
CS342 Operating Systemsİbrahim Körpeoğlu, Bilkent University 1 Lecture 7 Deadlocks (chapter 7) Dr. İbrahim Körpeoğlu
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.
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.
What we will cover…  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 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 Modified.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
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.
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.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Chapter 81 Deadlock Avoidance Technique  Resource Allocation Denial: Grant incremental resource requests if we can prove that this leaves the system in.
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.
Chapter 7: Deadlock Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to during class.
Lecture 12 Handling Deadlock – Prevention, avoidance and detection.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
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.
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Deadlock A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. Example : “When.
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.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Chapter 7 Deadlocks 1. Objectives and Outline Outline The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks 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
CSSE 332 Operating Systems Rose-Hulman Institute of Technology
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Outline Deadlocks, dead lock prevention, avoidance.
Deadlock Prevention Restrain the ways request can be made.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Presentation transcript:

Resource Allocation, Deadlock and Banker’s Algorithm Supplementary Notes Dr. R. D. Kent Last modified: Dec. 11, 2006

Introduction These slides are intended to illustrate and clarify some details of deadlock detection and avoidance applied to resource allocation.  In particular, they clarify examples in the textbook by providing significant detail Assume that all resource types and the number of instances of each type are known by the O/S  Initially configured at system startup/bootstrap  Tracked along with processes as they Request resources for the first time Use resources dynamically

Assumed Knowledge (1) Configured at startup and updated regularly  All resource types and the number of instances of each type Stored in a vector called Available with M entries (one for each resource type). Available[ j ] = k ; /* implies there are k instances of resource type j */ At a given, specific time  The maximum resource demand of each of N processes Stored in a NxM matrix Max Max[ i ][ j ] = k ; /* process P i may request at most k instances of resource type R j

Assumed Knowledge (2) At a given, specific time:  The number of resources of each type currently allocated to each of N processes Stored in a NxM matrix Allocation Allocation[ i ][ j ] = k ; /* process P i is currently allocated k instances of resource type R j  The remaining resource needs of each process Stored in a NxM matrix Need Need[ i ][ j ] = k ; /* process P i may need k instances of resource type R j to complete its task NOTE: Need = Max – Allocation ;

Assumed Knowledge (3) Comments:  The contents of these data structures varies over time as the processes progress to completion through the states  Sometimes it is convenient to refer to an entire row of an NxM matrix X as X i, where X i is a vector of length M that is associated with process P i.  Notation: Assume X, Y are vectors of length N X ≤ Y if and only if X[j] ≤ Y[j] for all j = 1.. N Ex. X = (1,7,3,2) and Y = (0,3,2,1) implies Y ≤ X

Assumed Knowledge (4) Comments: (continued)  The required vector and matrix data structures may be declared (statically) at compile time Need to consider the maximum number of processes (large) and resources (small) that the hardware and operating system can manage.  Ex. int Available [ MAX_Process ] ; int Max [MAX_Process ] [ MAX_Resource ] ;  If structures are allocated dynamically, code must be developed to manage the heap

Banker’s Algorithm Used to determine safe resource allocation to processes  Safety implies no deadlocks will occur By studying different scenarios of possible resource allocation, we try to find at least one scenario that is deadlock free, or safe Time consuming (algorithm complexity)  Resource allocation is performed using safe scenarios Can a request be safely granted?

Banker’s Algorithm At a given time assume that current values are stored in structures  Define: NP :: Number of current processes NR :: Number of currently available resources  Data Structures: Available – vector with NR entries Max, Allocation, Need – matrices with NP rows and NR entries in each row

Safety Algorithm Periodically, we need to determine if the system in a safe state.  Define vectors int Work [ NR ] ; int Finish [ NP ] ;  Initialize Work = Available ; For all k, Finish[ k ] = false ; /* 0 */ Safe State: For a set of processes there exists at least one schedule of allocating resources so that each process can progress to completion.

Safety Algorithm Goal: If all values Finish [ k ] == true (or 1), then the system is in a safe state. Starting at k = 0 (ie. first process).  First step: If both Finish[ k ] == false and Need k ≤ Work Then goto Third step, otherwise continue.  Second step: Work = Work + Allocation k ; Finish[ k ] = true ; /* or 1 */ k = k + 1 ; if k < NR then goto First step, otherwise report “No safe state exists”.

Safety Algorithm Third step: If, for all k from 0 to NR-1, Finish[ k ] == true Then system is in a safe state. Note that it is possible that no safe state exists (see Second step).  Therefore there is possibility of deadlock.

Resource-Request Algorithm We need to determine if a resource request can be safely granted. Define an NxM matrix Request that contains all current resource requests (from M resource types) for all N processes.  Request k is the resource request vector (a row in the matrix) for the process P k.  Resource request information (integer values) is obtained when jobs are submitted (through job headers).

Resource-Request Algorithm When P k requests resources:  Step 1: If Request k ≤ Need k, goto Step 2, otherwise report “Processes exceed max. claim”  Step 2: If Request k ≤ Available, goto Step 3, otherwise make P k wait since resources are not available

Resource-Request Algorithm  Step 3: (Prove the scenario is safe) Available = Available - Request k ; Allocation k = Allocation k + Request k ; Need k = Need k – Request k ;  If the resulting resource-allocation scenario is safe, the allocate the resources to P k to complete the transaction. Requires applying the Safety Algorithm  If the state is not safe, then P k must wait for Request k, and the old state is restored.

Example: Safe State Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 332 Various processes have requested the maximum amount of resources they intend to use (Max). They are allocated resources so that, at a given moment, one knows the Allocation matrix and also what is currently Available. Is the system in a safe state? What is a safe sequence? Following the textbook, we assume that the maximum resources available are: (10, 5, 7)

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 332 Need[0][1][2] P0743 P1122 P2600 P3011 P4431 Determine current Need = Max – Allocation

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 332 Need[0][1][2] P0743 P1122 P2600 P3011 P4431 Determine current Need = Max – Allocation Max – Allocation → Need :: 3 – 2 = 1 2 – 0 = 2 2 – 0 = 2

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 332 Need[0][1][2] P0743 P1122 P2600 P3011 P4431 Determine all processes that can be satisfied by the Available resources. NOTE: Both P1 and P3 are candidates, but we choose P1 because it occurs next.

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 210 Need[0][1][2] P0743 P1--- P2600 P3011 P4431 Now perform the Allocation of Needed resources to P1 Add Need Subtract Need

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 210 Need[0][1][2] P0743 P1--- P2600 P3011 P4431 Once a process’ needs are fulfilled, free those resources to re-allocate to the next process in sequence. Available[0][1][2] 532 Since the Maximum requirement is fulfilled, the process can run to completion in order to free its resources to other processes.

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 532 Need[0][1][2] P0743 P1--- P2600 P3011 P4431 Reapply the updated Available to Need and arrive at the choice of P3 (P4 would work, but occurs later). Then, free its resources.

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 521 Need[0][1][2] P0743 P1--- P2600 P3--- P4431 Reapply the updated Available to Need and arrive at the choice of P3 (P4 would work, but occurs later). Then, free its resources.

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 743 Need[0][1][2] P0743 P1--- P2600 P3--- P4431 Reapply the updated Available to Need and arrive at the choice of P4 (the next one that is satisfied).

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 312 Need[0][1][2] P0743 P1--- P2600 P3--- P4--- Allocate the Need resources to P4.

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 745 Need[0][1][2] P0743 P1--- P2600 P3--- P4--- Allocate the Need resources to P4. Then, since P4 can run to completion, free its resources.

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 745 Need[0][1][2] P0743 P1--- P2600 P3--- P4--- Both P0 and P2 can be satisfied from Available resources. Choose P0 to Allocate the Need. (NOTE: the textbook chooses P2 for this example.)

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 002 Need[0][1][2] P0--- P1--- P2600 P3--- P4--- Both P0 and P2 can be satisfied from Available resources. Choose P0 to Allocate the Need. (NOTE: the textbook chooses P2 for this example.)

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 755 Need[0][1][2] P0--- P1--- P2600 P3--- P4--- Both P0 and P2 can be satisfied from Available resources. Choose P0 to Allocate the Need. (NOTE: the textbook chooses P2 for this example.) Then, free the resources

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 755 Need[0][1][2] P0--- P1--- P2600 P3--- P4--- Finally, we handle P2 which can clearly be satisfied because, as required, Need ≤ Available

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 155 Need[0][1][2] P0--- P1--- P2--- P3--- P4--- Finally, we handle P2 which can clearly be satisfied because, as required, Need ≤ Available

Example Allocation[0][1][2]Max[0][1][2] P P P P P Available[0][1][2] 1057 Need[0][1][2] P0--- P1--- P2--- P3--- P4--- Freeing resources may have to be limited by the actual maximum resource availability (not specified for this example). Also, recall that processes request maximum resources throughout their lifetime, not those needed immediately.

Example Need[0][1][2] P0743 P1122 P2600 P3011 P4431 This leads to the possible resource allocation orderings: P1, P3, P4, P0, P2 Again, this is different than discussed in the textbook (P2 comes first, then P0). For these sequences the vector Finish [ k ] == true for all NP=5 processes Available[0][1][2] 1057 All of the stated Needs have been shown to be allocatable from the total allowed resource set Available. NOTE: All resources have now been reclaimed, in agreement with initial assumption.

Example: Resource Allocation Allocation[0][1][2]Need[0][1][2] P P P P P Available[0][1][2] 332 We established that the state is safe. If P1 makes a new request: Request 1 = { 1, 0, 2 } Can this request be granted safely and immediately? We started with the state tables populated with safe state values.

Example Allocation[0][1][2]. Need [0][1][2] P P12+1=30+0=00+2=21-1=02-0=22-2=0 P P P Available[0][1][2] 3-1=23-0=32-2=0 For: Request 1 = { 1, 0, 2 } Assume it can be allocated immediately.

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 230 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety.

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 210 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, Allocate resources to P1

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 532 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, Free P1 resources, Find P3: Next <= Available

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 521 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, Allocate resources to P3

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 743 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, Free P3 resources, Find P4 (next): Next <= Available

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 312 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, Allocate resources to P4

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 745 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, P0, Free P4 resources, Find P0 (next): Next <= Available

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 755 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, P0, P2 } Free P0 resources, Find P2 (next): Next <= Available

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 755 For: Request 1 = { 1, 0, 2 } After modifying the Need and Available, then test for safety. We find a sequence: { P1, P3, P4, P0, P2 } SAFE STATE ! Since the state is safe, then the request R 1 can be granted immediately.

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 230 Having already assumed: Request 1 = { 1, 0, 2 } for P1 Now consider, in addition: Request 4 = { 3, 3, 0 } for P4. This request cannot be granted because there are not sufficient resources available.

Example Allocation[0][1][2]. Need[0][1][2] P P P P P Available[0][1][2] 230 Having already assumed: Request 1 = { 1, 0, 2 } for P1 Next consider: Request 0 = { 0, 2, 0 } for P0. This request cannot be granted. Although sufficient resources are available, P0 retains a Need and this leads to an unsafe state. It cannot be guaranteed to complete, hence it cannot free sufficient resources.

Deadlock Detection Previous Banker’s algorithms provide answers to the questions:  Is a particular system (of allocated resources) safe?  Can a request for resources be granted safely? However, a safe state is not necessarily deadlock free !

Deadlock Detection Now ask the question  For a given resource allocation state, does a deadlock exist?  These slides elaborate on this point and an example This is important to support  Deadlock Prevention (or Avoidance)  Deadlock Recovery  These slides do not discuss these points. Read the discussion in the textbook (Silberschatz, Chapter 7)

Deadlock Detection Data structures  Scalars NP :: Number of Processes NR :: Number of Resources  Vectors Available, Work :: NR resources Finish :: NP processes  NP x NR Matrices Allocation :: Each of NP processes have been allocated NR resources Request :: Each of NP processes requesting NR resources

Deadlock Detection Algorithm  Step 1: Initialize Work = Available ; For all k < NP  if Allocation k ≠ 0 Then Finish[ k ] = false Else Finish[ k ] = true ;  Note that Finish[k]=true; means that it cannot produce a deadlock since it does not control any resources.

Deadlock Detection Algorithm  Middle logic Step 2:  Find a process index k such that Finish[ k ] == false AND Request k ≤ Work  If no k exists, goto Step 4 (Final step) Step 3:  Work = Work + Allocation ;  Finish[ k ] = true ;  Goto Step 2  This investigates every possible allocation sequence

Deadlock Detection Algorithm  Final logic Step 4:  If Finish[ k ] == false for any k in 0 ≤ k ≤ (NR-1) System is in a deadlock state Else, System is deadlock free. NOTE:  Finish[ k ] == false implies that P k is deadlocked

Example: Deadlock Detection Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 000 We consider the following state with declared Allocations and Available resources. A Request is made by every process at the same time. Can a sequence be found for allocating resources that does not lead to deadlock?

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 000 There are two processes, P0 and P2, that are not requesting resources. Start at P0 because it is first (and later, if necessary, we can try P2 first).

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 010 Assume that P0 runs to completion, then frees its resources so they become available again.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 010 Next, P2 is assumed to complete.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 313 And, P2 frees its resources.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 313 The next state (in order) that can complete is P3. NOTE: Also could allocate and complete P1 and P4.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 213 Allocate resources to P3.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 524 Then, reclaim resources from P3.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 524 Noting that we can allocate resources and complete both P1 and P4, we choose P4 because it is next in order.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 522 Allocate resources to P4.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 526 Then reclaim resources from P4.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 324 Finally, allocate resources to P1.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 726 And reclaim the resources from P1.

Example Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 726 And reclaim the resources from P1. Thus the sequence { P0, P2, P3, P4, P1 } is deadlock free, since Finish[k] = true for all k.

Example And reclaim the resources from P1. Thus the sequence { P0, P2, P3, P4, P1 } is deadlock free, since Finish[k] = true for all k. Other sequences that are deadlock free are: { P0, P2, P3, P1, P4 } { P2, P0, P3, P1, P4 } { P2, P0, P3, P4, P1 } Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 000

Example: Modified request Now, modify the resource request for P2 – it requests one more resource of type [2]. Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 000 This request will lead to deadlock. Follow the trace as the scenario develops...

Example: Modified request Only P0 has Request 0 ≤ Work Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 000

Example: Modified request Allocate resources to P0, then reclaim. Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 010

Example: Modified request Now we see that there are not sufficient resources to allocate to any other process. Allocation[0][1][2]Request[0][1][2] P P P P P Work[0][1][2] (Available) 010 Hence, this state is deadlocked and the resource request cannot be made.

Deadlock Recovery Read Section 7.7 in the textbook. Most systems take the simplest approach, aborting all deadlocked processes More sophisticated approaches can be developed  They take much more time to execute hence are applied less often  They are strongly dependent on choice of policies.

Summary We covered essential aspects of deadlock detection and avoidance in respect of resource allocation to handle process requests. We paid special attention to  Issue of safe states  Resource allocation Read Chapter 7 of Silberschatz et al.