210 likes | 474 Views
Chapter 7: Deadlocks. Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock. What is a Deadlock?.
E N D
Chapter 7: Deadlocks • The Deadlock Problem • System Model • Deadlock Characterization • Methods for Handling Deadlocks • Deadlock Prevention • Deadlock Avoidance • Deadlock Detection • Recovery from Deadlock
What is a Deadlock? • A deadlock is impossibility to change state of a waiting process because the resources it has requested are held by other waiting processes. • It is very desirable to prevent deadlocks • If they can occur, we need to take care of their handling through an operating system
The Deadlock Problem • A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. • Example • System has 2 disk drives. • P1 and P2 each hold one disk drive and each needs another one.
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.
System Model • Resource typesR1, R2, . . ., Rm CPU cycles, memory space, I/O devices • Each resource type Ri has Wi instances (sectors on the hard disc, pages in the main memory, multiple CPUs, etc.) • The request and release of resources are system calls ( request(), release () device; open(), close (file) file; allocate(), free() memory, etc.) • Each process utilizes a resource as follows: • Request (followed by the allocation of the resource) • use • release
Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. • Mutual exclusion: only one process at a time can use a resource. If another process requests that resource, the requesting process must be delayed until the resource has been released. • 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. • 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 Pn is waiting for a resource that is held by P0.
Resource-Allocation Graph A set of vertices V and a set of edges E. • V is partitioned into two types: • P = {P1, P2, …, Pn}, the set consisting of all the processes in the system. • R = {R1, R2, …, Rm}, the set consisting of all resource types in the system. • request edge – directed edge Pi Rj • assignment edge – directed edge Rj Pi
Resource-Allocation Graph (Cont.) • Process • Resource Type with 4 instances • Pirequests instance of Rj - request edge • Pi is holding an instance of Rj – assignment edge Pi Rj Pi Rj
The Deadlock Condition • It can be shown that if the graph contains no cycles, then no process in the system is deadlocked. • If the graph does contain a cycle, then a deadlockmay exist.
Resource Allocation Graph With A Deadlock Cycles: P1R1P2R3P3R2P1 P2R3P3R2P2 Processes P1, P2 and P3 are deadlocked: P2 is waiting for R3, which is held by P3 P3 is waiting for either P1 or P2 to release R2 P1 is waiting for P2 to release R1
Graph With A Cycle But No Deadlock Cycle: P1R1P3R2P1 There is nodeadlock: P4 may release its instance on resource type R2. That resource can then be allocated to P3, breaking the cycle.
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, possibility of deadlock.
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 problem and pretend that deadlocks never occur in the system.
Methods for Handling Deadlocks • If a system does not employ either a deadlock-avoidance or deadlock-prevention algorithm, then a deadlock situation may arise. • If a system neither ensures that a deadlock will never occur nor provides a mechanism for deadlock detection and recovery, then we may arrive at a situation where the system is in a deadlocked state and there is no way of recognizing what has happened.
Deadlock Methods for Handling Deadlocks • Deadlock prevention means that a system has to ensure that at least one of the four deadlock conditions cannot hold. • Deadlock avoidance requires that the operating system be given in advance additional information concerning which resources a process will request and use during its lifetime. • To decide whether the current request can be satisfied or must be delayed, the system must consider the resources currently available, the resources currently allocated to each process, and the future requests and releases of each process.
Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. • Mutual exclusion: only one process at a time can use a resource. If another process requests that resource, the requesting process must be delayed until the resource has been released. • 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. • 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 Pn is waiting for a resource that is held by P0.
Deadlock Prevention • Mutual Exclusion– not required for sharable resources(a read-only file is a typical example of a sharable resource), a process never needs to wait for a sharable resource; must hold for nonsharable resources (a printer is a typical example of such a resource). In general, this condition can not be denied. • Hold and Wait– must guarantee that whenever a process requests a resource, it does not hold any other resources. • Require a process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. • Low resource utilization; starvation possible.
Deadlock Prevention (Cont.) • No Preemption– • If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. • 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. • Circular Wait– impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.
Deadlock Avoidance Requires that the system has some additional a priori information available. • Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. • The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. • Resource-allocation stateis defined by the number of available and allocated resources, and the maximum demands of the processes.