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 ● System has 2 disk drives. ● P 1 and P 2 each hold one disk drive and each needs another one. ■ Example ● 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 R1, R2, . . ., Rm CPU cycles, memory space, I/O devices ■ Each resource type Ri has Wi instances. ■ Each process utilizes a resource as follows: ● request ● use ● 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 {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0. 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-Allocation Graph Resource-Allocation Graph ■ V is partitioned into two types: ● P = {P 1 , P 2 , …, P n }, the set consisting of all the processes in the system. ● R = {R 1 , R 2 , …, R m }, the set consisting of all resource types in the system. ■ request edge – directed edge P1 → Rj ■ assignment edge – directed edge Rj → Pi 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-Allocation Graph (Cont.) Resource-Allocation Graph (Cont.) ■ Process ■ Resource Type with 4 instances ■ Pi requests instance of Rj ■ Pi is holding an instance of Rj 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 [...]... - 7th Edition, Feb 14, 2005 7. 13 Silberschatz, Galvin and Gagne ©2005 Methods for Handling Deadlocks s Ensure that the system will never enter a deadlock state s Allow the system to enter a deadlock state and then recover s Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX Operating System Concepts - 7th Edition, Feb 14, 2005 7. 14... claimed 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... System Concepts - 7th Edition, Feb 14, 2005 7. 29 Silberschatz, Galvin and Gagne ©2005 Example of Banker’s Algorithm s 5 processes P0 through P4; 3 resource types: A (10 instances), B (5instances), and C (7 instances) s Snapshot at time T0: Allocation Max Available ABC ABC ABC P0 010 75 3 332 P1 200 322 P2 302 902 P3 211 222 P4 002 433 Operating System Concepts - 7th Edition, Feb 14, 2005 7. 30 Silberschatz,... resources, and so on Operating System Concepts - 7th Edition, Feb 14, 2005 7. 18 Silberschatz, Galvin and Gagne ©2005 Basic Facts s If a system is in safe state ⇒ no deadlocks s If a system is in unsafe state ⇒ possibility of deadlock s Avoidance ⇒ ensure that a system will never enter an unsafe state Operating System Concepts - 7th Edition, Feb 14, 2005 7. 19 Silberschatz, Galvin and Gagne ©2005 Safe,...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 s If graph contains no cycles ⇒ no deadlock s If graph contains a cycle... Operating System Concepts - 7th Edition, Feb 14, 2005 7. 25 Silberschatz, Galvin and Gagne ©2005 Banker’s Algorithm s Multiple instances s Each process must a priori claim maximum use s When a process requests a resource it may have to wait s 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 7. 26 Silberschatz, Galvin... ©2005 Safe, Unsafe , Deadlock State Operating System Concepts - 7th Edition, Feb 14, 2005 7. 20 Silberschatz, Galvin and Gagne ©2005 Avoidance algorithms s Single instance of a resource type Use a resource-allocation graph s 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... ABC P0 P1 122 P2 600 P3 011 P4 s 74 3 431 The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria Operating System Concepts - 7th Edition, Feb 14, 2005 7. 31 Silberschatz, Galvin and Gagne ©2005 Example: P1 Request (1,0,2) s Check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒ true Allocation Available ABC ABC ABC P0 010 74 3 230 P1 302 020 P2 301 600 P3... (3,3,0) by P4 be granted? Can 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 s Allow system to enter deadlock state s Detection algorithm s Recovery scheme Operating System Concepts - 7th Edition, Feb 14, 2005 7. 33 Silberschatz, Galvin and Gagne ©2005 Single Instance of Each Resource Type s Maintain... requires an order of n 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 Type . 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. possibility of deadlock. 7. 14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Methods for Handling Deadlocks Methods for Handling Deadlocks ■ Ensure that. ©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