Presentation is loading. Please wait.

Presentation is loading. Please wait.

Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.

Similar presentations


Presentation on theme: "Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks."— Presentation transcript:

1 Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks

2 7.2 Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock

3 7.3 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

4 7.4 The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire another 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)

5 7.5 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 Assumed to be identical and interchangeable Each process utilizes a resource in three steps: Request  System call like “request()”, “open()”, “allocate()” Use  Whatever the process needs the resource for Release  System call like “release()”, “close()”, “free()”

6 7.6 Deadlock Characterization Deadlock can arise if four conditions hold simultaneously 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 n } 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 n is waiting for a resource that is held by P 0

7 7.7 Resource-Allocation Graph A directed graph that represents the system’s resource allocation among processes Vertices include the set of all processes P (circles) and of all resources R (squares) including all instances of that resource Edge direction represents a process requesting a resource (request edge P i  R j ) or an instance of a resource being allocated to a process (assignment edge R j  P i ) PiPi PiPi RjRj RjRj PiPi RjRj

8 7.8 Resource-Allocation Graph

9 7.9 Resource-Allocation Graph

10 7.10 Resource-Allocation Graph

11 7.11 If graph contains no cycles  there is no deadlock If graph contains a cycle  there might be a deadlock Resource-Allocation Graph

12 7.12 Handling Deadlocks Three ways to deal with deadlocks Ensure that the system will never enter a deadlock state  Deadlock prevention & deadlock avoidance Allow the system to enter a deadlock state, detect it, and then recover  Deadlock detection & deadlock recovery Ignore the problem and pretend that deadlocks never occur in the system  User programs have to realize by themselves that they are in deadlock, and programmer has to include code to handle it  Otherwise, program is “not responding” and user terminates it  Used by most operating systems, including UNIX and Windows

13 7.13 Deadlock Prevention Deadlock can arise if four conditions hold simultaneously Mutual exclusion, hold and wait, no preemption, circular wait We can prevent deadlocks by preventing any one of them Mutual Exclusion Cannot be avoided, as some resources are intrinsically non- sharable Critical-section data, for example Hold and Wait Must guarantee that whenever a process requests a resource, it does not hold any other resources  Process requests all resources it needs at once at creation time  Process requests one resource at a time, and only gets another when it’s done  Low resource utilization, limits concurrency, possible starvation

14 7.14 Deadlock Prevention No Preemption If a process holds some resources and requests another resource that cannot be immediately allocated to it, then all its resources are released If a process requests a resource that is being held by a waiting process, waiting process forced to release it In both cases, process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting Circular Wait Impose an order on all resource types {R 1, R 2,..., R m } A process can request any number of an instance of a resource, but must get all in one request Processes can only requests resources in increasing order  After holding R i process can only get R j if R i < R j Processes can only requests resources in decreasing order  When requesting R i process must have released all resources R j such as R i ≤ R j Can be accomplished by lock-order-verifier programs, like Witness on FreeBSD

15 7.15 Deadlock Avoidance Requires that the system has some additional information about which resources each process will need Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need A deadlock-avoidance algorithm can then ensure that there can never 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

16 7.16 Deadlock Avoidance System is in safe state if there exists a sequence of all the processes in the systems such that for each P j, the resources that P j can still request can be satisfied by currently available resources + resources held by all P i, with P i < P j If no such order exists, the system is in an unsafe state An unsafe state may be a deadlock state

17 7.17 Deadlock Avoidance System with 12 tape drives Max requestedCurrently allocated P 0 105 P 1 42 P 2 92 Sequence is ok; System is in a safe state System can go into an unsafe state if P 2 requests 2 or 3 more tape drives Will become deadlocked if P 2 requests > 3 drives Our mistake was granting P 2 ’s request out of order, and going into an unsafe state Deadlock-avoidance algorithm has to prevent that

18 7.18 If we do not actively prevent deadlocks, then deadlocks may occur To correct the situation, we need Deadlock detection algorithm Deadlock recovery scheme Deadlock Detection

19 7.19 Deadlock Detection If there is a single instance of each resource Wait-For Graph: Variant of the resource-allocation graph, without resources 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. If there is a cycle, there exists a deadlock

20 7.20 When, and how often, to invoke detection algorithm depends on: How often a deadlock is likely to occur? How many processes will be involved? If deadlocks are a frequent problem, the detection algorithm should be invoked often If we invoke it at the beginning of a deadlock, we can not only detect it early but know exactly which processes are responsible If we wait too long, more processes will have become involved, more cycles will have formed, and we won’t know for sure what’s the core problem But if invoked too often, it becomes an expensive overhead Deadlock Detection

21 7.21 Deadlock Recovery Notify the user and let him manually resolve it Abort some of the processes Preempt some locked resources from the deadlocked processes

22 7.22 Which processes to abort? 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 – How long process has computed, and how much longer to completion – Resources the process has used – Resources process needs to complete – How many processes will need to be terminated – Is process interactive or batch Which process to preempt? Minimize “cost”, i.e. number of resources held, process execution time Can the process be rolled back to a safe state? How to avoid starvation? Deadlock Recovery

23 7.23 Review Consider the Dining Philosopher problem. Show how the four necessary conditions for deadlocks exist in that situation.

24 7.24 Exercises Read the entire chapter, but skip the parts about the Banker’s Algorithm in various sections If you have the “with Java” textbook, skip the Java sections and subtract 1 to the following section numbers 7.1 7.2 7.4 7.6 7.7 7.10 7.11 7.17 7.19 7.23 7.25 7.26

25 Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, End of Chapter 7


Download ppt "Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks."

Similar presentations


Ads by Google