52
1 Chapter 7: Chapter 7: Deadlock Deadlock

1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

  • View
    260

  • Download
    5

Embed Size (px)

Citation preview

Page 1: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

11

Chapter 7: Chapter 7: DeadlockDeadlock

Page 2: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

22

Chapter 7: DeadlockChapter 7: Deadlock

The Deadlock ProblemThe Deadlock Problem

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

Page 3: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

33

Chapter ObjectivesChapter Objectives

To develop a description of deadlocks, which To develop a description of deadlocks, which prevent sets of concurrent processes from prevent sets of concurrent processes from completing their taskscompleting their tasks

To present a number of different methods for To present a number of different methods for preventing or avoiding deadlocks in a preventing or avoiding deadlocks in a computer systemcomputer system

Page 4: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

44

The Deadlock ProblemThe Deadlock ProblemDefnDefn: 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 process and waiting to acquire a resource held by another process in the setin the set

Example: System has 2 disk drivesExample: System has 2 disk drives PP11 and and PP22 each hold one disk drive and each needs another one each hold one disk drive and each needs another one

Example Example semaphores semaphores AA and and B B, initialized to 1, initialized to 1

PP00 PP11

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

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

Page 5: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

55

Bridge Crossing ExampleBridge Crossing Example

Traffic only in one directionTraffic only in one direction

Each section of a bridge can be viewed as a Each section of a bridge can be viewed as a resourceresource

If a deadlock occurs, it can be resolved if one If a deadlock occurs, it can be resolved if one car backs up (car backs up (preempt resourcespreempt resources and and rollbackrollback))

Page 6: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

66

Bridge Crossing ExampleBridge Crossing Example

Several cars may have to be backed up if a Several cars may have to be backed up if a deadlock occursdeadlock occurs

StarvationStarvation is possible is possible

Note – Most OSes do not prevent or deal with Note – Most OSes do not prevent or deal with deadlocksdeadlocks

Page 7: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

77

System ModelSystem ModelResource types Resource types RR11, , RR22, . . ., , . . ., RRmm

ExEx: CPU cycles, memory space, I/O devices: CPU cycles, memory space, I/O devices

Each resource type Each resource type RRii has has WWii instances. instances.

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

Page 8: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

88

Deadlock CharacterizationDeadlock Characterization

1. Mutual exclusion1. Mutual exclusion:: only one process at a time can only one process at a time can use a resourceuse a resource

2. 2. Hold and waitHold and wait:: a process holding at least one a process holding at least one resource is waiting to acquire additional resources resource is waiting to acquire additional resources held by other processesheld by other processes

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

Deadlock can arise if four conditions hold simultaneously.

Page 9: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

99

Deadlock CharacterizationDeadlock Characterization

4. Circular wait4. Circular wait:: there exists a set { there exists a set {PP00, , PP11, …, , …, PP00} }

of waiting processes such that of waiting processes such that

PP0 0 is waiting for a resource that is held by is waiting for a resource that is held 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 PPnn is waiting for a resource that is held by is waiting for a resource that is held by PP00..

Page 10: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

1010

Resource-Allocation GraphResource-Allocation Graph

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

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

systemsystem

request edgerequest edge – directed edge – directed edge PPi i RRjj

assignment edgeassignment edge – directed edge – directed edge RRjj PPii

A set of vertices V and a set of edges E.

Page 11: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

1111

Resource-Allocation Graph Resource-Allocation Graph (Cont.)(Cont.)

ProcessProcess

Resource Type with 4 instancesResource Type with 4 instances

PPii requests instance of requests instance of RRjj

PPii is holding an instance of is holding an instance of RRjj

Pi

Rj

Pi

Rj

Page 12: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

1212

Example of a Resource Example of a Resource Allocation GraphAllocation Graph

Page 13: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

1313

Resource Allocation Graph With A Resource Allocation Graph With A DeadlockDeadlock

Page 14: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

1414

