Deadlock Avoidance Technique

15
Chapter 8 1 Deadlock Avoidance Technique Resource Allocation Denial: Grant incremental resource requests if we can prove that this leaves the system in a state in which deadlock cannot occur. Based on the concept of a “safe state” Banker’s Algorithm: Tentatively grant each resource request Analyze resulting system state to see if it is “safe”. If safe, grant the request if unsafe refuse the request (undo the tentative grant) block the requesting process until it is safe to grant it.

description

Deadlock Avoidance Technique. Resource Allocation Denial: Grant incremental resource requests if we can prove that this leaves the system in a state in which deadlock cannot occur . Based on the concept of a “safe state” Banker’s Algorithm: Tentatively grant each resource request - PowerPoint PPT Presentation

Transcript of Deadlock Avoidance Technique

Page 1: Deadlock Avoidance Technique

Chapter 81

Deadlock Avoidance Technique

Resource Allocation Denial:• Grant incremental resource requests if we can prove that

this leaves the system in a state in which deadlock cannot occur.

• Based on the concept of a “safe state” Banker’s Algorithm:

• Tentatively grant each resource request

• Analyze resulting system state to see if it is “safe”.

• If safe, grant the request

• if unsafe refuse the request (undo the tentative grant)

• block the requesting process until it is safe to grant it.

Page 2: Deadlock Avoidance Technique

Data Structures for the Banker’s Algorithm

Let n = number of processes, m = number of resource types

Available: Vector of length m. If Available [j] = k, there are k instances of resource type Rj currently available

Max: n x m matrix. If Max [i,j] = k, then process Pi will request at most k instances of resource type Rj.

Alloc: n x m matrix. If Alloc[i,j] = k then Pi is currently allocated (i.e. holding) k instances of Rj.

Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.

Need [i,j] = Max[i,j] – Alloc [i,j].

Page 3: Deadlock Avoidance Technique

Chapter 83

Safety Algorithm1. Let Work and Finish be vectors of length m and n,

respectively. Initialize:Work := AvailableFinish [i] == false for i = 1,2, …, n.

2. Find an i such that both: Finish [i] == falseNeedi Work

If no such i exists, go to step 4.3. Work := Work + Allocationi

(Resources freed when process completes!) Finish[i] := true go to step 2.

4. If Finish [i] = true for all i, then the system is in a safe state.

Page 4: Deadlock Avoidance Technique

Chapter 84

Resource-Request Algorithm for Process Pi

Requesti = request vector for Pi . Requesti [j] = k means process Pi wants k instances of resource type Rj.

1. If Requesti Needi go to step 2. Otherwise, error ( process exceeded its maximum claim).2. If Requesti Available, go to step 3. Otherwise Pi must wait, (resources not available).3. “Allocate” requested resources to Pi as follows:

Available := Available - Requesti

Alloci := Alloci + Requesti

Needi := Needi – Requesti

If safe the resources are allocated to Pi. If unsafe restore the old resource-allocation state and block Pi

Page 5: Deadlock Avoidance Technique

Chapter 85

Example of Banker’s Algorithm

5 processes P0 through P4 3 resource types A (10 units), B (5 units), and C (7

units).Snapshot at time T0:

Allocation Max AvailableA B C A B C A B C

P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3

Page 6: Deadlock Avoidance Technique

Chapter 86

Example (cont)Need = Max – Allocation

NeedA B C

P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1

The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.

Page 7: Deadlock Avoidance Technique

Chapter 87

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

Allocation Need AvailableA B C A B C A B C

P0 0 1 0 7 4 3 2 3 0P1 3 0 2 0 2 0 P2 3 0 2 6 0 0 P3 2 1 1 0 1 1P4 0 0 2 4 3 1

Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement.

Can request for (3,3,0) by P4 be granted? (no!)Can request for (0,2,0) by P0 be granted? (no, unsafe)

Page 8: Deadlock Avoidance Technique

Chapter 88

Banker’s algorithm: Comments A safe state cannot be deadlocked.

But an unsafe state is not necessarily deadlocked.

So if we withhold resources because the system would be “unsafe”:

• some processes may need to wait unnecessarily

• sub optimal use of resources

Page 9: Deadlock Avoidance Technique

Chapter 89

Deadlock Detection Resource accesses are granted to processes

whenever possible (might lead to deadlock)• (No need to declare Maximum Claim)

Detect the deadlock and recover.

The OS needs:• an algorithm to check if deadlock is present

• an algorithm to recover from deadlock

The deadlock check can be performed at every resource request (high overhead)

...or less frequently

Page 10: Deadlock Avoidance Technique

Chapter 810

Deadlock Detection Similar to Safety Algorithm, but use Request

matrix instead of Max matrix. Note that processes not holding any resources

cannot be involved in a deadlock! Like Safety, find a sequence (if possible) in which

resource allocations can be granted• assume they can run to completion with these

resources and then release all held resources

• might need more, leading to deadlock later, but we can detect that later!

Any processes not in this sequence are deadlocked

Page 11: Deadlock Avoidance Technique

Deadlock Detection Algorithm

Marks each process not deadlocked. Initially all processes are unmarked. Then perform:• Mark each process j for which: Alloc[ j,i] = 0 for all

resource type i. (These are not deadlocked)• Initialize work vector: Work[i] = Available[i] for all i• REPEAT: Find a unmarked process j such that

Request[ j,i ] <= Work[i] for all i. Stop if no such process exists.

• If such process j exists: mark process j and set Work[i] = Work [i] + Alloc[ j,i] for all i. Goto REPEAT.

• At the end: each unmarked process is deadlocked

Page 12: Deadlock Avoidance Technique

Chapter 812

Deadlock Detection Example

Mark P4 since it has no allocated resources Set Work = (0,0,0,0,1) P3’s request <= Work. So mark P3 and set

Work = Work + (0,0,0,1,0) = (0,0,0,1,1) Algorithm terminates. P1 and P2 are

deadlocked

R1 R2 R3 R4 R5

P1P2P3P4

Request Allocated Available

R1 R2 R3 R4 R5 R1 R2 R3 R4 R5

0 1 0 0 10 0 1 0 10 0 0 0 11 0 1 0 1

1 0 1 1 01 1 0 0 00 0 0 1 00 0 0 0 0

0 0 0 0 1

Page 13: Deadlock Avoidance Technique

Chapter 813

Deadlock Recovery

If you detect a deadlock, you need to do something!

The following approaches are possible:• Abort all deadlocked processes (common to

do this manually, inefficient)• Rollback each deadlocked process to some

previously defined checkpoint and restart them Requires checkpoint/restart procedures in OS The original deadlock may reoccur but good

chance it won’t

Page 14: Deadlock Avoidance Technique

Chapter 814

More recovery approaches

Successively abort deadlocked processes until deadlock no longer exists • (Re-invoke the deadlock detection algorithm each time)

Successively preempt some resources from processes and give them to other processes until deadlock no longer exists • a process that has a resource preempted must be rolled

back to a point prior to its acquisition of the resource

Page 15: Deadlock Avoidance Technique

Chapter 815

Deadlock Recovery (cont.)

Last approaches involve choosing a “victim” process :

Possible selection criteria:• least amount of CPU time consumed so far

• least total resources allocated so far

• least amount of “work” produced so far

• Lowest priority

• Largest estimated remaining time

• Dean’s processes last...