chapter 8 deadlocks page 2 chapter 8: deadlocks system model deadlock characterization methods for...

61
Chapter 8 Deadlocks

Post on 20-Dec-2015

235 views

Category:

Documents


3 download

TRANSCRIPT

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

Chapter 8 Deadlocks

Page 2: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 2

Chapter 8: Deadlocks

System Model

Deadlock Characterization

Methods for Handling Deadlocks

Deadlock Prevention

Deadlock Avoidance

Deadlock Detection

Recovery from Deadlock

Page 3: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 3

System Model

A system consists of a finite number of resources to be distributed among competing processes.

Resources are partitioned into several types, each of which consists of multiple identical instances.

CPU cycles, memory space, files, I/O devices, object locks

§8.1

Page 4: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 4

System Model

Each process utilizes a resource as following sequence:

request• If the request cannot be granted

immediately, then the requesting process must wait until it can acquire the resource.

use • The process can operate on the resource.

release• The process releases the resource.

Page 5: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 5

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 Pn is waiting for a resource that is held by P0.

Deadlock can arise if four conditions hold simultaneously.

§8.2

Page 6: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 6

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 Pn is waiting for a resource that is held by P0.

Deadlock can arise if four conditions hold simultaneously.

§8.2

Multiple Choices Question:( ) If the processes in the system can be

preempted, no deadlock can happen.

Answer: ○

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

Page 7

Resource-Allocation Graph

V is partitioned into two types of nodes:P = {P1, P2, …, Pn}, the set consisting of all the processes in the system.

R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.

Request edge – directed edge Pi Rj

Assignment edge – directed edge Rj Pi

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

§8.2.2

Page 8: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 8

Resource-Allocation Graph

Process

Resource Type with 4 instances

Pi requests instance of Rj

Pi is holding an instance of Rj

A request edge points to only the square Rj, whereas an assignment edge must also designate one of the dots in the square.

Pi

Pi

Rj

Rj

Page 9: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 9

Resource Allocation Graph

Example:

Page 10: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 10

Resource Allocation Graph

Deadlocked:

Page 11: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 11

Resource Allocation Graph

With Cycle butNo Deadlock:

Page 12: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 12

Resource Allocation Graph

With Cycle butNo Deadlock:

Short Answer Question:Please add two edges to the resource allocation graph show below to make it deadlocked.

Page 13: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 13

Basic Facts

If graph contains no cycles no deadlock.

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

if several instances per resource type, possibility of deadlock.

Page 14: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 14

Basic Facts

If graph contains no cycles no deadlock.

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

if several instances per resource type, possibility of deadlock.

True-False Question:( ) In order to prevent deadlock, we

must ensure that no cycle can happen in the resource allocation graph associated with this system.

Answer: ×

Page 15: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 15

Methods for Handling Deadlocks

Use a protocol to ensure that the system will never enter a deadlock state.

§8.3

Allow the system to enter a deadlock state and then recover. The system can provide an algorithm that examines the state of the system to determine whether a deadlock has occurred, and an algorithm to recover from the deadlock.

Deadlock Prevention: (§8.4) a set of methods for ensuring that at least one of

the necessary conditions cannot hold.

Deadlock avoidance: (§8.5) give OS information about processes and resources for the decision of satisfying or

delaying the requests.Deadlock Detection: (§8.5)Deadlock Recovery: (§8.7)

Ignore the problem and pretend that deadlocks never occur in the system.

Page 16: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 16

Methods for Handling Deadlocks

Use a protocol to ensure that the system will never enter a deadlock state.

§8.3

Allow the system to enter a deadlock state and then recover. The system can provide an algorithm that examines the state of the system to determine whether a deadlock has occurred, and an algorithm to recover from the deadlock.Ignore the problem and pretend that deadlocks never occur in the system.

True-False Question:( ) If a system has deadlock

prevention scheme available, no deadlock detection or avoidance are required.

Answer: ○

Page 17: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 17

Methods for Handling Deadlocks

Use a protocol to ensure that the system will never enter a deadlock state.

§8.3

Allow the system to enter a deadlock state and then recover. The system can provide an algorithm that examines the state of the system to determine whether a deadlock has occurred, and an algorithm to recover from the deadlock.Ignore the problem and pretend that deadlocks never occur in the system.

True-False Question:( ) If a system has deadlock

prevention scheme available, no deadlock detection or avoidance are required.

True-False Question:( ) Usually deadlock recovery

method should be invoked after deadlock detection.

Answer: ○

Page 18: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 18