Graph With A Cycle But No Graph With A Cycle But No DeadlockDeadlock

Page 15: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

1515

Basic FactsBasic FactsIf graph contains no cycles If graph contains no cycles no no deadlockdeadlock

If graph contains a cycle If graph contains a cycle if only one instance per resource if only one instance per resource

type, then deadlocktype, then deadlock if several instances per resource if several instances per resource

type, possibility of deadlocktype, possibility of deadlock

Page 16: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

1616

Methods for Handling Methods for Handling DeadlocksDeadlocks

Ensure that the system will Ensure that the system will nevernever enter a deadlock enter a deadlock statestate

Allow the system to enter a deadlock state and then Allow the system to enter a deadlock state and then recoverrecover

Ignore the problem and pretend that deadlocks never Ignore the problem and pretend that deadlocks never occur in the system occur in the system Used by most operating systems, including UNIXUsed by most operating systems, including UNIX Occasionally restart system manually after deadlock drags Occasionally restart system manually after deadlock drags

system to a halt.system to a halt.

Page 17: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

1717

Deadlock PreventionDeadlock Prevention

Mutual ExclusionMutual Exclusion – not required for sharable – not required for sharable resources; must hold for nonsharable resourcesresources; 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 resourcesother resources Two possible protocols:Two possible protocols:

Require process to request and be allocated all its resources Require process to request and be allocated all its resources before it begins execution before it begins execution

or allow process to request resources only when the process or allow process to request resources only when the process has nonehas none

Low resource utilization; starvation possibleLow resource utilization; starvation possible

Restrain the ways a request can be made

Page 18: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

1818

No PreemptionNo Preemption – (one protocol) – (one protocol) If a process that is holding some resources If a process that is holding some resources

requests another resource that cannot be requests another resource that cannot be immediately allocated to it, then all resources immediately allocated to it, then all resources currently being held are releasedcurrently being 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 waitingresources for which the process is waiting

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

Page 19: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

No PreemptionNo Preemption – (second protocol) – (second protocol) If a process requests some resources, first a If a process requests some resources, first a

check is made and if resources are available, check is made and if resources are available, they are allocated.they are allocated.

If some resources are not available, check to see If some resources are not available, check to see if these resources are allocated to some other if these resources are allocated to some other process that is waiting for additional resources. process that is waiting for additional resources.

If so, the desired resources are preempted and If so, the desired resources are preempted and allocated. allocated.

If resources are not either available nor held by a If resources are not either available nor held by a waiting process, the requesting process must waiting process, the requesting process must wait wait

While waiting, some of its resources may be While waiting, some of its resources may be preempted by another processor. preempted by another processor.

It can restart only after it obtains requested and lost It can restart only after it obtains requested and lost resources.resources. 1919

Page 20: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Circular WaitCircular Wait – impose a total ordering of – impose a total ordering of all resource types, and require that each all resource types, and require that each process requests resources in an process requests resources in an increasing order of enumerationincreasing order of enumeration Multiple resources of the same type must be Multiple resources of the same type must be

requested at the same time.requested at the same time. Does not work if locks can be acquired Does not work if locks can be acquired

dynamically.dynamically. See textbook for further details.See textbook for further details.

2020

Page 21: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

2121

Deadlock AvoidanceDeadlock Avoidance

Simplest and most useful model requires that each Simplest and most useful model requires that each process declare the process declare the maximum numbermaximum number of resources of of resources of each type that it may needeach type that it may need

The The deadlock-avoidance algorithmdeadlock-avoidance algorithm dynamically dynamically examines the resource-allocation state to ensure that examines the resource-allocation state to ensure that there can never be a circular-wait conditionthere can never be a circular-wait condition

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 processesdemands of the processes

Requires that the system has some additional a priori information available

Page 22: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

2222

Safe StateSafe State

Note: A basic consideration when deciding which Note: A basic consideration when deciding which deadlock avoidance algorithm to use. deadlock avoidance algorithm to use.

