DeadlocksChapter 33.1. Resource3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention 3.7. Other issuesIntroduction• Parallel operation among many devices driven by concurrent processes contribute significantly to high performance. But concurrency also results in contention for resources and possibility of deadlock among the vying processes.• Deadlock is a situation where a group of processes are permanently blocked waiting for the resources held by each other in the group.• Typical application where deadlock is a serious problem: Operating system, data base accesses, and distributed processing.System Model• Resource types R1, R2, . . ., RmCPU cycles, memory space, I/O devices• Each resource type Rihas Wiinstances.• Each process utilizes a resource as follows:– request –use –releaseDeadlock Characterization• 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.• 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, P1is waiting for a resource that is held by P2, …, Pn–1is waiting for a resource that is held by Pn, and P0is waiting for a resource that is held by P0.Deadlock can arise if four conditions hold simultaneously.Resource-Allocation Graph• 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 P1 → Rj• assignment edge – directed edge Rj→ PiA set of vertices V and a set of edges E.Resource-Allocation Graph (Cont.)• Process• Resource Type with 4 instances• Pirequests instance of Rj• Piis holding an instance of RjPiRjPiRjResource Allocation Graph with a DeadlockResource Allocation Graph with a cycle but No DeadlockHow deadlock occursA B CDeadlock ModelingMethods for Handling Deadlocks• Ensure that the system will never enter a deadlock state. (pessimistic)• Allow the system to enter a deadlock state and then recover. Database systems;• Ignore the problem and pretend that deadlocks never occur in the system; Older operating systems; (ostrich algorithm: optimistic)Dealing with Deadlock Strategies for dealing with Deadlocks1. just ignore the problem altogether2. detection and recovery3. dynamic avoidance • careful resource allocation4. prevention • negating one of the four necessary conditionsThe Ostrich Algorithm• Pretend there is no problem• Reasonable if – deadlocks occur very rarely – cost of prevention is high• UNIX and Windows takes this approach• It is a trade off between – convenience– correctnessDetection with One Resource of Each Type (1)• Note the resource ownership and requests• A cycle can be found within the graph, denoting deadlockRecovery from Deadlock (1)• Recovery through preemption– take a resource from some other process– depends on nature of the resource• Recovery through rollback– checkpoint a process periodically– use this saved state – restart the process if it is found deadlockedRecovery from Deadlock (2)• Recovery through killing processes– crudest but simplest way to break a deadlock– kill one of the processes in the deadlock cycle– the other processes get its resources – choose process that can be rerun from the beginningDeadlock Avoidance• 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 state is defined by the number of available and allocated resources, and the maximum demands of the processes.Requires that the system has some additional a priori information available.Safe State• When a process requests an available resource, system must decide if immediate 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 Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j<I.– If Piresource needs are not immediately available, then Pican wait until all Pjhave finished.– When Pjis finished, Pican obtain needed resources, execute, return allocated resources, and terminate. – When Piterminates, Pi+1can obtain its needed resources, and so on.Safe, Unsafe , Deadlock StateResource-Allocation Graph Algorithm• Claim edge Pi→ Rjindicated that process Pjmay request resource Rj; represented by a dashed line.• Claim edge converts to request edge when a process requests a resource.• When a resource is released by a process, assignment edge reconverts to a claim edge.• Resources must be claimed a priori in the system.Banker’s Algorithm• Multiple instances.• Each process must a priori claim maximum use.• When a process requests a resource it may have to wait. • When a process gets all its resources it must return them in a finite amount of time.Data Structures for the Banker’s Algorithm• Available: Vector of length m. If available [j] = k, there are kinstances of resource type Rjavailable.• Max: n x m matrix. If Max [i,j] = k, then process Pimay request at most k instances of resource type Rj.• Allocation: n x m matrix. If Allocation[i,j] = k then Piis currently allocated k instances of Rj.• Need: n x m matrix. If Need[i,j] = k, then Pimay need k more instances of Rjto complete its task.Need [i,j] = Max[i,j] – Allocation [i,j].Let n = number of processes, and m = number of resources types.Safety Algorithm1. Let Work and Finish be vectors of length m and n, respectively. Initialize:Work = AvailableFinish [i] = false for i - 1,3, …, n.2. Find and i such that both: (a) Finish [i] = false(b) Needi≤ WorkIf no such i exists, go to step 4.3. Work = Work + AllocationiFinish[i] = truego to step 2.4. If Finish [i] == true for all i, then the
View Full Document