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 Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock 7.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter Objectives Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks To present a number of different methods for preventing or avoiding deadlocks in a computer system. 7.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 The Deadlock Problem The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Example z System has 2 disk drives. z P 1 and P 2 each hold one disk drive and each needs another one. Example z semaphores A and B, initialized to 1 P 0 P 1 wait (A); wait(B) wait (B); wait(A) 7.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Bridge Crossing Example 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. 7.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 System Model System Model Resource types R 1 , R 2 , . . ., R m CPU cycles, memory space, I/O devices Each resource type R i has W i instances. Each process utilizes a resource as follows: z request z use z release 7.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Deadlock Characterization Deadlock Characterization Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait: there exists a set {P 0 , P 1 , …, P 0 } of waiting processes such that 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 0 is waiting for a resource that is held by P 0 . Deadlock can arise if four conditions hold simultaneously. 7.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Resource Resource - - Allocation Graph Allocation Graph V is partitioned into two types: z P = {P 1 , P 2 , …, P n }, the set consisting of all the processes in the system. z R = {R 1 , R 2 , …, R m }, the set consisting of all resource types in the system. request edge – directed edge P 1 → R j assignment edge – directed edge R j → P i A set of vertices V and a set of edges E. 7.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Resource Resource - - Allocation Graph (Cont.) Allocation Graph (Cont.) Process Resource Type with 4 instances P i requests instance of R j P i is holding an instance of R j P i P i R j R j 7.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Example of a Resource Allocation Graph Example of a Resource Allocation Graph [...]... a priori in the system Operating System Concepts - 7th Edition, Feb 14, 2005 7.22 Silberschatz, Galvin and Gagne ©2005 Resource-Allocation Graph Operating System Concepts - 7th Edition, Feb 14, 2005 7.23 Silberschatz, Galvin and Gagne ©2005 Unsafe State In Resource-Allocation Graph Operating System Concepts - 7th Edition, Feb 14, 2005 7.24 Silberschatz, Galvin and Gagne ©2005 Resource-Allocation Graph... deadlock Operating System Concepts - 7th Edition, Feb 14, 2005 7.13 Silberschatz, Galvin and Gagne ©2005 Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state Allow the system to enter a deadlock state and then recover Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX Operating System Concepts - 7th... terminates, Pi +1 can obtain its needed resources, and so on Operating System Concepts - 7th Edition, Feb 14, 2005 7.18 Silberschatz, Galvin and Gagne ©2005 Basic Facts If a system is in safe state ⇒ no deadlocks If a system is in unsafe state ⇒ possibility of deadlock Avoidance ⇒ ensure that a system will never enter an unsafe state Operating System Concepts - 7th Edition, Feb 14, 2005 7.19 Silberschatz, Galvin... requires an order of n2 operations, where n is the number of vertices in the graph Operating System Concepts - 7th Edition, Feb 14, 2005 7.34 Silberschatz, Galvin and Gagne ©2005 Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Operating System Concepts - 7th Edition, Feb 14, 2005 7.35 Corresponding wait-for graph Silberschatz, Galvin and Gagne ©2005 Several Instances of a Resource... request for (0,2,0) by P0 be granted? Operating System Concepts - 7th Edition, Feb 14, 2005 7.32 Silberschatz, Galvin and Gagne ©2005 Deadlock Detection Allow system to enter deadlock state Detection algorithm Recovery scheme Operating System Concepts - 7th Edition, Feb 14, 2005 7.33 Silberschatz, Galvin and Gagne ©2005 Single Instance of Each Resource Type Maintain wait-for graph Nodes are processes Pi... resource-allocation state is restored Operating System Concepts - 7th Edition, Feb 14, 2005 7.29 Silberschatz, Galvin and Gagne ©2005 Example of Banker’s Algorithm 5 processes P0 through P4; 3 resource types: A (10 instances), B (5instances), and C (7 instances) Snapshot at time T0: Allocation Max Available ABC ABC ABC P0 010 753 332 P1 200 322 P2 302 902 P3 211 222 P4 002 433 Operating System Concepts -. .. Unsafe , Deadlock State Operating System Concepts - 7th Edition, Feb 14, 2005 7.20 Silberschatz, Galvin and Gagne ©2005 Avoidance algorithms Single instance of a resource type Use a resourceallocation graph Multiple instances of a resource type Use the banker’s algorithm Operating System Concepts - 7th Edition, Feb 14, 2005 7.21 Silberschatz, Galvin and Gagne ©2005 Resource-Allocation Graph Scheme... be a circular-wait condition Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes Operating System Concepts - 7th Edition, Feb 14, 2005 7.17 Silberschatz, Galvin and Gagne ©2005 Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state System is in... the resource allocation graph Operating System Concepts - 7th Edition, Feb 14, 2005 7.25 Silberschatz, Galvin and Gagne ©2005 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 Operating System Concepts - 7th Edition, Feb 14, 2005...Resource Allocation Graph With A Deadlock Operating System Concepts - 7th Edition, Feb 14, 2005 7.11 Silberschatz, Galvin and Gagne ©2005 Graph With A Cycle But No Deadlock Operating System Concepts - 7th Edition, Feb 14, 2005 7.12 Silberschatz, Galvin and Gagne ©2005 Basic Facts If graph contains no cycles ⇒ no deadlock . 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 Chapter 7: Deadlocks . ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Methods for Handling Deadlocks Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state. Allow the system. problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. 7.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb