Top Banner
1 Deadlock
46
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: Deadlock

1

Deadlock

Page 2: Deadlock

2

Deadlock Def. A set of processes is deadlocked if each process in the

set is waiting for an event that only another process in the set can cause.

Necessary Conditions for Deadlock

1 Mutual Exclusion: Processes claim exclusive control of the resources they require.

2 Hold and Wait: Processes hold resources already allocated to them while waiting for additional resources.

3 No Preemption: Resources cannot be forcibly removed from the process holding them until the resources are used to completion.

4 Circular Wait: A circular chain of processes exists such that each process hold one or more resources that are being requested by the next process in the chain.

Page 3: Deadlock

3

Dealing with Deadlock

Three principle strategies for dealing with deadlock

Detection: How can deadlock be identified?

Recovery: What are the “best” ways to recover from deadlock?

Prevention (and Avoidance): How can deadlock be prevented in the first place?

o Avoidance: Can we avoid deadlock through careful allocation scheme?

Page 4: Deadlock

4

Relevant Events

• A process follows the following sequence to use resources:

– Request (resource)

– Use (resource)

– Release (resource)

• The three important events are when the process

– requests,

– acquires, and

– releases

resources.

Page 5: Deadlock

5

“Claim” (Future-Request) Edges

Tape 2

P1

Tape 1

P2

Tape 3

P3

Page 6: Deadlock

6

Claim Request

Tape 2

P1

Tape 1

P2

Tape 3

P3

Page 7: Deadlock

7

Request Assignment

Tape 2

P1

Tape 1

P2

Tape 3

P3

Page 8: Deadlock

8

Safe: No Cycle

Tape 2

P1

Tape 1

P2

Tape 3

P3

Page 9: Deadlock

9

A Dangerous Request

Tape 2

P1

Tape 1

P2

Tape 3

P3

Page 10: Deadlock

10

See Any Cycles?

Tape 2

P1

Tape 1

P2

Tape 3

P3

Page 11: Deadlock

11

A System Model A system is a pair (S,P) where S is a set of system states {S,T,U,V,...} and

P is a set of processes {P1 ,P2,...}.

A process Pi is a partial function from system states into nonempty subsets of system states,

Pi: S 2S