When a process requests an available resource, When a process requests an available resource, system must decide if immediate allocation leaves the system must decide if immediate allocation leaves the system in a safe statesystem in a safe state

System is in a System is in a safe statesafe state if there exists a sequence if there exists a sequence <<PP11, P, P22, …, P, …, Pnn> of ALL the processes in the systems > of ALL the processes in the systems

such that for each Psuch that for each P ii, the resources that P, the resources that P i i can still can still

request can be satisfied by currently available request can be satisfied by currently available resources + resources held by all the resources + resources held by all the PPjj, with, with j j < < ii . .

Page 23: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

2323

That is:That is: If PIf Pii resource needs are not immediately resource needs are not immediately

available, then available, then PPii can wait until all can wait until all PPjj have have

finishedfinished When When PPjj is finished, is finished, PPii can obtain needed can obtain needed

resources, execute, return allocated resources, execute, return allocated resources, and terminateresources, and terminate

When When PPii terminates, terminates, PPi i +1+1 can obtain its can obtain its

needed resources, and so on needed resources, and so on

Page 24: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

2424

Basic FactsBasic FactsIf a system is in safe state If a system is in safe state no no deadlocksdeadlocks

If a system is in unsafe state If a system is in unsafe state possibility of deadlockpossibility of deadlock

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

Page 25: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

2525

Safe, Unsafe , Deadlock State Safe, Unsafe , Deadlock State

Page 26: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

2626

Avoidance algorithmsAvoidance algorithmsSingle instance of a resource typeSingle instance of a resource type Use a resource-allocation graphUse a resource-allocation graph

Multiple instances of a resource typeMultiple instances of a resource type Use the banker’s algorithmUse the banker’s algorithm

Page 27: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

2727

Resource-Allocation Graph SchemeResource-Allocation Graph Scheme

A claim edgeA claim edge PPii RRjj indicates that process indicates that process PPii

may request resource may request resource RRjj; represented by a ; represented by a

dashed linedashed line

A claim edge converts to a A claim edge converts to a request edgerequest edge when a when a process requests a resourceprocess requests a resource

A A request edgerequest edge converts to an assignment edge converts to an assignment edge when the resource is allocated to the processwhen the resource is allocated to the process

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

Resources must be claimed Resources must be claimed a prioria priori in the system in the system

Page 28: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

2828

Resource-Allocation GraphResource-Allocation Graph

Page 29: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

2929

Unsafe State In Resource-Unsafe State In Resource-Allocation GraphAllocation Graph

Page 30: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3030

Resource-Allocation Graph Resource-Allocation Graph AlgorithmAlgorithm

Suppose that processSuppose that process P Pii requests requests

a resource a resource RRjj

The request can be granted only if The request can be granted only if converting the request edge to an converting the request edge to an assignment edge does not result assignment edge does not result in the formation of a cycle in the in the formation of a cycle in the resource allocation graphresource allocation graph

Page 31: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3131

Banker’s AlgorithmBanker’s AlgorithmMultiple instancesMultiple instances

Each process must a priori claim maximum useEach process must a priori claim maximum use

When a process requests a resource it may have When a process requests a resource it may have to wait to wait

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

Page 32: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3232

Data Structures for the Banker’s AlgorithmData Structures for the Banker’s Algorithm

AvailableAvailable:: Vector of length Vector of length mm. If available [. If available [jj] = ] = kk, there are, there are k k instances of resource type instances of resource type RRjj availableavailable

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

mostmost k k instances of resource type instances of resource type RRjj

AllocationAllocation: n : n xx m m matrix. If Allocation[ matrix. If Allocation[i,ji,j] = ] = kk then then P Pii is currently is currently

allocated allocated kk instances of instances of RRjj

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

instances of instances of RRjj to complete its taskto complete its task

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

Let n = number of processes, and m = number of resources types.

Page 33: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3333

