42
CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling Deadlocks Deadlock Prevention Deadlock Prevention Deadlock Avoidance Deadlock Avoidance Deadlock Detection Deadlock Detection Recovery from Deadlock Recovery from Deadlock Combined Approach to Deadlock Combined Approach to Deadlock Handling Handling

CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Embed Size (px)

Citation preview

Page 1: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

CHAPTER 8: DEADLOCKS

System ModelSystem Model Deadlock CharacterizationDeadlock Characterization Methods for Handling DeadlocksMethods for Handling Deadlocks Deadlock PreventionDeadlock Prevention Deadlock AvoidanceDeadlock Avoidance Deadlock Detection Deadlock Detection Recovery from Deadlock Recovery from Deadlock Combined Approach to Deadlock HandlingCombined Approach to Deadlock Handling

Page 2: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

SYSTEM MODEL A set of blocked processes each holding a resource A set of blocked processes each holding a resource

and waiting to acquire a resource held by another and waiting to acquire a resource held by another process in the set.process in the set.

Example Example System has 2 tape drives.System has 2 tape drives. PP11 and and PP22 each hold one tape drive and each needs each hold one tape drive and each needs

another one.another one. Example Example

semaphores semaphores AA and and B B, initialized to , initialized to

PP00 PP11

wait (A);wait (A); wait(B);wait(B);wait (B);wait (B); wait(A);wait(A);

Page 3: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

System Model : Bridge crossing example

Traffic only in one direction.Traffic only in one direction. Each section of a bridge can be viewed as a resource.Each section of a bridge can be viewed as a resource. If a deadlock occurs, it can be resolved if one car If a deadlock occurs, it can be resolved if one car

backs up (preempt resources and rollback).backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock Several cars may have to be backed up if a deadlock

occurs.occurs. Starvation is possible.Starvation is possible.

Page 4: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

System Model: The general model Resource types Resource types RR11, , RR22, . . ., , . . ., RRmm

CPU cycles, memory space, I/O devicesCPU cycles, memory space, I/O devices Each resource type Each resource type RRii has has WWii instances. instances.

Each process utilizes a resource as follows:Each process utilizes a resource as follows: request request use use ReleaseRelease

A set of processes is in a deadlock state when everA set of processes is in a deadlock state when every process in the set is waiting for an event that can y process in the set is waiting for an event that can be caused only by another process in the set.be caused only by another process in the set.

Page 5: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

DEADLOCK CHARACTERIZATION Mutual exclusion:Mutual exclusion: only one process at a time can use a only one process at a time can use a

resource.resource. Hold and wait:Hold and wait: a process holding at least one resource is a process holding at least one resource is

waiting to acquire additional resources held by other waiting to acquire additional resources held by other processes.processes.

No preemption:No preemption: a resource can be released only a resource can be released only voluntarily by the process holding it, after that process has voluntarily by the process holding it, after that process has completed its task.completed its task.

Circular wait:Circular wait: there exists a set { there exists a set {PP00, , PP11, …, , …, PnPn} of waiting } of waiting processes such that processes such that PP0 0 is waiting for a resource that is held is waiting for a resource that is held by by PP11, , PP11 is waiting for a resource that is held by is waiting for a resource that is held by

PP22, …, , …, PPnn–1–1 is waiting for a resource that is held by is waiting for a resource that is held by PPnn, and , and PnPn is waiting for a resource that is held by is waiting for a resource that is held by PP00..

Page 6: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Characterization : Resource-allocation graph

V is partitioned into two types:V is partitioned into two types: PP = { = {PP11, , PP22, …, , …, PPnn}, the set consisting of all }, the set consisting of all

the processes in the system.the processes in the system.

RR = { = {RR11, , RR22, …, , …, RRmm}, the set consisting of all }, the set consisting of all

resource types in the system.resource types in the system. request edge – directed edge request edge – directed edge PP1 1 RRjj

