Deadlocks Peng Lu In a multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources; if the resources are not available at that time, the process enters a wait state. Waiting processes may never again change state, because the resources they have requested are held by other waiting processes. This situation is called a deadlock. CSCI-6303 Dr. John P. Abraham
Deadlocks CSCI-6303 Dr. John P. Abraham
Deadlocks “When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.” ---The Kansas legislature, early in the 20th century. Perhaps the best illustration of a deadlock is : CSCI-6303 Dr. John P. Abraham
Deadlocks 1.System Model 2.Deadlock Characterization 3.Methods for Handling Deadlocks 4.Deadlock Prevention 5.Deadlock Avoidance 6.Deadlock Detection 7.Recovery from Deadlock In deadlocks, we will disuses these topics. CSCI-6303 Dr. John P. Abraham
1. System Model Under the normal mode of operation, a process utilizes a resource as follows: Request Use Release A process must request a resource before using it, and must release the resource after using it. Request: If the request cannot be granted immediately (for example, the resource is being used by another process), then the requesting process must wait until it can acquire the resource. Use: the process can operate on the resource (for example, if the resource is a printer, the process can print on the printer). Release: the process releases the resource. CSCI-6303 Dr. John P. Abraham
2.Deadlock Characterization Necessary Conditions: Mutual exclusion Hold and wait No preemption Circular wait 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 CSCI-6303 Dr. John P. Abraham
Resource Allocation Graph With A Deadlock P = Process R = Resource CSCI-6303 Dr. John P. Abraham
3. Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state Prevention Allow the system to enter a deadlock state and then recover Detection Ignore the problem and pretend that deadlocks never occur in the system Most operating systems CSCI-6303 Dr. John P. Abraham
4. Deadlock Prevention Mutual Exclusion Hold and Wait No Preemption Circular Wait CSCI-6303 Dr. John P. Abraham
5. Deadlock Avoidance Safe state Resource-Allocation Graph Algorithm Banker’s Algorithm Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. The deadlock-avoidance algorithm dynamically Examines the resource-allocation state to 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. CSCI-6303 Dr. John P. Abraham
Safe State CSCI-6303 Dr. John P. Abraham
Resource-Allocation Graph Algorithm CSCI-6303 Dr. John P. Abraham
Unsafe State In Resource-Allocation Graph CSCI-6303 Dr. John P. Abraham
Banker’s Algorithm Data Structures: Available: A vector of length m indicates the number of available resource of each type. Max: An n x m matrix defines the maximum demand of each process. Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. Need: An n x m matrix indicates the remaining resource need of each process. The name was chosen because this algorithm could be used in a banking system to ensure That the band never allocates it’s available Cash such that it can no longer satisfy the Needs of all it’s customers. CSCI-6303 Dr. John P. Abraham
6. Deadlock Detection Single Instance of Each Resource Type Several Instances of a Resource Type Detection-Algorithm Usage SIER Type: If all resources have only a single instance, the we can define a deadlock detection algorithm that uses a variant of the resource-allocation graph, called a wait-for graph. Detection-Algorithm Usage When should we invoke the detection algorithm? The answer depends on two factors: How often is a deadlocks likely to occur? How many processes will be affected by deadlock when it happens? CSCI-6303 Dr. John P. Abraham
7. Recovery from Deadlock Process Termination Resource Preemption When a detection algorithm detecting the deadlock Exists, one possibility is let the operator deal with The deadlock manually; the other possibility is to Let the system recover from the deadlock Automatically. There two option for breaking a Deadlock: CSCI-6303 Dr. John P. Abraham
Summary of the Deadlock Prevention or avoidance deadlocks, ensuring that the system will never enter a deadlock state. Allow the system to enter deadlock state, detect it, and then recover. Ignore the problem all together, and pretend that deadlocks never occur in the system. CSCI-6303 Dr. John P. Abraham
Thanks! CSCI-6303 Dr. John P. Abraham
Resource-Allocation Graph P = Process R = Resource CSCI-6303 Dr. John P. Abraham
Graph With A Cycle But No Deadlock CSCI-6303 Dr. John P. Abraham