Safety AlgorithmSafety Algorithm1.1. Let Let Work Work and and FinishFinish be vectors of lengthbe vectors of length m m and and n n, respectively. , respectively.

Initialize:Initialize:Work Work = = AvailableAvailableFinish Finish [[ii] =] = false false forfor i i = 0, 1, …, = 0, 1, …, n- n- 11

2.2. Find an Find an i i such that both: such that both: (a) (a) FinishFinish [ [ii] = ] = falsefalse

(b) (b) NeedNeedii WorkWork

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

3.3. WorkWork = = Work Work + + AllocationAllocationii

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

4.4. If If FinishFinish [ [ii] == true for all ] == true for all ii, then the system is in a safe state, then the system is in a safe state

Page 34: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3434

Resource-Request Algorithm for Process Resource-Request Algorithm for Process PPii

RequestRequest = request vector for process = request vector for process PPii. If . If RequestRequestii

[[jj] = ] = kk then process then process PPii wants wants kk instances of resource instances of resource type type RRjj

1.1. If If RequestRequestii NeedNeedii go to step 2. Otherwise, go to step 2. Otherwise, raise error condition, since process has exceeded raise error condition, since process has exceeded its maximum claimits maximum claim

2.2. If If RequestRequestii AvailableAvailable, go to step 3. , go to step 3. Otherwise Otherwise PPii must wait, since resources are not must wait, since resources are not availableavailable

==>>==>>

Page 35: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3535

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

AvailableAvailable = = Available Available –– Request; Request;

AllocationAllocationii = = AllocationAllocationii + +

RequestRequestii;;

NeedNeedii == Need Needii – – RequestRequestii;;

If safe If safe the resources are allocated to Pi the resources are allocated to Pi If unsafe If unsafe Pi must wait, and the old resource- Pi must wait, and the old resource-

allocation state is restoredallocation state is restored

Page 36: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3636

Example of Banker’s AlgorithmExample of Banker’s Algorithm5 processes 5 processes PP0 0 through through PP44; ; 3 resource types: 3 resource types: AA (10 instances), (10 instances), BB (5 instances), and (5 instances), and CC (7 (7

instances)instances)

Snapshot at time Snapshot at time TT00::

AllocationAllocation MaxMax AvailableAvailable

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

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

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

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

PP33 2 1 1 2 1 1 2 2 2 2 2 2

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

Page 37: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3737

Example (Cont.)Example (Cont.)The content of the matrix The content of the matrix NeedNeed is defined to be is defined to be MaxMax – – AllocationAllocation

NeedNeed

A 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 1 0 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 38: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3838

Example: Example: PP11 Request (1,0,2) Request (1,0,2)Check that Request Check that Request Available (that is, (1,0,2) Available (that is, (1,0,2) (3,3,2) (3,3,2) truetrue

AllocationAllocation NeedNeed AvailableAvailable

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

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

PP1 1 3 0 2 0 2 0 3 0 2 0 2 0

PP2 2 3 0 1 3 0 1 6 0 0 6 0 0

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

PP4 4 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

Page 39: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

3939

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 PP00 be granted? be granted?

Page 40: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4040

Deadlock DetectionDeadlock Detection

Allow system to enter deadlock state Allow system to enter deadlock state

Detection algorithmDetection algorithm

Recovery schemeRecovery scheme

Page 41: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4141

Single Instance of Each Resource TypeSingle Instance of Each Resource TypeMaintain Maintain wait-forwait-for graph graph Nodes are processesNodes are processes PPii PPj j if if PPii is waiting foris waiting for P Pjj

Periodically invoke an algorithm that searches for a Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a cycle in the graph. If there is a cycle, there exists a deadlockdeadlock

An algorithm to detect a cycle in a graph requires an An algorithm to detect a cycle in a graph requires an order oforder of n n22 operations, where operations, where nn is the number of is the number of vertices in the graphvertices in the graph

Page 42: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4242

Resource-Allocation Graph and Resource-Allocation Graph and Wait-for GraphWait-for Graph

a) Resource-Allocation Graph

b) Corresponding wait-for graph