Methods for Handling Deadlocks

If no prevention, no avoidance, no detection and no recovery schemes, then the system may reach a deadlock state and eventually need to be restarted manually.

Although does not seem to be viable, it is used in most OS, including UNIX.

In many systems, deadlocks occur infrequently; thus it is cheaper to use this method.

JVM does nothing to manage deadlocks, it is up to the application developer to write programs that are deadlock-free.

Page 19: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 19

Methods for Handling Deadlocks

If no prevention, no avoidance, no detection and no recovery schemes, then the system may reach a deadlock state and eventually need to be restarted manually.

Although does not seem to be viable, it is used in most OS, including UNIX.

In many systems, deadlocks occur infrequently; thus it is cheaper to use this method.

JVM does nothing to manage deadlocks, it is up to the application developer to write programs that are deadlock-free.

True-False Question:( ) Although the operating systems

today are very sophisticated, most of them has no deadlock manipulation schemes available.

Answer: ○

Page 20: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 20

Deadlock Prevention

By ensuring that at least one of the conditions (§8.2) cannot hold, we can prevent the occurrence of a deadlock.

1. Mutual Exclusion – not required for sharable resources, but must hold for nonsharable resources (printers, synchronized method)

§8.4

Page 21: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 21

Deadlock Prevention

By ensuring that at least one of the conditions (§8.2) cannot hold, we can prevent the occurrence of a deadlock.

1. Mutual Exclusion – not required for sharable resources, but must hold for nonsharable resources (printers, synchronized method)

§8.4

True-False Question:( ) Mutual exclusion is not required

for nonsharable resources in a multi-threaded environment.

Answer: ×

Page 22: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 22

Deadlock Prevention

2. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.

Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none… release all the resources that is is currently allocated before requesting additional resources.Low resource utilization; starvation possible.Impractical in Java (difficult to implement these protocols with synchronized methods or blocks).

Page 23: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 23

Deadlock Prevention

2. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.

Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none… release all the resources that is is currently allocated before requesting additional resources.Low resource utilization; starvation possible.Impractical in Java (difficult to implement these protocols with synchronized methods or blocks).

Page 24: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 24

Deadlock Prevention

2. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.

Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none… release all the resources that is is currently allocated before requesting additional resources.Low resource utilization; starvation possible.Impractical in Java (difficult to implement these protocols with synchronized methods or blocks).

Page 25: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 25

Deadlock Prevention

2. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.

Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none… release all the resources that is currently allocated before requesting additional resources.Low resource utilization; starvation possible.Impractical in Java (difficult to implement these protocols with synchronized methods or blocks).

THINKTHINK

Page 26: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 26

Deadlock Prevention

2. Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.

Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none… release all the resources that is currently allocated before requesting additional resources.

Low resource utilization; starvation possible.

Multiple Choices Question:( ) Breaking the “Hold and wait”

condition in a multi-threaded system may cause(a) deadlock (b) mutual exclusive (c) starvation (d) implementation problem

Answer: c

Page 27: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 27

Deadlock Prevention (Cont.)

No Preemption –If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released.Preempted resources are added to the list of resources for which the process is waiting.Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.Often applied to resources whose state can be easily saved and restored later, such as CPU registers and memory space. It cannot be applied to resources such as printers and tape drives.

Page 28: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 28

Deadlock Prevention (Cont.)

Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.Let R={R1,R2, …, Rm} be the set of resource types.F:R→NF(type drive) =1F(disk drive) = 5F(printer) = 12A process owning resource type Ri can continue to request type Rj iff F(Rj)>F(Ri)orWhenever a process requests an instance of resource type Rj, it has released any resources Ri such that F(Ri) >=F(Rj).

Should be defined according to the normal order of usage of

the resources in a system.

Page 29: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 29

Proof by contradiction

Assume circular wait exists in {P0, P1, ..., Pn}, where Pi is waiting for a resource Ri, which is held by process Pi+1.

F(Ri) < F(Ri+1)=>F(R0) < F(R1) < ... <F(Rn) <F(R0)=> F(R0) < F(R0)

Impossible

Page 30: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 30

Deadlock Avoidance

Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need.The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition.Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.

Requires that the system has some additional a priori information available.

§8.5

Page 31: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 31

Safe State

A state is safe if the system can allocate resources to each process in some order and still avoid a deadlock.

Safe sequence <P1, P2, ..., Pn> :if, for each Pi, the resources that Pi can still request can be safisfied by the currently available resources plus the resources held by all the Pj with j < i.

If no such sequence exists, then the system is said to be unsafe.