Def. A process Pi is blocked in state S if there exists no T such that S i T.(A process is blocked in a given state if it can't change state.)

Def. A process Pi is deadlocked in state S if for all T such that

S * T, Pi is blocked inT.

Ex1. P2 is blocked (and deadlocked) in both U and V.

Ex2. P1 is blocked but not deadlocked in T.

Def. A state S is called a deadlock state if there exists a process Pi that is deadlocked in S.

Def. A state S is a safe state if for all T such that S i T, T is not a deadlock state.

Page 12: Deadlock

12

Example

VU

TS

P = {S,T,U,V}

P = {P1,P2}

P1(S) = {T,U}

P1(U) = {V}

P2(S) = {U}

1

2

1 2

1

1

2

Page 13: Deadlock

13

Resource (Allocation) Graph (RAG) A directed graph is a pair (N,E), where N is a set of nodes

and E is a set of ordered pairs (a,b), a,b N, called edges.

Def. A RAG is a directed graph with N = P R

where P = {P1 ,...,Pn} a set of process nodes and R = {R1 ,...,Rm} a set of resource nodes.

• The graph is “bipartite” with respect to P and R.

• An edge (Pi ,Rj) is called a request edge (request by Pi for 1 unit of Rj).

• An edge (Rj ,Pi) is called an assignment edge (allocation of 1 unit of Rj to Pi).

• For each resource Ri R, there exists a non-negative integer tI denoting the number of units of Ri.

Page 14: Deadlock

14

Invariants on RAG

Let |(a,b)| be the number of edges directed for node a to node b.

Then

j |(Ri ,Pj)| ti for all i.(No more than ti assignments (allocation) may be made for Ri.)

|(Ri ,Pj)| + |(Pj ,Ri)| ti for all i and j.(The sum of the requests and allocation of any process for a particular resource cannot exceed the available units.)

Page 15: Deadlock

15

State Transitions

The system state is changed to a new state only as a result of requests, releases, or acquisitions of resources by a single process.

1 Request. If a system is in state S and process Pi has no

requests outstanding (no request edges), then Pi

may request any # of resources.The system then enters state T, say

3 Release. Pi can cause a state change from S to T by a release operation iff Pi has no requests and some allocations. Pi may release any nonempty subset of its resources in this operation.

S Ti .

Page 16: Deadlock

16

2 Acquisition. A system can change from state S to state T by an acquisition operation by iff Pi has

outstanding requests and all such requests can be satisfied; for all resources Rj such that (Pi , Rj) E,

we have

A process Pi is blocked if it is unable to perform any of these operations: 1, 2, or 3. That is, if there exists at least one resource Rj such that

State Transitions (con’d)

|(P,R )| |(R ,P )| ti j j k jk

|( , )| |( , )|P R R P ti j j k jk

S Ti

Page 17: Deadlock

17

Reduction on RAG

A RAG is reduced by a process Pi , which is neither blocked nor an isolated node, by removing all edges to and from Pi .

A RAG is irreducible if the graph cannot be reduced by any process. A RAG is completely reducible if there exists a sequence of reductions that deletes all edges of the graph.

Page 18: Deadlock

18

Theorems

Theorem 1: S is a deadlock state iff the RAG of S is not completely reducible.

Cor. 1: A process Pi is not deadlocked iff a series of reductions leaves a state in which Pi is not blocked.

Cor. 2: If S is a deadlock state, then at least two processes are deadlocked in S.

Theorem 2: A cycle in a RAG is a necessary condition for deadlock.

Theorem 3: If S is not a deadlock state and then T is a deadlock state iff the operation by Pi is a request and Pi is deadlocked in T.

S Ti ,

Page 19: Deadlock

19

Data structures for RAG

RAG can be represented by

1 An allocation matrix A, where Aij=| ( Pi , Rj ) | for i = 1,…,n, j = 1,…,m.

2 A request matrix B, where Bij=| ( Pi , Rj ) | for i = 1,…,n, j = 1,...,m.

will use Bi to denote i-th row, i.e.,

Bi = ( Bi1 ,..., Bim ).

3 An available vector T, where

Ti = # of available unit for Ri, i = 1,...,n.

Page 20: Deadlock

20

Deadlock Detection Algorithm

L := {};repeat L' := L; for i:=1 to n do if Pi not in L and Bi <= T then T := T + Ai; L := L U {Pi}; end if end for until L = L';Deadlock := not( L = {Pi, ..., Pn})

Page 21: Deadlock

21

Example

A | R1 R2 R3 B | R1 R2 R3--------------- ---------------P1 | 1 1 1 P1 | 3 2 1P2 | 1 1 1 P2 | 2 2 1P3 | 1 1 1 P3 | 1 1 1P4 | 1 1 1 P4 | 0 0 0

T = (0, 0, 0).

Inspection order P1, P2, ...Reduction order Pn, Pn-1, ...# of process inspections = n + (n-1) + ... = n(n+1)/2So worst-case exec. time = O(mn2)

Page 22: Deadlock

22

Recovery

• Recovery through preemption

• Recovery through rollback

• Recovery through killing processes

Page 23: Deadlock

23

Prevention• Eliminate possibilities

• Techniques

– Serialization (Prevention)

– One-shot allocation (Prevention)

– Hierarchical allocation (Prevention)

– Banker’s algorithm (Avoidance)

Page 24: Deadlock

24

Serialization

1 Only one process may hold resources at any time.

2 Very inefficient use of resources

Page 25: Deadlock

25

One-shot Allocation

1 A process may only request all its resources at one time. It is blocked until the entire request can be satisfied.

2 Resources are locked even if they are not in use.

3 This method may be necessary for real-time processes that must be guaranteed not to wait for resource allocation once they are underway.O.w., it is too conservative.

Page 26: Deadlock

26

Hierarchical Allocation

Algorithm:

1. Resources are grouped into levels.

2. A process may only request resources at levels higher than any resource currently held by that process.

3. Resources may be released in any order.

Page 27: Deadlock

27

Proof that deadlock cannot occur

Proof by Induction: Assume N is highest and 0 is lowest.

Induction hypothesis: Resources requested at levels i will always be acquired and released in a finite time. (No circular wait is possible.)

Induction basis: The hypothesis is true for i = highest level N.

Induction step:

1. Suppose a process has requested resources at level i-1..

2. It will be delayed if other processes have those resources.

3. Each of these other processes must release them eventually, or be blocked waiting for resources at level i or higher.

4. By induction hypothesis, this blockage cannot last forever.

Page 28: Deadlock

28

Properties

1. When all requests are at the same level, this method is equivalent to one-shot allocation.

2. Resources at lower levels are blocked for longer periods, but those at higher levels are shared well. Thus, place the scariest resources at the highest levels so that requests for them will be made only when they are actually needed by a process.

3. This method works well when the resources are semaphores.

semaphore S1,S2,S3 P(S1,S2,S3) P(S1) P(S2) P(S2) P(S3) P(S3) P(S1)

V(S1,S2,S3) order of V's doesn't matter

Page 29: Deadlock

29

Avoidance

• The question is: “Is there an algorithm that can always avoid deadlock by making the right choice all the time?”

• Deadlock is the result of granting a resource.

• Banker’s algorithm

Page 30: Deadlock

30

Banker's Algorithm• Each process starts with a claim. A process

may never request more than its claim.(However, the sum of the claims of all process may exceed the number of resources.)

• The current allocation state is kept separately for each resource type:(a) For each process: (1) claim (2) holdings (acquired resources) (3) outstanding request (if process is blocked for allocation)(b) Amount of unallocated resources.

Page 31: Deadlock

31

Example

Who Max Has RoomP0 10 5 5P1 4 2 2P2 9 2 7System 12 3 -"Is it safe?""Yes, it's safe; it's very safe, so safe youwouldn't believe it."

Page 32: Deadlock

32

P1: 2 4

Who Max Has Room

P0 10 5 5 P1 4 2 4 2 0 P2 9 2 7 System 12 3 –

Page 33: Deadlock

33

P1: complete

Who Max Has Room P0 10 5 5 P2 9 2 7 System 12 1 5 -

Page 34: Deadlock

34

P0: 5 10

Who Max Has RoomP0 10 10 0P2 9 2 7System 12 0 -

Page 35: Deadlock

35

P0: complete

Who Max Has RoomP2 9 2 7System 12 10 -

Safe sequence is: P1, P0, P2So the current state is safe.

Page 36: Deadlock

36

Example (from text)

Who Max Has RoomP0 10 5 5P1 4 2 2P2 9 2 7System 12 3 -Can P2 ask for more?"Is it safe?""No, it's not safe, it's very dangerous, be careful."

Page 37: Deadlock

37

P2: 2 3?

Who Max Has RoomP0 10 5 5P1 4 2 2P2 9 3 6System 12 2 -

Only P1 can be satisfied without waiting

Page 38: Deadlock

38

P1: 2 4?

Who Max Has RoomP0 10 5 5P1 4 4 0P2 9 3 6System 12 0 -

Page 39: Deadlock

39

P1: complete?

Who Max Has RoomP0 10 5 5P2 9 3 6System 12 4 -

P0 and P2 can each ask for >4If both do, each will wait for other: deadlock

Page 40: Deadlock

40

Safe state

o An allocation state is realizable if(a) each claim maximum available.(b) each process is holding its claim.(c) the total amount of held resources is the total available.Otherwise, the allocation state is unrealizable.

o A realizable state is safe if there is a sequence of processes, P1 ,...,Pn ,(a safe sequence) such that: P1 can finish (i.e., there are enough unallocated resources to satisfy its claim.)

o In general, Pi can finish if Pi-1 releases its current holding.

o The state is safe because we can avoid deadlock at the moment by blocking any new processes (or any new claims) until all the current processes have finished in the safe order.

Page 41: Deadlock

41

Example

(One resource class only)

process holding max claims A 4 6 B 2 7 C 4 11 unallocated: 2 safe sequence: A,B,C

– If B should have a claim of 9 instead of 7, there is no safe sequence.

Page 42: Deadlock

42

Unsafe state

1. An unsafe state is deadlock free if there is a sequence of processes, P1 ,...,Pn , (a deadlock free sequence) such that P1 might finish. (There are enough unallocated resources to satisfy its current outstanding requests, but not necessarily its entire claim.)

2. In general, Pi might finish if Pi-1 does.

3. The state is deadlock free since no process is waiting. However, it may be unsafe because the processes may now request resource that put them in a deadlock state, no matter what action the allocator takes.

Page 43: Deadlock

43

Example

process holding max claims outstanding requests A 4 6 2 B 2 9 6 C 4 11 7unallocated: 2deadlock-free sequence: A,B,C

However, this sequence is not safe: if B should have 7 instead of 6 outstanding requests, deadlock exists.

Page 44: Deadlock

44

Banker’s algorithm

The Banker's Algorithm: satisfy a request iff the resulting state is safe.

The Banker's Algorithm is conservative: it cautiously avoids entering an unsafe state even if this unsafe state has no deadlock.It also requires prior claims.

deadlockunsafe

safe

Page 45: Deadlock

45

Implementation method

Find a safe sequence whenever a request is made. If unsuccessful, block the requester. When a resource is released, consider again allocating resources to blocked processes.

• The cost of finding a sequence is O(n2) not O(n!).

• If more than one resource type, the same sequence must work for resources of all types.

• There is more efficient algorithm by Habermann.

Example. five processes: P0 , P1 , P2 , P3 , P4

three resource types: A, B, C with 10, 5, 7 units.At time T0 (Max = Allocation + Need)

Page 46: Deadlock

46

Example Allocation Max Need Available

A B C A B C A B C A B CP0 0 1 0 7 5 3 7 4 3 3 3 2P1 2 0 0 3 2 2 1 2 2 P2 3 0 2 9 0 2 6 0 0P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1safe sequence <P1, P3, P4, P2, P0>

Suppose that P1 requests (1,0,2). To decide whether or not to grant this request, Allocation Need P1 3 0 2 0 2 0 2 3 0Again, safe seq <P1, P3, P4, P0, P2>

In this new state,P4 requests (3,3,0) not enough available resources P0 requests (0,2,0) unsafe state? Why?