assignment edge – directed edge assignment edge – directed edge RRjj PPii

Page 7: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Characterization : Example of a resource allocation graph

Page 8: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Characterization : Resource allocation graph with a deadlock

Page 9: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Characterization : Resource allocation graph with a cycle but no deadlock

Page 10: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Characterization : Basic facts

If graph contains no cycles If graph contains no cycles no deadlock. no deadlock.

If graph contains a cycle If graph contains a cycle if only one instance per resource type, then if only one instance per resource type, then

deadlock.deadlock. if several instances per resource type, if several instances per resource type,

possibility of deadlockpossibility of deadlock

Page 11: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

METHODS FOR HANDLING DEADLOCKS

Ensure that the system will Ensure that the system will nevernever enter a enter a deadlock state.deadlock state.

Allow the system to enter a deadlock state and Allow the system to enter a deadlock state and then recover.then recover.

Ignore the problem and pretend that deadlocks Ignore the problem and pretend that deadlocks never occur in the system; used by most never occur in the system; used by most operating systems, including UNIX.operating systems, including UNIX.

Page 12: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

DEADLOCK PREVENTION Mutual ExclusionMutual Exclusion – not required for sharable – not required for sharable

resources; must hold for nonsharable resources.resources; must hold for nonsharable resources.

Hold and WaitHold and Wait – must guarantee that whenever a – must guarantee that whenever a process requests a resource, it does not hold any process requests a resource, it does not hold any other resources.other resources. Require process to request and be allocated all Require process to request and be allocated all

its resources before it begins execution, or its resources before it begins execution, or allow process to request resources only when allow process to request resources only when the process has none.the process has none.

Low resource utilization; starvation possible.Low resource utilization; starvation possible.

Page 13: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Prevention No PreemptionNo Preemption – –

If a process that is holding some resources requests If a process that is holding some resources requests another resource that cannot be immediately another resource that cannot be immediately allocated to it, then all resources currently being allocated to it, then all resources currently being held are released.held are released.

Preempted resources are added to the list of Preempted resources are added to the list of resources for which the process is waiting.resources for which the process is waiting.

Process will be restarted only when it can regain Process will be restarted only when it can regain its old resources, as well as the new ones that it is its old resources, as well as the new ones that it is requesting.requesting.

Circular WaitCircular Wait – impose a total ordering of all – impose a total ordering of all resource types, and require that each process requests resource types, and require that each process requests resources in an increasing order of enumeration.resources in an increasing order of enumeration.

Page 14: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

DEADLOCK AVOIDANCE Deadlock prevention Deadlock prevention low device utilization and low device utilization and

reduced system throughput.reduced system throughput. Deadlock avoidanceDeadlock avoidance

Given the complete sequence of requests and releases Given the complete sequence of requests and releases for each process, we can decide for each request for each process, we can decide for each request whether or not the process should wait.whether or not the process should wait.

For every request, the system For every request, the system considers the resources currently available, the considers the resources currently available, the

resources currently allocated, and the future resources currently allocated, and the future requests and releases of each process, and requests and releases of each process, and

decides whether the current request can be satisfied decides whether the current request can be satisfied or must wait to avoid a possible future deadlock.or must wait to avoid a possible future deadlock.

Page 15: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance The simplest and most useful deadlock avoidance The simplest and most useful deadlock avoidance

algorithm requires that each process declare the algorithm requires that each process declare the maximum numbermaximum number of resources of each type that it of resources of each type that it may need.may need.

The deadlock-avoidance algorithm dynamically The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that examines the resource-allocation state to ensure that there can never be a circular-wait condition (unsafe there can never be a circular-wait condition (unsafe state).state).

Resource-allocation Resource-allocation statestate is defined by the number of is defined by the number of available and allocated resources, and the maximum available and allocated resources, and the maximum demands of the processes.demands of the processes.

Page 16: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Safe state When a process requests an available resource, the system mWhen a process requests an available resource, the system m

