Top Banner
Chapter 7: Deadlock 11.4.2008
26

Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Jan 17, 2018

Download

Documents

Walter Johnson

Outline n What is a deadlock? n How to deal with a deadlock? n Deadlock detection n Deadlock prevention n Single-instance: wait-for graph n Multi-instance: banker’s algorithm
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Chapter 7: Deadlock

11.4.2008

Page 2: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Course Administration

Midterm exam next week in class Closed book

Final exam time changed to 1.13.2009 during class time. Avoid conflict with other courses.

Page 3: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Outline

What is a deadlock? How to deal with a deadlock? Deadlock detection Deadlock prevention Single-instance: wait-for graph Multi-instance: banker’s algorithm

Page 4: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

4

Deadlocks

X(A) = request exclusive lock A Wait on mutex semaphore A

T1 and T2 will make no further progress.

When can deadlock occur? How to handle deadlock?

Prevent deadlocks from occurring. Detection deadlocks and resolve

them.

How to do deadlock prevention and detection?

T1 T2

X(A)

X(B)

Request X(B)Blocked!

Request X(A)Blocked too!

Page 5: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Deadlock 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, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0.

Deadlock can arise if four conditions hold simultaneously.

Page 6: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

6

Single-instance Deadlock Detection

Resource type: Single-Instance: mutex Multi-Instance: CPUs

Create a waits-for graph: Nodes are processes/transactions There is an edge from Ti to Tj if Ti is

waiting for Tj to release a lock Periodically check for cycles in

the waits-for graph, cycle = Deadlock Resolve a deadlock by aborting a

process a cycle.

T1 T2

S(A)

R(A) S(B)

R(B)

X(B): W(B)

X(A): W(A)

Page 7: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Single-Instance Wait-For-Graph

7

T1 T2

T4 T3

T1 T2

T4 T3

T1 T2 T3 T4S(A)R(A)

X(B)W(B)

S(B)S(C)R(C)

X(C)X(B)

X(A)

Page 8: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

8

Single-instance Deadlock Prevention

Make sure that deadlock will not occur. How to do it?

Assign priorities to transactions based on timestamps when they start up. (lower timestamps = higher priority)

Lower priorities cannot wait for higher-priority transactions.

Page 9: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

9

Single-instance Deadlock Prevention

Say Ti wants a lock that Tj holds. What would a deadlock prevention policy do? Wait-Die: If Ti has higher priority, Ti waits for Tj; otherwise Ti

aborts (lower-priority T never waits for higher-priority T) Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti

waits. (higher-priority T never waits for lower-priority T)

Why these two policies prevent deadlocks? The oldest transaction (highest priority one) will eventually

get all the locks it requires!

Page 10: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

10

Wait-Die Policy

Lower-priority process never waits for higher-priority process

If Ti has higher priority, Ti waits for Tj;

Otherwise Ti aborts

T1 T2 T3 T4S(A)

R(A)

X(B)

W(B)

S(B)

S(C)

R(C)

X(C)

X(B) // abort

X(A) // abort

Page 11: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

11

Wound-Wait Policy

Higher-priority process never waits for lower-priority T

If Ti has higher priority, Tj aborts;

Otherwise Ti waits.

T1 T2 T3 T4S(A)

R(A)

X(B)

W(B)

S(B) // Abort T2 Abort

Page 12: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

12

Single-instance Deadlock Prevention (2)

If a transaction re-starts, make sure it has its original timestamp. It will not be forever aborted due to low priority.

Page 13: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Multi-instance Deadlock

Single instance of a resource type. Detection: Use a wait-for graph

Multiple instances of a resource type – Banker’s algorithm Deadlock-free or safe state detection : safety algorithm Deadlock prevention: resource-request algorithm Deadlock detection: detection algorithm

All three algorithms use similar data structure.

Page 14: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

16:35 /4314

Multi-instance example

P1 P2 P3

R2 R1

