450 likes | 466 Views
Explore Deadlock states, prevention, avoidance, detection, and recovery in multiprogramming environments. Learn about deadlock characterization, causes, and resource-allocation graphs.
E N D
Operating System Principles Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
Chapter 7 Deadlocks • In a multiprogramming environment • Several processes may compete for a finite number of resources • A process requests resources • If no available, the process enter a wait state. • Waiting process may never again change state • Such a situation is called a deadlock • A law passed by the Kansas legislature • 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. Chapter 7 Deadlocks
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Summary Exercises Chapter 7 Deadlocks Chapter 7 Deadlocks
7.1 System Model • A process may utilize a resource in only the following sequence • Request • If this request cannot be granted immediately, the requesting process must wait. • Use • Release • Examples • open and close file system calls • allocate and free memory system calls Chapter 7 Deadlocks
Deadlock State • A set of processes is in a deadlock state when every process in the set is waiting for an event that can be caused only by another process in the set. • Resources • Physical resources • printers, tape drives, memory space, CPU • Logical resources • files, semaphores, monitors • Other types of events • IPC facilities Chapter 7 Deadlocks
Deadlock Examples • Example • A system with 3 CD RW drives • Each of three processes holds one of these three CD RW drives • Each process now requests another drive • Example • semaphores A and B, initialized to 1 P0P1 wait (A); wait (B); wait (B); wait (A); Chapter 7 Deadlocks
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. Chapter 7 Deadlocks
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Summary Exercises Chapter 7 Deadlocks Chapter 7 Deadlocks
7.2.1 Necessary Conditions • A deadlock situation arises if the following four conditions hold simultaneously • Mutual exclusion • At least one resource must be held in a nonsharable mode • Hold and wait • Hold at least one resource and wait to acquire additional resources that are currently being held by other processes • No preemption • Resources cannot be preempted • Circular wait • 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 Pn is waiting for a resource that is held by P0. Chapter 7 Deadlocks
7.2.2 Resource-Allocation Graph • System resource-allocation graph • A directed graph for describing deadlocks • A set of vertices V • Active processes • P = {P1, P2, …, Pn} • Resource types • R = {R1, R2, …, Rm} • A set of edges E • A request edge • Pi -> Rj • An assignment edge • Rj -> Pi 4 instances Pi Rj Pi Rj Chapter 7 Deadlocks
Resource-Allocation Graph • Sets P, R, and E • P = {P1, P2, P3} • R = {R1, R2, R3 , R4} • E = {P1 ->R1, P2 ->R3, R1 ->P2, R2 ->P2,R2 ->P1, R3 ->P3 } • Resource instances • R1 : one instance • R2 : two instances • R3 : one instance • R4 : three instances • Process states Chapter 7 Deadlocks
Resource-Allocation Graph • If the graph contains no cycles • No process in the system is deadlocked • If the graph does contain a cycle • A deadlock may exist Chapter 7 Deadlocks
Resource-Allocation Graphwith a Deadlock • P3 requests an instance of R2 • Two minimal cycles exist • P1 -> R1 -> P2 -> R3 -> P3 -> R2 -> P1 • P2 -> R3 -> P3 -> R2 -> P2 • Processes P1, P2, and P3 are deadlocked Chapter 7 Deadlocks
Resource-Allocation Graphwithout a Deadlock • A cycle exists • P1 -> R1 -> P3 -> R2 -> P1 • No deadlock • P4 may release its instance of R2 • That resource can the be allocated to P3, breaking the cycle Chapter 7 Deadlocks
Deadlock Characterization • In summary, if a resource-allocation graph • Does not have a cycle • The system is not in a deadlock state • Have a cycle • The system may or may not be in a deadlock state Chapter 7 Deadlocks
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Summary Exercises Chapter 7 Deadlocks Chapter 7 Deadlocks
7.3 Methods for Handling Deadlocks • Deal with the deadlock problem in one of three ways • Use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock state • Allow the system to enter a deadlock state, detect it, and recover. • Ignore the problem altogether, and pretend that deadlocks never occur in the system • Deadlocks occurs infrequently (say, once per year) Chapter 7 Deadlocks
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Summary Exercises Chapter 7 Deadlocks Chapter 7 Deadlocks
7.4 Deadlock Prevention • Ensure at least one of the four necessary conditions cannot hold • Mutual Exclusion • Hold and Wait • No Preemption • Circular Wait Chapter 7 Deadlocks
Deadlock Prevention • Mutual Exclusion • Must hold for nonsharable resources ( ex. a printer) • We cannot prevent deadlocks by denying the mutual-exclusion condition • Some resources are intrinsically nonsharable • Hold and Wait • Whenever a process requests a resource, it does not hold any other resources • Request and be allocated all resources before execution • Request resources only when the process has none • Main disadvantages • Low resource utilization • Starvation is possible Chapter 7 Deadlocks
Deadlock Prevention • No Preemption • If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are preempted / released. • Preempted resources are added to the list of resources for which the process is waiting. • Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. • Applied to resources whose state can be easily saved and restored • Such as CPU registers and memory spaces • Not applied to printers and tape drives Chapter 7 Deadlocks
Deadlock Prevention • Circular Wait • Impose a total ordering of all resource types • A one-to-one function F: R -> N • N is the set of natural numbers • Require that each process requests resources in an increasing order of enumeration • Initially request any number of instances of a resource type Ri • Request instances of resource type Rj if and only if F(Rj)>F(Ri) • Example • F(tape drive) = 1 • F(disk drive) = 5 • F(printer) = 12 Chapter 7 Deadlocks
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Summary Exercises Chapter 7 Deadlocks Chapter 7 Deadlocks
7.5 Deadlock Avoidance • Possible side effects of preventing deadlocks • Low device utilization • Reduced system throughput • Avoid deadlock • Require additional information about how resources are to be requested • Simplest and most useful model • Each process declare the maximum number of resources of each type that it may need • Possible to ensure that the system will never enter a deadlock state Chapter 7 Deadlocks
7.5.1 Safe State • If the system can allocate resources to each process in some order and still avoid a deadlock • A safe sequence <P1, P2, …, Pn> • for each Pi , the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj , with j < i. • If Pi resource needs are not immediately available, then Pi can wait until all Pj have 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. • Unsafe : no such sequence exists Chapter 7 Deadlocks
Basic Facts • If a system is in safe state • no deadlocks • If a system is in unsafe state • possibility of deadlock. • Avoidance • ensure that a system will never enter an unsafe state. Chapter 7 Deadlocks
An Example • A system with 12 magnetic tape drives and 3 processes: P0, P1, and P2 • The sequence <P1, P0, P2> satisfies the safety condition. Chapter 7 Deadlocks
An Example • Process P2 requests and is allocated 1 more tape drive • The system is no longer in a safe state. • If a process requests a resource that is currently available, it may still have to wait. Chapter 7 Deadlocks
7.5.2 Resource-Allocation Graph Algorithm • Resource-allocation graph with only one instance of each resource type • Used for deadlock avoidance • Claim edgePi ----> Rj • Process Pi may request resource Rj at some time in the future • Convert to request edge when a process requests a resource. • When a resource is released by a process, assignment edge reconverts to a claim edge. • Resources must be claimed a priori in the system. Chapter 7 Deadlocks
Resource-Allocation GraphFor Deadlock Avoidance Chapter 7 Deadlocks
Unsafe State InResource-Allocation Graph Chapter 7 Deadlocks
7.5.3 Banker’s Algorithm • Multiple instances • resource-allocation graph allocation is not applicable • A new process must declare the maximum number of instances of each resource type that it may need. • Cannot exceed the total number of resources in the system • Determine whether the allocation of these resources will leave the system in a safe state • If yes, the resources are allocated • If not, the process must wait until some other process releases enough resources Chapter 7 Deadlocks
Data Structuresfor the Banker’s Algorithm Let n = number of processes, andm = number of resources types. • Available: Vector of length m. If Available[ j ] = k, there are k instances of resource type Rjavailable. • Max: n x m matrix. If Max [ i, j ] = k, then process Pimay request at most k instances of resource type Rj. • Allocation: n x m matrix. If Allocation[ i, j ] = k then Pi is currently allocated k instances of Rj. • Need: n x m matrix. If Need[ i, j ] = k, then Pi may need k more instances of Rjto complete its task. • Need [ i, j ] = Max[ i, j ] –Allocation [ i, j ]. Chapter 7 Deadlocks
Notationfor the Banker’s Algorithm • Let X and Y be vectors of length n. • X ≤ Y if and only if X[ i ] ≤ Y[ i ]for all i =1,2, …, n • For example • if X = (1, 7, 3, 2) and Y = (0, 3, 2, 1),then Y ≤ X Chapter 7 Deadlocks
7.5.3.1 Safety Algorithm • Find out whether or not the system is in a safe state • Let Work and Finish be vectors of length m and n, respectively. Initialize: Work := Available Finish [ i ] := false for i = 1, 2, 3, …, n. • Find an i such that both: • Finish [ i ] = false • 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. Chapter 7 Deadlocks
7.5.3.2 Resource-Request Algorithm • Requesti [ j ] = k • Process Pi wants k instances of resource type Rj. • If Requesti≤ Needi, go to step 2. Otherwise, raise an error condition, since the process has exceeded its maximum claim. • If Requesti≤Available, go to step 3. Otherwise Pi must wait, since the resources are not available. • Pretend to allocate requested resources to Pi by modifying the state as follows: Available := Available – Requesti ; Allocationi := Allocationi + Requesti ; Needi := Needi – Requesti ; • If safe -> the resources are allocated to Pi . • If unsafe -> Pi must wait, and the old resource-allocation state is restored. Chapter 7 Deadlocks
7.5.3.3 An Illustrative Example • Three resource types • A : 10 instances • B : 5 instances • C : 7 instances • Five processes • P0 through P4 • Snapshot at time T0: Allocation M a x Available Need A B C A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 7 4 3 P1 2 0 0 3 2 2 1 2 2 P2 3 0 2 9 0 2 6 0 0 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 • <P1, P3, P4, P2, P0> satisfies the safety criteria. Chapter 7 Deadlocks
7.5.3.3 An Illustrative Example • P1 requests • One instance of resource type A • Two instances of resource type C • Request1 = (1, 0, 2) ≤ Available (3, 3, 2) • Snapshot at time T0: AllocationNeed Available A B C A B C A B C P0 0 1 0 7 4 3 2 3 0 P1 3 0 20 2 0 P2 3 0 2 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1 • <P1, P3, P4, P0, P2> satisfies the safety criteria. • Grant the request of process P1. Chapter 7 Deadlocks
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Summary Exercises Chapter 7 Deadlocks Chapter 7 Deadlocks
Be skipped Chapter 7 Deadlocks
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Summary Exercises Chapter 7 Deadlocks Chapter 7 Deadlocks
Summary • P.259 Chapter 7 Deadlocks
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock Summary Exercises Chapter 7 Deadlocks Chapter 7 Deadlocks
Exercises • 7.8 Chapter 7 Deadlocks