§8.5.1

Page 32: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 32

Safe State

A safe state is not a deadlock state.

A deadlock state is an unsafe state.

Not all unsafe states are deadlocks.

An unsafe state may lead to a deadlock.

In unsafe state, OS cannot prevent processes from requesting resources and cause deadlock: the behavior of the processes controls unsafe state.

Page 33: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 33

Safe State

Example: 12 tape drives, 3 processes. max needs current holdingP0 10 5P1 4 2P2 9 2

Safe for <P1,P0,P2>

If P2 request one more, system go into unsafe state.

Page 34: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 34

Basic Facts

If a system is in safe state no deadlocks.

If a system is in unsafe state possibility of deadlock.

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

Page 35: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 35

Basic Facts

If a system is in safe state no deadlocks.

If a system is in unsafe state possibility of deadlock.

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

True-False Question:( ) It is not necessary that a system

will become deadlocked even when the system reaching a unsafe state.

Answer: ○

Page 36: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 36

Resource-Allocation Graph Algorithm

Claim edge Pi Rj indicated that process Pi may request resource Rj at some time in the future. It is represented by a dashed line.

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

When the resource is allocated to the process, assignment edge is added.

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

P1 R1

§8.5.2

Page 37: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 37

Safe StateA request can be granted only if converting the request edge to assignment edge does not result in the formation of a cycle in the resource-allocation graph.

If no cycle exists, then the allocation of the resource will leave the system in a safe state. Otherwise, process have to wait.

Page 38: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 38

Banker’s Algorithm (From SHAY)

Multiple instances.

Each process must a priori claim maximum use.

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

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

§8.5.3

Page 39: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 39

• Safe• Process 1,2, or 4 could request its maximum

number of pages and finish• For example: process 1 finish

Current Number of pages allocated

Maximum Number of pages needed

Process 1 2 4 Process 2 3 6Process 3 1 6Process 4 4 8

14 pages in the system4 pages are unallocated

Example 1

Page 40: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 40

• Now any process could request pages up to its maximum and finish

• Suppose process 3 finish

Current Number of pages allocated

Maximum Number of pages needed

Process 1 2 4 Process 2 3 6Process 3 1 6Process 4 4 8

14 pages in the system6 pages are unallocated

Example 1

Page 41: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 41

• Any one can finish.• However, a sequence of requests that allowed

all processes to finish does not indicate a safe state.

Current Number of pages allocated

Maximum Number of pages needed

Process 1 2 4 Process 2 3 6Process 3 1 6Process 4 4 8

14 pages in the system7 pages are unallocated

Example 1

Page 42: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 42

• If process 3 requests the 4 available pages, and each process subsequently requests 1 more page, the system is deadlocked

• However, it doesn't necessarily occur. ....A unsafe state doesn't mean that the deadlock will occur inevitably.

Current Number of pages allocated

Maximum Number of pages needed

Process 1 2 4 Process 2 3 6Process 3 1 5 6Process 4 4 8

14 pages in the system0 pages are unallocated

Example 1

Page 43: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 43

Current Number of pages allocated

Maximum Number of pages needed

Process 1 1 2 Process 2 2 4Process 3 2 7Process 4 1 6

7 pages in the system1 pages are unallocated

Example 2

Page 44: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 44

Current Number of pages allocated

Maximum Number of pages needed

Process 1 1 2 Process 2 2 4Process 3 2 7Process 4 1 6

7 pages in the system1 pages are unallocated

Process 1 1 2 Process 2 2 4Process 3 2 7Process 4 1 6

7 pages in the system2 pages are unallocated

Example 2

Page 45: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 45

If each process asserts its claim, deadlock will occur

Unsafe !

Current Number of pages allocated

Maximum Number of pages needed

Process 1 1 2 Process 2 2 4Process 3 2 7Process 4 1 6

7 pages in the system1 pages are unallocated

Process 1 1 2 Process 2 2 4Process 3 2 7Process 4 1 6

7 pages in the system2 pages are unallocated

Process 1 1 2 Process 2 2 4Process 3 2 7Process 4 1 6

7 pages in the system4 pages are unallocated

Example 2

Page 46: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 46

Banker’s Algorithm

Safe state. <P1, P3, P4, P2, P0>

If request1=(1,0,2)

§8.5.3

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

Allocation Max Available A B C A B C A B C

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

Need A B C

Page 47: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 47

Banker’s Algorithm

Safe state. <P1, P3, P4, P2, P0>

If request1=(1,0,2)

