Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 8: Deadlocks Operating System Spring 2008.

Similar presentations


Presentation on theme: "1 Lecture 8: Deadlocks Operating System Spring 2008."— Presentation transcript:

1 1 Lecture 8: Deadlocks Operating System Spring 2008

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

3 3 What is deadlock? 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

4 4 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.

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

6 6 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

7 7 Example 2 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

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

9 9 Conditions for deadlock 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, …, P n–1 is waiting for a resource that is held by P n, and P 0 is waiting for a resource that is held by P 0.

10 10 Methods for Handling Deadlocks 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.

11 11 Deadlock Prevention Design policies so that deadlock can’t occur: Mutual Exclusion – usually cannot be disallowed Hold and wait 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. No preemption – preemption is allowed. 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. R1<R2<R3… 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.

12 12 Deadlock Avoidance 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. 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.

13 13 Deadlock Avoidance(cont.) 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

14 14 Example 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 n=4, m=3 4 processes, 3 resources in the system

15 15 Safe State 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.

16 16 Is this example a safe state? 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 n=4, m=3 4 processes, 3 resources P1 cannot proceed. In the worst case, P1 will request 2 units of R1, 2 units of R2 and 2 units of R3. Impossible to satisfy P2 can proceed. In the worst case, P2 will request 1 more units of R3. Possible to satisfy. When it completes, it will release all resources. Now P1 can proceed. Now P3 can proceed. Now P4 can proceed. It is a safe sate.

17 17 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. 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

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

19 19 Example 1 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 n=4, m=3 4 processes, 3 resources This is a safe state. Suppose P1 makes a request of 1 unit of R2. Should the OS Grants the request? Well, the OS should if it lead it to a safe State. Is this a safe state? Yes. So the OS will grant this request.

20 20 Example 2 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 n=4, m=3 4 processes, 3 resources This is a safe state. Suppose P3 asks for one unit of R3. Should the OS grants the request? Is this a safe state? No. So the OS will block this request.

21 21 Deadlock Detection and Recovery 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.

22 22 Deadlock Detection Algorithm Are we in a deadlock state? Detection Algorithm: 1.Initialize a temporary vector w to equal to Available 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 unmarked and the k th row of Q  w. That is, Q(k,*)  w(*) for  *. If no such row is found, goto step 5 4.Mark process k and add the corresponding row of the allocation matrix to w. That is, w(*)=w(*)+A(k,*). Go to step 3 5.If every process is marked, then there is no deadlock. Otherwise, the unmarked process are deadlocked.

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

24 24 Example 2 Is there a deadlock? w=(0,1,1) P4’s request  w w=(0,1,1)+(0,0,2)=(0,1,3) P1,P2,P3 are involved in a deadlock.

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

26 26 Example for method 2 Cost of aborting P1 = c1 = 3 Cost of aborting P2 = c2 = 9 Cost of aborting P3 = c3 = 4 Aborting P2 will resolve the deadlock, the cost = 9 Aborting P1 will not resolve the deadlock Aborting P3 will resolve the deadlock, the cost = 4 P3 is the optimal choice. The problem is NP-Hard.

27 27 End of lecture 8 Thank you!


Download ppt "1 Lecture 8: Deadlocks Operating System Spring 2008."

Similar presentations


Ads by Google