Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Course: Operating Systems Instructor: M Umair M Umair – http://www.m-umair.com Deadlock M Umair – http://www.m-umair.com Introduction Under normal operation, a resource allocations proceed like this 1. Request a resource (suspend until available if necessary ). 2. Use the resource. 3. Release the resource. { Ref: http://web.cs.wpi.edu/~cs3013/c07/lectures/Section07-Deadlocks.pdf } M Umair – http://www.m-umair.com Introduction A set of processes is in a deadlocked state when every process in the set is waiting for an event that can be caused only by another process in the set. { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com The 4 necessary conditions Mutual exclusion One or more than one resource must be held by a process in a non-sharable (exclusive) mode. Hold and Wait A process holds a resource while waiting for another resource held by other process. No Preemption There is only voluntary release of a resource - nobody else can make a process give up a resource. Circular Wait Process A waits for Process B waits for Process C .... waits for Process A. { Ref: http://web.cs.wpi.edu/~cs3013/c07/lectures/Section07-Deadlocks.pdf } M Umair – http://www.m-umair.com Resource Allocation Graph { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Handling deadlock We can deal with the deadlock problem in one of three ways: • We can use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlocked state. • We can allow the system to enter a deadlocked state, detect it, and recover. • We can ignore the problem altogether and pretend that deadlocks never occur in the system. { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Prevention Mutual Exclusion The mutual-exclusion condition must hold for non-sharable resources. For example, a printer cannot be simultaneously shared by several processes. Sharable resources, in contrast, do not require mutually exclusive access and thus cannot be involved in a deadlock. Read-only files are a good example of a sharable resource. We cannot prevent deadlocks by denying the mutual-exclusion condition, because some resources are intrinsically nonsharable. { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Prevention Hold and Wait To ensure that the hold-and-wait condition never occurs One protocol that can be used requires each process to request and be allocated all its resources before it begins execution. Problems? Solution: A process may request some resources and use them. Before it can request any additional resources, however, it must release all the resources that it is currently allocated. { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Prevention DVD Drive Hard Drive Printer Problems? >Device hold >Starvation DVD Drive Hard Drive Printer { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Prevention No Preemption If a process is holding some resources and requests another resource that cannot be immediately allocated to it (that is, the process must wait), then all resources the process is currently holding are preempted. { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Prevention Circular Wait Let R = {R1, R2, ..., Rm} be the set of resource types Define a one-to-one function F: R→N, where N is the set of natural numbers. A process can initially request any number of instances of a resource type —say, Ri . After that, the process can request instances of resource type Rj if and only if F(Rj ) > F(Ri ). Proof ? { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Avoidance Safe State A state is safe if the system can allocate resources to each process (up to its maximum) in some order and still avoid a deadlock. A safe state is not a deadlocked state. Conversely, a deadlocked state is an unsafe state. Not all unsafe states are deadlocks. An unsafe state may lead to a deadlock. { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Avoidance Total Resources: 12 Process Maximum Need Currently Held Current Need Currently Free P0 10 5 5 - P1 4 2 2 - P2 9 2 5 - Total - 9 - 3 Process Sequence: P1, P0, P2 (Is it safe ?) { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Avoidance The Banker’s Algorithm A new process must declare the maximum number of instances of each resource type that it may need. This number may not exceed the total number of resources in the system System grants request only if the request will result into a safe state. { Ref: http://www.cs.colostate.edu/~cs551/CourseNotes/Bankers.html} M Umair – http://www.m-umair.com Deadlock Avoidance Banker Algorithm's Example - 1 A: 6k, B:5K, C:4k, D:7K deposits in bank. Bank maintain 10K cash in the branch { Ref: Modern Operating Systems, Andrew S. Tanenbaum } M Umair – http://www.m-umair.com Deadlock Avoidance Banker Algorithm's Example -2 Assume we have the following resources 5 tape drives 2 graphic displays 4 printers 3 disks A vector representing our total resources Total = (5, 2, 4, 3) { Ref: http://www.cs.colostate.edu/~cs551/CourseNotes/Bankers.html} M Umair – http://www.m-umair.com Deadlock Avoidance Allocation [4,2,2,3] Total = (5, 2, 4, 3) Allocation [4,2,2,3] Available [1,0,2,0] Need { Ref: http://www.cs.colostate.edu/~cs551/CourseNotes/Bankers.html} M Umair – http://www.m-umair.com Deadlock Recovery Process Termination Abort all deadlocked processes. →Running for some time -> Resource/Time →Results of partial computations -> Lost →Re-compute later -> Resource/Time The Problems Abort one process at a time until the deadlock cycle is eliminated. →Considerable overhead →What if process is updating a file ? →Deadlock-detection algorithm must be invoked to check the possibility of reoccurring deadlock. { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Recovery Considerations for process termination 1. Priority of the process 2. How long the process has computed and how much longer the process will compute before completing its designated task 3. How many and what types of resources the process has used (whether the resources are simple to preempt) 4. How many more resources the process needs in order to complete 5. How many processes will need to be terminated 6. Whether the process is interactive or batch { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com Deadlock Recovery Resource Preemption Selecting a victim Determine the order of preemption to minimize cost Rollback Safe state rollback Restart it from that state Difficult to determine what a safe state is Keep information about the state of all running processes? Starvation How to ensure that resources will not always be preempted from the same process? { Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin } M Umair – http://www.m-umair.com