Still safe with < P1, P3, P4, P0, P2 >

§8.5.3

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

Allocation Max Available A B C A B C A B C

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

Need A B C

Page 48: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 48

Banker’s Algorithm

Safe state. <P1, P3, P4, P2, P0>

Request4=(3,3,0)

Cannot be granted. No resources.

§8.5.3

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

Allocation Max Available A B C A B C A B C

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

Need A B C

Page 49: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 49

Banker’s Algorithm

Safe state. <P1, P3, P4, P2, P0>

Request0 =(0,2,0)

Will result in unsafe state.

§8.5.3

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

Allocation Max Available A B C A B C A B C

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

Need A B C

Page 50: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 50

Deadlock Detection

Allow system to enter deadlock state

System should provide:An algorithm that examines the state of the system to determine whether a deadlock has occurred.

An algorithm to recover from the deadlock.

It requires overhead that includesmaintaining necessary information

executing the detection algorithm

potential losses when recovering

§8.6

Page 51: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 51

Deadlock Detection

Allow system to enter deadlock state

System should provide:An algorithm that examines the state of the system to determine whether a deadlock has occurred.

An algorithm to recover from the deadlock.

It requires overhead that includesmaintaining necessary information

executing the detection algorithm

potential losses when recovering

§8.6

True-False Question:( ) Deadlock prevention is not

required for a system that equipped with proper deadlock detection scheme.

Answer: ×

Page 52: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 52

Single Instance of Each Resource Type

Maintain wait-for graph: obtained from the resource-allocation graph by removing the nodes of type resource and collapsing the appropriate edges.

Nodes are processes.

Pi Pj if Pi is waiting for Pj. to release a resource that Pi needs.

Needs to maintain the wait-for graph and periodically invoke an algorithm that searches for a cycle in the graph.An algorithm to detect a cycle in a graph requires an order of O(n2) operations, where n is the number of vertices in the graph.

§8.6.1

Page 53: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 53

Resource-Allocation Graph And Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph

Page 54: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 54

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 deadlocks occur frequently, then the detection algorithm should be invoked frequently.In the extreme, we could invoke the deadlock-detection algorithm every time a request for allocation cannot be granted immediately. Expensive

Page 55: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 55

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 deadlocks occur frequently, then the detection algorithm should be invoked frequently.In the extreme, we could invoke the deadlock-detection algorithm every time a request for allocation cannot be granted immediately. Expensive

True-False Question:( ) Deadlock detection algorithm

should be invoked only when the system is deadlocked. It therefore examines the system allocation situation and prepare for a roll back.

Answer: ×

Page 56: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 56

Detection-Algorithm Usage

Less expensive alternative:invoke the algorithm at less frequent intervals.

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 57: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 57

Several Instances of a Resource Type

Not deadlocked. <P0, P2, P3, P1, P4>

If P2 make request (0, 0, 1), Deadlocked between P1, P2, P3, P4.

§8.6.2

P0 0 1 0 0 0 0 0 0 0P1 2 0 0 2 0 2 P2 3 0 3 0 0 0 P3 2 1 1 1 0 0P4 0 0 2 0 0 2

Allocation Request Available A B C A B C A B C

Page 58: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 58

Several Instances of a Resource Type

Not deadlocked. <P0, P2, P3, P1, P4>

If P2 make request (0, 0, 1), Deadlocked between P1, P2, P3, P4.

§8.6.2

P0 0 1 0 0 0 0 0 0 0P1 2 0 0 2 0 2 P2 3 0 3 0 0 1 P3 2 1 1 1 0 0P4 0 0 2 0 0 2

Allocation Request Available A B C A B C A B C

Page 59: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 59

Recovery from Deadlock: Process Termination

Abort all deadlocked processes.Expensive to discard and re-compute partial computation.

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

overhead for invoking deadlock-detection algorithm after each process is aborted.

§8.7

Page 60: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 60

Recovery from Deadlock: Process TerminationWe should abort those processes that will incur the minimum cost when terminated.

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 61: Chapter 8 Deadlocks Page 2 Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance

Page 61

Recovery from Deadlock: Resource PreemptionSuccessively preempt some resources from processes and give these resources to other processes until the deadlock cycle is broken. Three matters addressed:

Selecting a victim – Which resources and which processes are to be preempted? We must determine the order to minimize cost. Factors include:

• Number of resources a deadlock process is holding

• the amount of time a deadlocked process has consumed

Rollback – return to some safe state, restart process fro that state. Requires the system to keep more information about the state of all the running processes.

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