310 likes | 327 Views
Learn about deadlocks in operating systems, necessary conditions, prevention methods, system models, and more. Understand deadlock avoidance and handling strategies. Explore resource-allocation graphs and practical approaches to prevent deadlocks. Improve your knowledge of system safety conditions to prevent system deadlock issues effectively.
E N D
School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Deadlocks
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 • Semaphores A and B, initialized to 1 P0 P1 wait (A); wait(B) wait (B); wait(A)
Necessary Conditions for Deadlock Deadlock may arise if four conditions hold simultaneously • Mutual exclusion:only one process can use resource at a time • Hold and wait: a process holding at least one resource and waiting for additional resources held by other processes • No preemption: a resource can be released only voluntarily by the process holding it • Circular wait: there exists a set {P0, P1, …, Pn} of processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for P2, …, Pn–1 is waiting Pn, and Pn is waiting for P0
System Model: Resources • Resource types R1, R2, . . ., Rm • CPU, memory space, I/O devices • Each resource type Ri has Wi instances • Each process utilizes a resource as follows: • Request • Process can block if resource not free • Use • Release
System Model: Resource-Allocation Graph • Graph G = (V, E) • Set of vertices V, and set of edges E • V is partitioned into two types: • P = {P1, P2, …, Pn}, the set consisting of all processes in the system • R = {R1, R2, …, Rm}, the set consisting of all resource types in the system • request edge – directed edge Pi Rj • assignment edge – directed edge Rj Pi
Pi Rj Pi Rj Resource-Allocation Graph • Process • Resource Type with 4 instances • Pirequests an instance of Rj (request edge) • Piis holding an instance of Rj (assignment edge) • Pi Deadlock?
Graph With A Cycle But No Deadlock Deadlock?
One Instance per Resource Type Deadlock?
Methods of Handling Deadlocks • Ensure system will never enter a deadlock state • Deadlock Prevention • Deadlock Avoidance • Allow system to enter deadlock state, then recover • Deadlock Detection and Recovery • Third method? Hint: it is practical! • Ignore the problem and pretend that deadlocks never occur in the system • Done by most OS, including UNIX and Windows
Deadlock Prevention • Ensure at least one of the conditions cannot hold • By restraining the ways resource requests can be made • Mutual Exclusion • Cannot be violated! • Hold and Wait • A process requests a resource only if it does not hold any other resources, or • A process requests and is allocated all its resources before it begins execution • Disadvantages? • Low resource utilization • Starvation possible
Deadlock Prevention (cont’d) • No Preemption • Preempt all resources currently held by a process if the next resource request cannot be satisfied, or • Preempt resources held by other process if other process is waiting on some other resource • Problems? • Resources whose states cannot be easily savede.g., printers • Atomicity
Deadlock Prevention (cont’d) • Circular Wait • Impose a total ordering on all resource types • Assign unique numbers (IDs) to resources • And enforce that each process requests resources in an increasingorder of the IDs • Exercise: prove that if we follow the above protocol, no circular wait can occur • Note: • Deadlock Prevention methods may results in low resource utilization and low system throughput • Deadlock avoidance
Deadlock Avoidance • Requires the system to have some information on how resources will be requested • Each process declares the maximum number of resources of each type that it may need • Deadlock-avoidance algorithm: • When a process requests an available resource, OS decides if allocation leaves the system in a safe state • If yes, grant the resource • Else, process must wait • System state is defined by the • Number of available resources • Number of allocated resources • Maximum demands of the processes
Safe State • The system can allocate resources to each process (up to its maximum) in some order and still avoid a deadlock • Safety Condition: • There exists a sequence <P1, P2, …, Pn> of ALL processes such that for each Pithe resources that Pican still request can be satisfied by currently available resources plus resources held by all Pj with j < i. • Why is this condition enough to avoid deadlocks? • If Pi resource needs are not immediately available, then Pi can wait until all Pjhave finished. • When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. • When Pi terminates, Pi +1 can obtain its needed resources, and so on.
Deadlock Avoidance Algorithms • Resource-Allocation-Graph algorithm • Single instance of each resource type • Banker’s algorithm • Multiple instances of a resource type
Resource-Allocation Graph • Definition: A claim edge from Pi to Rj indicates that process Pimay request resource Rj • Represented by a dashed line in the graph • Claim edge converts to request edge when a process requests a resource • Request edge converted to an assignment edge when the resource is allocated to the process • When a resource is released by a process, assignment edge reconverts to a claim edge • Resources must be claimed a prioriin the system • All claim edges of a process must be available at its start
Resource-Allocation Graph Assignment Edge Request Edge Claim Edge Claim Edge
Resource-Allocation Graph Algorithm • When Pi requests Rj, the request can be granted only if converting the request edge to assignment edge does not create a cycle in the resource-allocation graph Unsafe
Banker’s Algorithm • Multiple instances of resources • Available: No. of instances available of each type • Available[i] = k: k instances of resource type i • Maxi: Maximum demand of Pi • Maxi[j] = k: Pi will need k inst. of resource j to execute • Declared apriori • Allocationi: Resources currently allocated to Pi • Allocationi[j] = k: k inst. of resource j are allocated to Pi • Needi: Additional resources Pi may still request • Needi[j] = k: k inst. of resource j may be request by Pi • Needi = Maxi - Allocationi
Banker’s Algorithm: Basic Idea • If Pirequests resource AND resource is available • Pretend to allocate requested resource to Pi by modifying the state • Check whether the resulting state is safe by finding any execution sequence of processes that satisfies safety condition • If state is safe, the resource is assigned to Pi • Safety Algorithm • Resource-Request Algorithm
Banker’s Algorithm: Safety Algorithm • Let Work and Finish be vectors: • Work = Available • Finish[i] = false for all processes Pi • Find an index i such that: • Finish[i] == false AND Needi≤ Work If no such i exists, go to step 4. • Work = Work + AllocationiFinish[i] = trueGo to step 2. • If Finish[i] = true for all i, then the system is in a safe state.
Banker’s Algorithm: Resource-Request Algorithm • Requesti: Request vector for Pi • Requesti[j] = k: Pi requests k instances of resource j • If Requesti > Needi, raise error. • If Requesti≤Availablei, go to step 3. Otherwise Pi must wait. • Pretend: • Available = Available –Requesti • Allocationi = Allocationi + Requesti • Needi = Needi–Requesti If resulting state is safe, allocate resources. Otherwise Pi must wait.
Deadlock Detection • Allow system to enter deadlock state • Then use Detection algorithm to detect deadlock • And a Recovery scheme to mitigate its effects
Single Instance of Each Resource Type • Maintain wait-for graph • Nodes are processes • Pi Pjif Piis waiting forPj • Periodically invoke an algorithm that searches for cyclesin the graph. If there is a cycle, there exists a deadlock • An algorithm to detect a cycle in a graph requires an order ofn2 operations, where n is the number of vertices in the graph
Resource-Allocation Graph and Wait-for Graph Corresponding wait-for graph Resource-Allocation graph
Several Instances of a Resource Type • The algorithm is similar to the Banker’s Algorithm • See textbook • When to invoke deadlock detection algorithm? • Depends on how often a deadlock is likely to occur • Every time allocation cannot be satisfied – too much! • Once every hour or so …? • When CPU utilization drops to less than 40% • How many processes will need to be rolled back? • One for each disjoint cycle
Recovery from Deadlock: Process Termination • Abort all deadlocked processes, or • Abort one process at a time until the deadlock cycle is eliminated • How can we choose a victim process 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?
Recovery from Deadlock: Resource Preemption • Preempt resources from some processes • Select a victim – minimize cost • Rollback – return the process to some safe state, restart process from that state • Starvation – same process may always be picked as victim, include number of rollbacks in cost factor
Summary • Deadlock: A set of processes each holding a resource and waiting for a resource held by another process in the set • Four necessary conditions • Mutual exclusive, no preemption, hold and wait, circular wait • If they all hold, deadlock may (or may not) occur • Deadlock handling • Prevention: ensure that at least one of the necessary conditions does not hold (may yield low utilization) • Avoidance: decide for each request whether the process should wait, to avoid leaving the system in unsafe state • Resource-allocation graph: single instance of each resource • Banker’s algorithm: multiple instances of each resource • Detection and Recovery • Detection algorithms • Recovery: process termination or resource preemption