Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Operating System Fall 2009

Similar presentations


Presentation on theme: "Advanced Operating System Fall 2009"— Presentation transcript:

1 Advanced Operating System Fall 2009
Lecture 9: Deadlocks Advanced Operating System Fall 2009

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

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 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 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 Example 1 P Q 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 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 Consumable Resources One that can be created and destroyed Example:
Message passed among processes

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 {P0, P1, …, P0} of waiting processes such that 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 P0 is waiting for a resource that is held by P0.

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

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 Is this example a safe state?
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 n=4, m=3 4 processes, 3 resources Total amount of each resource in the system 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. Total amount of each resource not allocated to a process Now P1 can proceed. Requirement of each process of each resource Now P3 can proceed. Now P4 can proceed. It is a safe sate. Current allocation

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 Banker’s Algorithm Checking to see if a state is safe or not
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.

19 Example 1 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. Total amount of each resource in the system 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. Is this a safe state? Yes. So the OS will grant this request.

20 Example 2 Suppose P3 asks for one unit of R3. Should the OS grants the request? Total amount of each resource in the system 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. Is this a safe state? No. So the OS will block this request.

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

23 Example 1 w=(0,1,1) P1’s request  w w=(0,1,1)+(1,0,0)=(1,1,1)
Is there a deadlock? 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 Example 2 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. Is there a deadlock?

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

26 Example for method 2 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. Cost of aborting P1 = c1 = 3 Cost of aborting P2 = c2 = 9 Cost of aborting P3 = c3 = 4 The problem is NP-Hard.

27 End of lecture 9 Thank you!


Download ppt "Advanced Operating System Fall 2009"

Similar presentations


Ads by Google