ust decide if immediate allocation leaves the system in a ust decide if immediate allocation leaves the system in a safsafe statee state..

The system is in The system is in safe statesafe state if there exists a safe sequence of if there exists a safe sequence of all processes. The sequence <all processes. The sequence <PP11, , PP22, …, , …, PPnn> is safe if for ea> is safe if for eachch P Pii, the resources that , the resources that PPii can still request can be satisfied b can still request can be satisfied by currently available resources + resources held by all the y currently available resources + resources held by all the PPjj, , with with j<ij<i.. If PIf Pii resource needs are not immediately available, then resource needs are not immediately available, then PPii

can wait until all can wait until all PPjj have finished.have finished. When When PPjj is finished, is finished, PPii can obtain needed resources, exec can obtain needed resources, exec

ute, return allocated resources, and terminate. ute, return allocated resources, and terminate. When When PPii terminates, terminates, PPii+1+1 can obtain its needed resources, can obtain its needed resources,

and so on.and so on.

Page 17: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Basic facts

If a system is in safe state If a system is in safe state no deadlocks. no deadlocks. If a system is in unsafe state If a system is in unsafe state possibility of possibility of

deadlock.deadlock. May be YESMay be YES May be NOMay be NO

Avoidance Avoidance to ensure that a system will never enter to ensure that a system will never enter an unsafe state. an unsafe state.

Two algorithmsTwo algorithms Resource allocation graph algorithm (Every Resource allocation graph algorithm (Every

resource type has a single instance).resource type has a single instance). Banker’s algorithm ( Every type has any number Banker’s algorithm ( Every type has any number

of instances.)of instances.)

Page 18: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Safe, unsafe , deadlock state

Page 19: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Resource-allocation graph algorithm Claim edgeClaim edge PPii RRjj indicated that process indicated that process PPjj may requ may requ

est resource est resource RRjj; represented by a dashed line.; represented by a dashed line.

Claim edge converts to request edge when a process rClaim edge converts to request edge when a process requests a resource.equests a resource.

When a resource is released by a process, assignment When a resource is released by a process, assignment edge reconverts to a claim edge.edge reconverts to a claim edge.

Initially, resources must be claimed in the system.Initially, resources must be claimed in the system.

Page 20: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Resource-allocation graph for deadlock avoidance

Page 21: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Unsafe state in resource-allocation graph

Page 22: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Banker’s algorithm

Multiple instances.Multiple instances.

Each process must a priori (Each process must a priori ( 事前事前 ) claim maxi) claim maximum use.mum use.

When a process requests a resource it may havWhen a process requests a resource it may have to wait. e to wait.

When a process gets all its resources it must reWhen a process gets all its resources it must return them in a finite amount of time.turn them in a finite amount of time.

Page 23: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Data structures for the banker’s algorithm

Available:Available: Vector of length Vector of length mm. If . If available [j] = kavailable [j] = k, there , there areare k k instances of resource type instances of resource type RRjj available.available.

Max: n x mMax: n x m matrix. If matrix. If Max Max [[i,ji,j] = ] = kk, then process , then process PPii may may

request at mostrequest at most k k instances of resource type instances of resource type RRjj..

Allocation: n Allocation: n xx m m matrix. If matrix. If Allocation[i,j] = kAllocation[i,j] = k then then P Pii i i

s currently allocated s currently allocated kk instances of instances of RRj.j.

Need: n Need: n xx m m matrix. If matrix. If NeedNeed[[i,ji,j] =] = k k, then, then P Pii may need may need kk

more instances of more instances of RRjj to complete its task.to complete its task.

NeedNeed [ [i,j]i,j] = = MaxMax[[i,ji,j] – ] – AllocationAllocation [ [i,ji,j].].

Page 24: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Safety algorithm

1.1. Let Let Work Work and and FinishFinish be vectors of length be vectors of length m m and and n n, respectiv, respectively. Initialize:ely. Initialize:

