Lecture 7 Deadlocks Operating System Fall 2006 1

  • Slides: 27
Download presentation
Lecture 7: Deadlocks Operating System Fall 2006 1

Lecture 7: Deadlocks Operating System Fall 2006 1

Contents n n n What is deadlock Conditions for deadlock Methods for Handling Deadlocks

Contents n n n What is deadlock Conditions for deadlock Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection and Recovery 2

What is deadlock? n n A set of blocked processes each holding a resource

What is deadlock? n n A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Example: Process P … request A … request B … release A … release B Process Q … request B … request A … release B … release A 3

Bridge Crossing Example n n n Traffic only in one direction. Each section of

Bridge Crossing Example n n n 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. 4

Reusable Resources n n n One that can be safely used by only one

Reusable Resources n n n One that can be safely used by only one process at a time and is not depleted by that use. Serially Reusable Example: n n n n Memory (main & secondary) Processors I/O channels Semaphores Files Databases Printer etc. 5

Example 1 P Request disk drive Lock disk drive Request tape drive Lock tape

Example 1 P Request disk drive Lock disk drive Request tape drive Lock tape drive … perform function Unlock disk drive Unlock tape drive Q Request tape drive Lock tape drive Request disk drive Lock disk drive … perform function Unlock tape drive Unlock disk drive 6

Example 2 n Suppose we have a pool of 200 kbytes of memory for

Example 2 n Suppose we have a pool of 200 kbytes of memory for allocation: P … Request 80 Kbytes … Request 60 Kbytes Q … Request 70 Kbytes … Request 80 Kbytes 7

Consumable Resources n n One that can be created and destroyed Example: n Message

Consumable Resources n n One that can be created and destroyed Example: n Message passed among processes 8

Conditions for deadlock n n Mutual exclusion: only one process at a time can

Conditions for deadlock n n 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. No resources can be forcibly removed from a process holding it. Circular wait: there exists a set {P 0, P 1, …, P 0} 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, …, Pn– 1 is waiting for a resource that is held by Pn, and P 0 is waiting for a resource that is held by P 0. 9

Methods for Handling Deadlocks n n Deadlock prevention: Design policies so that deadlock can’t

Methods for Handling Deadlocks n n Deadlock prevention: Design policies so that deadlock can’t occur. Deadlock avoidance: The OS will monitor the progress of processes so that deadlock can always be avoided. Deadlock detection and recovery: Do nothing and allow deadlock occur, if deadlock are detected, then recover at a minimal cost. Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. 10

Deadlock Prevention n Design policies so that deadlock can’t occur: n n Mutual Exclusion

Deadlock Prevention n Design policies so that deadlock can’t occur: n n Mutual Exclusion – usually cannot be disallowed Hold and wait n n No preemption – preemption is allowed. n n require each process to release all resources held before making a new request. a process may request resources ahead of time. Drawback: inefficient utilization of resources. A good example is the example of the memory pool. If we get into a deadlock, move the allocated memory to secondary memory (preemption) and restore later. Circular Wait – Assign a linear ordering of resource types. n n R 1<R 2<R 3… If a process has been allocated resources of type i (Ri), then it may request resources of type higher than i. If it wants resources of type lower than i, then it must release resources of type i. 11

Deadlock Avoidance n When a process requests resources, a decision is made whether the

Deadlock Avoidance n When a process requests resources, a decision is made whether the current request will, if granted, potentially lead to a deadlock. Deadlock avoidance thus requires knowledge of future processes resource request. n n Do not start a process if its demands might lead to deadlock Do not grant an incremental resource request to a process if this allocation might lead to deadlock. 12

Deadlock Avoidance(cont. ) n Resource-allocation state is defined by the number of available and

Deadlock Avoidance(cont. ) n Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. Total amount of each resource in the system Total amount of each resource not allocated to a process Requirement of each process of each resource (declared by the processes ahead of time) Current allocation 13

Example n=4, m=3 4 processes, 3 resources in the system Total amount of each

Example n=4, m=3 4 processes, 3 resources in the system Total amount of each resource not allocated to a process Requirement of each process of each resource (declared by the processes ahead of time) Current allocation 14

Safe State n n n The state of the system is simply the current

Safe State n n n The state of the system is simply the current allocation of resources to processes. A safe state is one in which there is at least one sequence that does not result in a deadlock An unsafe state is a state that is not safe. 15

Is this example a safe state? n=4, m=3 4 processes, 3 resources Total amount