Page 15: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Common Data Structure

Let n = number of processes, and m = number of resources types. Available: Vector of length m. If available [j] = k, there are k

instances of resource type Rj available.

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

Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated 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] – Allocation [i,j].

Page 16: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Safety Algorithm

Safety algorithm means … Is the system (given Available, Need, Allocation matrices) deadlock free? Does there exist a schedule for all processes to complete their execution

without a deadlock?1. Let Work and Finish be vectors of length m and n, respectively.

Initialize:Work = AvailableFinish [i] = false for i = 0, 1, …, n- 1.

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 + Allocationi Finish[i] = true go to step 2.

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

Page 17: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Example of Safety Algorithm

5 processes P0 through P4;

3 resource types: A (10 instances), B (5instances), and C (7 instances).

Snapshot at time T0:Allocation NeedAvailableA B C A B C A B CP0 0 1 0 7 4 3 3 3 2 P1 2 0 0 1 2 2 P2 3 0 2 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1

A safe sequence <P1, P3, P4, P2, P0>

Page 18: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Resource-Request Algorithm for Process Pi

Can a request be safety granted without causing a deadlock?

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

1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.

2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available.

3. Pretend to allocate requested resources to Pi by modifying the state as follows:Available = Available – Request;

Allocationi = Allocationi + Requesti;

Needi = Needi – Requesti; If safe the resources are allocated to Pi. If unsafe Pi must wait, and the old resource-allocation state is restored

Page 19: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Example of Resource-Request Algorithm

5 processes P0 through P4;

3 resource types: A (10 instances), B (5instances), and C (7 instances).

Snapshot at time T0, say P1 Request (1,0,2)Allocation NeedAvailableA B C A B C A B CP0 0 1 0 7 4 3 3 3 2 P1 2 0 0 1 2 2 P2 3 0 2 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1

A safe sequence <P1, P3, P4, P0, P2>

Page 20: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Detection Algorithm

Check if there is a deadlock in the system.

1. Let Work and Finish be vectors of length m and n, respectively Initialize:(a) Work = Available(b) For i = 1,2, …, n, if Allocationi 0, then Finish[i] = false;otherwise,

Finish[i] = true.2. Find an index i such that both:

(a) Finish[i] == false(b) Requesti WorkIf no such i exists, go to step 4.

3. Work = Work + Allocationi

Finish[i] = truego to step 2.

4. If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked.

Page 21: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Example of Detection Algorithm

Five processes P0 through P4; three resource types A (7 instances), B (2 instances), and C (6 instances).

Snapshot at time T0: Allocation Request Available

A B C A B C A B CP0 0 1 0 0 0 0 0 0 0P1 2 0 0 2 0 2P2 3 0 3 0 0 0 P3 2 1 1 1 0 0 P4 0 0 2 0 0 2

Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.

Page 22: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Example (Cont.)

P2 requests an additional instance of type C.Request

A B C P0 0 0 0 P1 2 0 1P2 0 0 1P3 1 0 0 P4 0 0 2

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

resources to fulfill other processes; requests. Deadlock exists, consisting of processes P1, P2, P3, and P4.

Page 23: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Detection-Algorithm Usage

When, and how often, to invoke depends on: How often a deadlock is likely to occur? How many processes will need to be rolled back?

one for each disjoint cycle

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

Page 24: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Recovery from Deadlock: Process Termination

Abort all deadlocked processes.

Abort one process at a time until the deadlock cycle is eliminated.

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?

Page 25: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

Summary

What is a deadlock? How to deal with a deadlock?

Deadlock detection Deadlock prevention

Single-instance: wait-for graph Multi-instance: banker’s algorithm

Safety algorithm Resource-allocation algorithm Detection algorithm

Page 26: Chapter 7: Deadlock 11.4.2008. Course Administration n Midterm exam next week in class l Closed book n Final exam time changed to 1.13.2009 during class.

End of Chapter 7