Work Work = = AvailableAvailableFinish Finish [[ii] =] = false false forfor i i = 1,2, …, = 1,2, …, n.n.

2.2. Find an Find an i i such that both: such that both:

(a) (a) FinishFinish [ [ii] = ] = false false (b) (b) NeedNeedii WorkWork

If no such If no such i i exists, go to step 4.exists, go to step 4.

3.3. WorkWork = = Work Work + + AllocationAllocationii

FinishFinish[[ii] =] = true truego to step 2.go to step 2.

4.4. If If FinishFinish [ [ii] == true for all ] == true for all ii, , safe state; otherwise, safe state; otherwise, unsunsafe state.afe state.

Page 25: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Resource-request algorithm for process Pi

RequestRequest = request vector for process = request vector for process PPii. If . If RequestRequestii [[jj] = ] = kk then pr then process ocess PPii wants wants kk instances of resource type instances of resource type RRjj..

1.1.If If RequestRequestii NeedNeedii go to step 2. Otherwise, error. go to step 2. Otherwise, error.

2.2.If If RequestRequestii AvailableAvailable, go to step 3. Otherwise , go to step 3. Otherwise PPii wait. wait.

3.3.Pretend to allocate requested resources to Pretend to allocate requested resources to PPii by modifying th by modifying the state as follows:e state as follows:

AvailableAvailable = = Available Available - - RequestRequestii;;

AllocationAllocationii = = AllocationAllocationii + + RequestRequestii;;

NeedNeedii == Need Needii – – RequestRequesti;;i;;

• If safe If safe the resources are allocated to P the resources are allocated to Pii. . • If unsafe If unsafe P Pii must wait, and the old resource-allocation must wait, and the old resource-allocation

state is restoredstate is restored

Page 26: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Example of banker’s algorithm 5 processes 5 processes PP0 0 through through PP44; 3 resource types ; 3 resource types AA

(10 instances), B (5instances), and (10 instances), B (5instances), and CC (7 instances). (7 instances). Snapshot at time Snapshot at time TT00::

AllocationAllocation MaxMax AvailableAvailable

A B CA B C A B C A B C A B CA B C

PP00 0 1 00 1 0 7 5 3 7 5 3 3 3 23 3 2

PP11 2 0 0 2 0 0 3 2 2 3 2 2

PP22 3 0 2 3 0 2 9 0 29 0 2

PP33 2 1 1 2 1 1 2 2 22 2 2

PP44 0 0 20 0 2 4 3 3 4 3 3

Page 27: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Example (Cont.)

The content of the matrix. The content of the matrix. NeedNeed is defined to be is defined to be Max – Allocation.Max – Allocation.

NeedNeedA B CA B C

PP00 7 4 3 7 4 3

PP11 1 2 2 1 2 2

PP22 6 0 0 6 0 0

PP33 0 1 10 1 1

PP44 4 3 1 4 3 1 The system is in a safe state since the sequence < The system is in a safe state since the sequence < PP11, , PP33, ,

PP44, , PP22, , PP00> satisfies safety criteria. > satisfies safety criteria.

Page 28: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Avoidance: Example P1 Request (1,0,2) (Cont.)

