2Deadlocks• System Model•Deadlock Characterization• Methods for Handling Deadlocks• Deadlock Prevention• Deadlock Avoidance• Deadlock Detection• Recovery from Deadlock• Combined Approach to Deadlock Handling
3.
3The Deadlock Problem•A set of blocked processes each holding a resource and waiting toacquire a resource held by another process in the set.• Example: Suppose that process A asks for CD-ROM drive and gets it.A moment later, process B asks for the flatbed plotter and gets it, too.Now process A asks for the plotter and blocks waiting for it. Finallyprocess B asks for CD-ROM drive and also blocks. This situation iscalled a deadlock.
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.4
5.
5Resources• A resourceis anything that must be acquired, used, and releasedover the course of time• Resource types R1, R2, . . ., RmCPU cycles, memory space, I/O devices• Each resource type Ri has Wi instances.• A resource is anything that can only be used by a single process atany instance (at any amount).• Each process utilizes a resource as follows: request use release
6.
6Preemptable and Non-preemptableResourcesPreemptableresource Can be taken away from a process Example : MemoryNon-preemptable resource Cannot be taken away from its current owner without causingthe computation to fail. Example : CD-ROM• Deadlock involves non-preemptable resources.
7.
7What is Deadlock?•A Deadlock is a situation where a group of processes are permanentlyblocked as a result of each process having acquired a subset of theresources needed for its completion and waiting for release of theremaining resource held by others in the same group.• Deadlock occurs when two or more processes are each waiting for anevent that will never occur, since it can only be generated by anotherprocess in that set.• A set of blocked processes each holding a resource and waiting toacquire a resource held by another process in the set.
8.
8Deadlock Characterization• Deadlockcan arise if four conditions hold simultaneously. Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waitingto acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily bythe process holding it, after that process has completed its task. Circular wait: there exists a set {P0, P1, …, P0} of waitingprocesses such that P0 is waiting for a resource that is held byP1, P1 is waiting for a resource that is held by P2, …, Pn–1 iswaiting for a resource that is held by Pn, and P0 is waiting for aresource that is held by P0.
9.
9Deadlock Modeling: Resource-AllocationGraph•Two kinds of nodes: Squares— Represent resources –Instances of the resource are representedas dots within the box. Circles — Represent threads / processes.• Two kinds of (directed) edges: Request edge — from process to resource — indicates the process hasrequested the resource, and is waiting to acquire it. Assignment edge —from resource-instance to process— indicates theProcess is holding the resource instance. When a request is made, a request edge is added. When request is fulfilled, the request edge is transformed into anassignment edge. When process releases the resource, the assignment edge is deleted.
15Basic Facts• Ifgraph 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.
16.
16Methods for HandlingDeadlocks• Four strategies are used for dealing with deadlocks.1. Just ignore the problem.2. Detection and recovery3. Dynamic avoidance4. Prevention
17.
17The Ostrich AlgorithmThesimplest approach is the ostrich algorithm: stick yourhead in the sand and pretend (assume) there is no problem
18.
18Deadlock Detection andRecovery• Allow system to enter deadlock state• Detection algorithm• Recovery scheme
19.
19Deadlock Detection withOne Resource ofEach Type• For such a system, we can construct a resource graph.• If this graph contains one or more cycles, a deadlock exists.• Any process that is part of a cycle is deadlocked.• If no cycles exist, the system is not deadlocked.
20.
20Deadlock Detection withOne Resource ofEach Type• Example: consider a system with seven processes, A through G, and sixresources, R through W. The state of which resources are currentlyowned and which ones are currently being requested is as follows:1. Process A holds R and wants S.2. Process B holds nothing but wants T.3. Process C holds nothing but wants S.4. Process D holds U and wants S and T.5. Process E holds T and wants V.6. Process F holds W and wants S.7. Process G holds V and wants U.•Q: Is this system deadlocked? And if so, which processes are involved?
22Recovery from DeadlockRecoverythrough Preemption• Select a victim• Suspend that process (Mark it as not runnable).• Take a resource away from the process and give it to another process.• Restart the suspended process.• Recovering this way is frequently difficult or impossible.• Choosing the process to suspend depends largely on which ones haveresources that can easily be taken back.
23.
23Recovery from DeadlockRecoverythrough Rollback• Having processes check-pointed (a place where actions were stoped)periodically.• Check-pointing a process means that its state is written to a file sothat it can be restarted later.• The checkpoint contains not only the memory image, but also theresource state, in other words, which resources are currentlyassigned to the process.• To do the recovery, a process that owns a needed resource is rolledback to a point in time before it acquired that resource by starting atone of its earlier checkpoints.
24.
24Recovery from DeadlockRecoverythrough Killing Processes• Abort(end) one process at a time until the deadlock cycle is eliminated.• If this does not help, it can be repeated until the cycle is broken.• Alternatively, a process not in the cycle can be chosen as the victim in orderto release its resources.• In which order should we choose to abort? Priority of the process. How long process has computed, and how much longer to completion. Resources the process has used. Resources process needs to complete. How many processes will need to be terminated. Is process interactive or batch?
25.
25Deadlock Avoidance• Requiresthat the system has some additional a priori informationavailable. Simplest and most useful model requires that each processdeclare the maximum number of resources of each type that itmay need. The deadlock-avoidance algorithm dynamically examines theresource-allocation state to ensure that there can never be acircular-wait condition. Resource-allocation state is defined by the number of availableand allocated resources, and the maximum demands of theprocesses.
26.
26Safe State• Whena process requests an available resource, system must decide ifimmediate allocation leaves the system in a safe state.• System is in safe state if there exists a safe sequence of all processes.• Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pican still request can be satisfied by currently available resources +resources held by all the Pj, with j<I. If Pi resource needs are not immediately available, then Pi canwait until all Pj have finished. When Pj is finished, Pi can obtain needed resources, execute,return allocated resources, and terminate. When Pi terminates, Pi+1 can obtain its needed resources, andso on.
27.
27Basic Facts• Ifa 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.⇒
29Deadlock PreventionRestrain theways request can be made• Mutual Exclusion – not required for sharable resources; must holdfor non-sharable resources.• Hold and Wait – must guarantee that whenever a process requests aresource, it does not hold any other resources. Require process to request and be allocated all its resourcesbefore it begins execution, or allow process to requestresources only when the process has none. Low resource utilization; starvation possible.
30.
30Deadlock Prevention (Cont.)•No Preemption – If a process that is holding some resources requestsanother resource that cannot be immediately allocated to it, then allresources currently being held are released. Preempted resources are added to the list of resources forwhich the process is waiting. Process will be restarted only when it can regain its oldresources, as well as the new ones that it is requesting.• Circular Wait – impose a total ordering of all resource types, andrequire that each process requests resources in an increasing order ofenumeration.
31.
31Combined Approach toDeadlock HandlingPrevention (Cont.)• Combine the three basic approaches prevention avoidance detectionallowing the use of the optimal approach for each of resources in thesystem.• Partition resources into hierarchically ordered classes.• Use most appropriate technique for handling deadlocks within eachclass.