Page 43: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4343

Several Instances of a Resource TypeSeveral Instances of a Resource TypeAvailableAvailable:: A vector of length A vector of length mm indicates the number of indicates the number of available resources of each type.available resources of each type.

AllocationAllocation:: 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 resources of each type currently allocated to each process.process.

RequestRequest:: An An n n xx m m matrix indicates the current request matrix indicates the current request of each process. If of each process. If Request Request [[iijj] = ] = kk, then process, then process P Pii is is

requestingrequesting k k more instances of resource type. more instances of resource type. RRjj..

Page 44: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4444

Detection AlgorithmDetection Algorithm1.1. Let Let WorkWork and and FinishFinish be vectors of length be vectors of length mm and and nn, ,

respectively Initialize:respectively Initialize:

(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

Page 45: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4545

Detection Algorithm (Cont.)Detection Algorithm (Cont.)3.3.WorkWork = = WorkWork + + AllocationAllocationii

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

4.4.If If FinishFinish[[ii] == false, for some ] == false, for some ii, 1 , 1 ii nn, then , then the system is in deadlock state. Moreover, if the system is in deadlock state. Moreover, if FinishFinish[[ii] == ] == falsefalse, then , then PPii is deadlocked is deadlocked

The algorithm requires an order of O(m x n2) operations to detect whether the system is in a deadlocked state

Page 46: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4646

Example of Detection AlgorithmExample of Detection AlgorithmFive processes Five processes PP00 through through PP44;; three resource types: (7 three resource types: (7

instances), instances), B B (2 instances), and (2 instances), and CC (6 instances) (6 instances)

Snapshot at time Snapshot at time TT00::

AllocationAllocation RequestRequestAvailableAvailable

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

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

PP11 2 0 0 2 0 0 2 0 2 2 0 2

PP22 3 0 3 0 0 0 3 0 3 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 2 0 0 2

Sequence <Sequence <PP00, , PP22, , PP33, , PP11, , PP44> will result in > will result in FinishFinish[[ii] = true for all ] = true for all ii

Page 47: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4747

Example (Cont.)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

Page 48: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4848

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

insufficient resources to fulfill other processes; insufficient resources to fulfill other processes; requestsrequests

Deadlock exists, consisting of processes Deadlock exists, consisting of processes PP11, ,

PP22, , PP33, and , and PP44

Page 49: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

4949

Detection-Algorithm UsageDetection-Algorithm UsageWhen, and how often, to invoke depends on:When, and how often, to invoke 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

If the detection algorithm is invoked arbitrarily, If the detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph there may be many cycles in the resource graph and so we would not be able to tell which of the and so we would not be able to tell which of the many deadlocked processes “caused” the many deadlocked processes “caused” the deadlockdeadlock

Page 50: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

5050

Recovery from Deadlock: Process Recovery from Deadlock: Process TerminationTermination

Abort all deadlocked processesAbort 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 eliminatedeliminated

In which order should we choose to abort?In which order should we choose to abort? Priority of the processPriority of the process How long process has computed, and how much longer to How long process has computed, and how much longer to

completioncompletion Resources the process has usedResources the process has used Resources process needs to completeResources process needs to complete How many processes will need to be terminatedHow many processes will need to be terminated Is process interactive or batch?Is process interactive or batch?

Page 51: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

5151

Recovery from Deadlock: Recovery from Deadlock: Resource PreemptionResource Preemption

Selecting a victim – minimize costSelecting a victim – minimize cost

Rollback – return to some safe state, Rollback – return to some safe state, restart process for that staterestart process for that state

Starvation – same process may Starvation – same process may always be picked as victim, include always be picked as victim, include number of rollback in cost factornumber of rollback in cost factor

Page 52: 1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

5252

End of Chapter 7End of Chapter 7