


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Operating Systems [CS-604] Lecture No. 27
Chapter 8 of the textbook Lecture 27 on Virtual TV
Deadlock handling Deadlock prevention Deadlock avoidance
We can deal with deadlocks in a number of ways: Ensure that the system will never enter a deadlock state. Allow the system to enter a deadlock state and then recover from deadlock. Ignore the problem and pretend that deadlocks never occur in the system. These three ways result in the following general methods of handling deadlocks:
1. Deadlock prevention: is a set of methods for ensuring that at least one of the necessary conditions cannot hold. These methods prevent deadlocks by constraining how processes can request for resources. 2. Deadlock Avoidance: This method of handling deadlocks requires that processes give advance additional information concerning which resources they will request and use during their lifetimes. With this information, it may be decided whether a process should wait or not. 3. Allowing Deadlocks and Recovering: One method is to allow the system to enter a deadlocked state, detect it, and recover.
By ensuring that one of the four necessary conditions for a deadlock does not occur, we may prevent a deadlock.
Mutual exclusion The mutual exclusion condition must hold for non-sharable resources, e.g., printer. Sharable resources do not require mutually exclusive access and thus cannot be involved in a deadlock, e.g., read-only files. Also, resources whose states can be saved and restored can be shared, such as a CPU. In general, we cannot prevent deadlocks by denying the mutual exclusion condition, as some resources are intrinsically non-sharable.
Hold and Wait To ensure that the hold and wait condition does not occur in a system, we must guarantee that whenever a process requests a resource, it does not hold any other resources. One protocol that can be used requires each process to request and be allocated all its
resources before it begins execution. We can implement this provision by requiring that system calls requesting resources for a process precede all other system calls. An alternative protocol requires a process to request resources only when it has none. A process may request some resources and use them. But it must release these before requesting more resources. The two main disadvantages of these protocols are: firstly, resource utilization may be low, since many resources may be allocated but unused for a long time. Secondly, starvation is possible. A process that needs several popular resources may have to wait indefinitely, because at least one of the resources that it needs is always allocated to some other process.
No preemption To ensure that this condition does not hold we may use the protocol: if a process is holding some resources and requests another that cannot be allocated immediately to it, then all resources currently being held by the process are preempted. These resources are implicitly released, and added to the list of resources for which the process is waiting. The process will be restarted when it gets all its old, as well as the newly requested resources.
Circular Wait One way to ensure that this condition never holds is to impose a total ordering of all resource types, and to require that each process requests resources in an increasing ordering of enumeration. Let R={ R 1 , R 2 , R 3 }be resource types. We assign to each a unique integer, which allows us to compare two resources and to determine whether one precedes another in our ordering. For example, if the set of resource types R includes tape drivers, disk drives, and printers then the function F: RN might be used to assign positive integers to these resources as follows:
F(tape drive) = F(disk drive) = F(printer)=
Each process can request resources in an increasing order of enumeration. For example, a process wanting to use the tape and the disk drive must first request the tape drive and then the disk drive. We can prove that if processes use this protocol then circular wait can never occur. We will prove this by contradiction. Letโs assume that there is a cycle involving process P0 through Pk and that Pi is holding an instance of Ri , as shown below. The proof follows.
P0 P1 P2 โฆ Pk P R0 R1 R2 Rk R
F(R0) < F(R1) < โฆ < F(Rk) < F(R0)
F(R0) < F(R0), which is impossible There can be no circular wait.
Letโ consider the following example to explain how a deadlock avoidance algorithm works. There is a system with 12 tape drives and three processes. The current system state is as shown in the following table. The available column shows that initially there are three tapes drives available and when process P1 finishes, the two rape drives allocated to it are returned, making the total number of tape drives 5. With 5 available tape drives, the maximum remaining future needs of P0 (of 5 tape drives) can be met. Once this happens, the 5 tape drives that P0 currently holds will go back to the available pool of drives, making the grand total of available tape drives 10. With 10 available drives, the maximum future need of P2 of 7 drives can be met. Therefore, system is currently in a safe state, with the safe sequence <P1, P0, P2>.
Process Max Need Allocated Available P0 10 5 3 P1 4 2 5 P2 9 2 10
Now, consider that P2 requests and is allocated one more tape drive. Assuming that the tape drive is allocated to P2, the new system state will be:
Process Max Need Allocated Available
P0^10 5 2
P1 4 2 4 P2 9 3
This new system is not safe. With two tape drives available, P1โs maximum remaining future need can be satisfied which would increase the number of available tapes to 4. With 4 tapes available, neither P0โs nor P2โs maximum future needs can be satisfied. This means that if P2 request for an additional tape drive is satisfied, it would the system in an unsafe state. Thus, P2โs request should be denied at this time.
Resource Allocation Graph Algorithm In addition to the request and assignment edges explained in the previous lectures, we introduce a new type of edge called a claim edge to resource allocation graphs. A claim edge Pi Rj indicates that process P (^) i may request resource Rj at some time in the future. A dashed line is used to represent a claim edge. When Pi requests resource Rj the claim edge is converted to a request edge. Suppose that Pi requests resource Rj. The request can be granted only if converting the request edge Pi Rj into an assignment edge Rj Pi does not result in the formation of a cycle. If no cycle exists, then the allocation of the resource will leave the system in a safe state. If a cycle is found, then the allocation will put the system in an unsafe state.