Is this example a safe state? n=4, m=3 4 processes, 3 resources Total amount of each resource in the system Total amount of each resource not allocated to a process P 1 cannot proceed. In the worst case, P 1 will request 2 units of R 1, 2 units of R 2 and 2 units of R 3. Impossible to satisfy P 2 can proceed. In the worst case, P 2 will request 1 more units of R 3. Possible to satisfy. When it completes, it will release all resources. Now P 1 can proceed. Requirement of each process of each resource Current allocation Now P 3 can proceed. Now P 4 can proceed. It is a safe sate. 16

Basic Facts n n If a system is in safe state no deadlocks. If

Basic Facts n n 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. n n In deadlock avoidance, each time a process requests resource, the OS will determine if it will, after granting the resources to the process, lead it to an unsafe state. At the beginning, the system is in a safe sate, at each step, the system is always in a safe state. Avoidance algorithm: Banker’s Algorithm 17

Banker’s Algorithm Checking to see if a state is safe or not Algorithm: n

Banker’s Algorithm Checking to see if a state is safe or not Algorithm: n n 1. 2. 3. 4. Find a process Pk such that Claim(k, *)Allocation(k, *) Available(*) for *. If not found, go to 4. Available(*) <- Available(*)+Allocation(k, *) for *. Goto 1 If there is no process left, the state is safe. Otherwise, it is not safe. 18

Example 1 n=4, m=3 4 processes, 3 resources Total amount of each resource in

Example 1 n=4, m=3 4 processes, 3 resources Total amount of each resource in the system Suppose P 1 makes a request of 1 unit of R 2. Should the OS Grants the request? Well, the OS should if it lead it to a safe State. Total amount of each resource not allocated to a process Requirement of each process of each resource Current allocation Is this a safe state? Yes. This is a safe state. So the OS will grant this request. 19

Example 2 n=4, m=3 4 processes, 3 resources Suppose P 3 asks for one

Example 2 n=4, m=3 4 processes, 3 resources Suppose P 3 asks for one unit of R 3. Should the OS grants the request? Total amount of each resource in the system Total amount of each resource not allocated to a process Requirement of each process of each resource Current allocation Is this a safe state? No. This is a safe state. So the OS will block this request. 20

Deadlock Detection and Recovery n n Allow deadlocks to occur. If and when it

Deadlock Detection and Recovery n n Allow deadlocks to occur. If and when it occurs, recover from it. The system checks periodically whether it is in deadlock. If a deadlock occurs, recovery procedure will be called. 21

Deadlock Detection Algorithm: 1. Initialize a temporary vector w to equal to Available 2.

Deadlock Detection Algorithm: 1. Initialize a temporary vector w to equal to Available 2. vector. w(*)=V(*) 2. Mark each process that has a row in the Allocation matrix of all zeros. 3. Find an index k such that process k is currently 4. unmarked and the kth row of Q w. That is, 5. Q(k, *) w(*) for *. If no such row is found, 6. goto step 5 4. Mark process k and add the corresponding row of 5. the allocation matrix to w. 6. That is, w(*)=w(*)+A(k, *). Go to step 3 5. If every process is marked, then there is no deadlock. 6. Otherwise, the unmarked process are deadlocked. Are we in a deadlock state? 22

Example 1 w=(0, 1, 1) P 1’s request w w=(0, 1, 1)+(1, 0, 0)=(1,

Example 1 w=(0, 1, 1) P 1’s request w w=(0, 1, 1)+(1, 0, 0)=(1, 1, 1) P 2’s request w w=(1, 1, 1)+(6, 1, 2)=(7, 2, 3) P 3’s request w w=(7, 2, 3)+(2, 1, 1)=(9, 3, 4) P 4’s request w w=(9, 3, 4)+(0, 0, 2)=(9, 3, 6) Every process is marked. No deadlock. Is there a deadlock? 23

Example 2 w=(0, 1, 1) P 4’s request w w=(0, 1, 1)+(0, 0, 2)=(0,

Example 2 w=(0, 1, 1) P 4’s request w w=(0, 1, 1)+(0, 0, 2)=(0, 1, 3) P 1, P 2, P 3 are involved in a deadlock. Is there a deadlock? 24

Recovery n Two methods: 1. 2. Abort all deadlocked processes. Find a subset of

Recovery n Two methods: 1. 2. Abort all deadlocked processes. Find a subset of deadlocked processes with the minimum cost that can break the deadlock. 25

Example for method 2 Aborting P 2 will resolve the deadlock, the cost =

Example for method 2 Aborting P 2 will resolve the deadlock, the cost = 9 Aborting P 1 will not resolve the deadlock Aborting P 3 will resolve the deadlock, the cost = 4 P 3 is the optimal choice. Cost of aborting P 1 = c 1 = 3 Cost of aborting P 2 = c 2 = 9 Cost of aborting P 3 = c 3 = 4 The problem is NP-Hard. 26

End of lecture 7 Thank you! 27

End of lecture 7 Thank you! 27