System Model Resource types R 1 1, R 2 2,..., R m CPU cycles, memory space, files, I/O devices Each resource type R i has W i instances. Each process utilizes a resource as follows: request use e.g., request device, open file, allocate memory release e.g., release device, close file, free memory This sequence should always be followed. 7.7 Silberschatz, Galvin and Gagne 2009
Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. Necessary conditions for Deadlock are followings. 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 a process that is holding it, after the process has completed its task. Circular wait: there exists a set {P 0, P 1,, P n } 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 t is held by P 2, P n 1 is waiting for a resource that is held by P n, and P n is waiting for a resource that is held by P 0. 7.8 Silberschatz, Galvin and Gagne 2009
Deadlock Characterization This is a Necessary Condition e.g., not all accesses of mutual exclusion result in deadlock, but in case of deadlock, mutual exclusion happens. e.g., not all circular wait results in deadlock, but in case of deadlock, circular wait happens. However, when deadlock happens, these four conditions are satisfied simultaneously. 7.9 Silberschatz, Galvin and Gagne 2009
Resource-Allocation Graph A set of vertices V and a set of edges E. V is partitioned into two types: P = {P 1, P 2,, P n }, the set consisting of all the processes in the system. R = {R 1 1, R 2 2,, R m m}, the set consisting of all resources types in the system. t d di t d d P R request edge directed edge P i R j assignment edge directed edge R j P i 7.10 Silberschatz, Galvin and Gagne 2009
Resource-Allocation Graph (Cont.) Process Resource Type with 4 instances P i requests instance of R j P i Request edge P i is holding an instance of R j R j P i Assignment edge R j 7.11 Silberschatz, Galvin and Gagne 2009
Example of a Resource Allocation Graph 7.12 Silberschatz, Galvin and Gagne 2009
Resource Allocation Graph With A Deadlock 2 cycles 7.13 Silberschatz, Galvin and Gagne 2009
Resource Allocation Graph With A Cycle But No Deadlock Observations After P4 release the resource R2, P3 can obtain resource R2. : No cycles No deadlock : Cycles may have deadlock i.e., Cycle in graph is necessary condition, not sufficient condition, for deadlock. 7.14 Silberschatz, Galvin and Gagne 2009
Basic Facts If graph contains no cycles no deadlock. If graph contains a cycle if only one instance per resource type, then deadlock. if several instances per resource type, the possibility of deadlock. 7.15 Silberschatz, Galvin and Gagne 2009
Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state. Allow the system to enter a deadlock state and then recover. Ignore the deadlock and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. It is then up to the application developer to write the program that handle deadlocks. 7.16 Silberschatz, Galvin and Gagne 2009
Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection and Recovery 7.17 Silberschatz, Galvin and Gagne 2009
Deadlock Prevention Ensure that at lease one of the necessary conditions is false at all time. Mutual Exclusion This condition is inherently not allowed for nonsharable resources Mutual exclusion must hold at all times e.g., printer can not be simultaneously shared by several processes. i.e., must always hold this condition in case of nonsharable resources. cf. sharable resource all requestor can share (request) so no deadlock at all. 7.18 Silberschatz, Galvin and Gagne 2009
Deadlock Prevention Ensure that at lease one of the necessary conditions is false at all time. Hold and Wait must guarantee that whenever a process requests a resource, it does not hold any other resources. Method 1 Only before execution, process can request and be allocated its resources in one shot method execution (only before starting) i.e., after all resources are obtained, the process can execute its very first start. Method 2 Method 1 is too much inefficient because of before execution Allow process to request resources only when the process has no resource at all (even in the middle of executing). Problems Low resource utilization Starvation possible. 7.19 Silberschatz, Galvin and Gagne 2009
Deadlock Prevention (Cont.) Ensure that at lease one of the necessary conditions is false at all time. No Preemption If a process, that is holding some resources, requests another resource that cannot be immediately allocated to it, then all currently allocated resources are released. That is, preempted. 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. i.e., re-request all its resources again and re-start. 7.20 Silberschatz, Galvin and Gagne 2009
Deadlock Prevention (Cont.) Ensure that at lease one of the necessary conditions is false at all time. Circular Wait impose a total ordering into all resource types, and each process requests its resources in an increasing order of enumeration. Impose the sequence of resource request e.g., F(card drive) = 1, F(disk drive) = 5, F(tape drive) = 7, F(printer drive) = 12 If Ri can be requested, then new resource Rj can only be requested F(Rj) > F(Ri). Proof by contradiction In this total ordering, there is no circular wait. But let s assume that there exists circular wait. Then, F(r0) < F(r1)< < F(rn) < F(r0). But F(r0) < F(r0) is wrong because of wrong assumption. So there is no circular wait. 7.21 Silberschatz, Galvin and Gagne 2009
Deadlock Prevention (Cont.) Ensure that at lease one of the necessary conditions is false at all time. E.g., dining philosophers problem Hold and wait : picking up chopsticks only if both chopsticks are available No Preemption : if other chopstick is not available, release chopsticks. Circular wait : chopstick ordering 1 to 5, with requesting 1-2, 2-3, 3-4, 4-5, 1-5 7.22 Silberschatz, Galvin and Gagne 2009
Deadlock Avoidance Deadlock Prevention is implemented by imposing Restriction on Resource Request. Although cycle exists, no deadlock is possible. Therefore, low device utilization and low system throughput. Possibility of starvation ti Deadlock Avoidance requires that the system has some additional priori information With the knowledge of the complete sequence of requests and releases for each process, system can avoid a possible future deadlock Deadlock avoidance allows deadlock necessary conditions but it avoid properly. Thus avoidance allows better resource utilization than prevention. 7.23 Silberschatz, Galvin and Gagne 2009