Check that Check that Request Request Available Available (that is, (1,0,2) (that is, (1,0,2) (3,3,2) (3,3,2) true.true.

AllocationAllocation NeedNeed AvailableAvailable

A B CA B CA B CA B C A B C A B C

PP00 0 1 0 0 1 0 7 4 3 7 4 3 2 3 02 3 0

PP11 3 0 23 0 2 0 2 0 0 2 0

PP22 3 0 1 3 0 1 6 0 0 6 0 0

PP33 2 1 1 2 1 1 0 1 10 1 1

PP44 0 0 2 0 0 2 4 3 1 4 3 1

Executing safety algorithm shows that sequence <Executing safety algorithm shows that sequence <PP11, , PP33, , PP44, , PP00, , PP22> >

satisfies safety requirement. satisfies safety requirement. Can request for (3,3,0) by Can request for (3,3,0) by PP44 be granted? be granted?

Can request for (0,2,0) by Can request for (0,2,0) by PP0 0 be granted?be granted?

Page 29: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

DEADLOCK DETECTION If a system does not employ either a deadlock-preventIf a system does not employ either a deadlock-prevent

ion or deadlock-avoidance algorithm, then a deadlock ion or deadlock-avoidance algorithm, then a deadlock situation may occur. situation may occur. Detection algorithm: An algorithm that examines tDetection algorithm: An algorithm that examines t

he state of the system to determine whether a deadlhe state of the system to determine whether a deadlock has occurred. ock has occurred.

Recovery algorithm: An algorithm to recover from Recovery algorithm: An algorithm to recover from the deadlock. the deadlock.

Two detection algorithmsTwo detection algorithms Wait-for graph (Every type has one single instancWait-for graph (Every type has one single instanc

e).e). Detection-algorithm (similar to safety algorithm) Detection-algorithm (similar to safety algorithm)

(Every type has any number of intances)(Every type has any number of intances)

Page 30: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Detection:Single Instance of Each Resource Type

Generate a Generate a wait-forwait-for graph for its corresponding graph for its corresponding resource allocation graphresource allocation graph Nodes are processes.Nodes are processes. PPii PPj j if if PPii is waiting foris waiting for P Pjj..

Periodically invoke an algorithm that searches Periodically invoke an algorithm that searches for a cycle in the graph.for a cycle in the graph.

An algorithm to detect a cycle in a graph requiAn algorithm to detect a cycle in a graph requires an order ofres an order of n n22 operations, where operations, where nn is the nu is the number of vertices in the graph.mber of vertices in the graph.

Page 31: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Detection:Resource-Allocation Graph and Wait-for Graph

Page 32: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Detection:Several Instances of a Resource Type Data structures: Data structures:

Available:Available: A vector of length A vector of length mm indicates the num indicates the number of available resources of each type.ber of available resources of each type.

Allocation:Allocation: An An n n xx m m matrix defines the number of matrix defines the number of resources of each type currently allocated to each presources of each type currently allocated to each process Every row of allocation is a vector. rocess Every row of allocation is a vector.

Request:Request: An An n n xx m m matrix indicates the current req matrix indicates the current request of each process. If uest of each process. If Request Request [[i,ji,j] = ] = kk, then pro, then processcess P Pii is requesting is requesting k k more instances of resource t more instances of resource t

ype. ype. RRjj. Every row of request is a vector. . Every row of request is a vector.

Page 33: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Detection: Detection Algorithm1.1. Let Let WorkWork and and FinishFinish be vectors of length be vectors of length mm and and nn, respectively Init, respectively Init

ialize:ialize:

(a) (a) WorkWork = = AvailableAvailable

(b)(b) For For ii = 1,2, …, = 1,2, …, n n, if , if AllocationAllocationii 0, then 0, then

FinishFinish[i] = false;otherwise, [i] = false;otherwise, FinishFinish[i] = [i] = truetrue..

2.2. Find an index Find an index i i such that both:such that both:

(a)(a) FinishFinish[[ii] == ] == falsefalse (b) (b) RequestRequestii WorkWork

If no such If no such ii exists, go to step 4. exists, go to step 4.

3.3. WorkWork = = WorkWork + + AllocationAllocationii

FinishFinish[[ii] = ] = truetruego to step 2.go to step 2.

4.4. If If Finish[i] == falseFinish[i] == false, for some , for some ii, , 1 1 i i n n, then the system is in d, then the system is in deadlock state. Moreover, if eadlock state. Moreover, if FinishFinish[[ii] == ] == falsefalse, then , then PiPi is deadlocke is deadlocked.d.

Page 34: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Detection Algorithm: Am example Five processes Five processes PP00 through through PP44;; three resource types three resource types

A (7 instances), A (7 instances), B B (2 instances), and (2 instances), and CC (6 instances). (6 instances). Snapshot at time Snapshot at time TT00::

AllocationAllocation RequestRequest AvailableAvailableA B C A B C A B C A B C A B CA B C

PP00 0 1 0 0 1 0 0 0 0 0 0 0 0 0 00 0 0

PP11 2 0 0 2 0 0 2 0 22 0 2

PP22 3 0 33 0 3 0 0 0 0 0 0

PP33 2 1 1 2 1 1 1 0 0 1 0 0

PP44 0 0 2 0 0 2 0 0 20 0 2 Sequence Sequence <P<P00, P, P22, P, P33, P, P11, P, P44>> will result in will result in Finish[i] = Finish[i] =

truetrue for all for all ii. .

Page 35: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Detection Algorithm: Am example (Cont.) PP22 requests an additional instance of type requests an additional instance of type C C..

RequestRequest

A B CA B C

PP00 0 0 00 0 0

PP11 2 0 12 0 1

PP22 0 0 10 0 1

PP33 1 0 0 1 0 0

PP44 0 0 20 0 2

State of system?State of system? Can reclaim resources held by process Can reclaim resources held by process PP00, but insufficient , but insufficient

resources to fulfill other processes; requests.resources to fulfill other processes; requests. Deadlock exists, consisting of processes Deadlock exists, consisting of processes PP11, , PP22, , PP33, and , and PP44..

Page 36: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Detection-Algorithm: Usage

When, and how often, to invoke detection When, and how often, to invoke detection algorithmalgorithm

It depends on:It depends on: How often a deadlock is likely to occur?How often a deadlock is likely to occur? How many processes will need to be rolled How many processes will need to be rolled

back?back?one for each disjoint cycleone for each disjoint cycle

Page 37: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

DEADLOCK RECOVERY

Process TerminationProcess Termination Resource PreemptionResource Preemption

Page 38: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Recovery: Process Termination Abort all deadlocked processes.Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is Abort one process at a time until the deadlock cycle is

eliminated.eliminated. In which order should we choose to abort?In which order should we choose to abort?

Priority of the process.Priority of the process. How long process has computed, and how much How long process has computed, and how much

longer to completion.longer to completion. Resources the process has used.Resources the process has used. Resources process needs to complete.Resources process needs to complete. How many processes will need to be terminated. How many processes will need to be terminated. Is process interactive or batch?Is process interactive or batch?

Page 39: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Deadlock Recovery: Resource Preemption

Selecting a victim – minimize cost.Selecting a victim – minimize cost.

Rollback – return to some safe state, restart prRollback – return to some safe state, restart process for that state.ocess for that state.

Starvation – the same process may always be Starvation – the same process may always be picked as victim, include number of rollback ipicked as victim, include number of rollback in cost factor.n cost factor.

Page 40: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Combined Approach to Deadlock Handling Combine the three basic approaches (prevention, avoiCombine the three basic approaches (prevention, avoi

dance, and detection), allowing the use of the optimal dance, and detection), allowing the use of the optimal approach for each class of resources in the system. approach for each class of resources in the system.

Partition resources into hierarchically ordered classes. Partition resources into hierarchically ordered classes. Use most appropriate technique for handling deadlocUse most appropriate technique for handling deadloc

ks within each class. ks within each class. An example: An example:

Internal resources (Prevention through resource orInternal resources (Prevention through resource ordering) dering)

Central memory (Prevention through preemption) Central memory (Prevention through preemption) Job resources (Avoidance) Job resources (Avoidance) Swappable space (Preallocation) Swappable space (Preallocation)

Page 41: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling

Homework 8.58.5 8.68.6 8.138.13

Page 42: CHAPTER 8: DEADLOCKS System Model System Model Deadlock Characterization Deadlock Characterization Methods for Handling Deadlocks Methods for Handling