33
Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular Wait

Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Embed Size (px)

Citation preview

Page 1: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Deadlock Characterization

Deadlock can arise if four conditions hold simultaneously:

1. Mutual Exclusion2. Hold and Wait3. No Preemption4. Circular Wait

Page 2: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Mutual Exclusion

only one process at a time can use a resource.

If another process requests that resource, the requesting process must be delayed until the resource has been released

Page 3: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Hold and Wait

a process that holding at least one resource is waiting to acquire additional resources held by other processes.

Page 4: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

No preemption

a resource can be released only voluntarily by the process holding it, after that process has completed its task.

Page 5: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

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,

P1is waiting for a resource that is held by P2, …, Pn–1is waiting for a resource that is held by Pn, and P0is waiting for a resource that is held by P0.

Page 6: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Resource-Allocation Graph

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

V is partitioned into two types: 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.

E is also partitioned into two types: request edge –directed edge P1 →Rj assignment edge –directed edge Rj→Pi

Page 7: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

7.4 Deadlock Prevention We try to ensure that one of the four

necessary conditions cannot hold, then we can prevent it

Mutual Exclusion:If it is shareable resource, then we can

break the mutual exclusion (such as: Read-only file)

If it is not a shareable resource, then mutual exclusion must hold (such as: Printer)

Page 8: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

7.4 Deadlock Prevention Hold and wait: two methods

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

2. allow process to request resources only when the process has none.

Page 9: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Hold and Wait method example We consider a process that copies data from

DVD drive to a picture file on disk and then prints the picture to a printer

Method1: request DVD drive, Disk, and Printer before it execute. It will hold the Printer for entire execution, even though it needs the printer only at the end

Method2: request only for DVD drive and Disk initially. It finishes the copy step and release both resource. The process must then again ask for disk and printer to finish the job

Page 10: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Hold and Wait method example

Low resource utilization;

starvation possible, (if a process needs several popular resources)

Page 11: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

7.4 Deadlock Prevention NO preemption:

If a process that is holding some resources requestsanother resource that cannot be immediately to allocated it, then all resources currently being held are released.

Preempted resources are added to the list of resourcesfor which the process is waiting.

Process will be restarted only when it can regain its oldresources, as well as the new ones that it is requesting.

Page 12: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

7.4 Deadlock Prevention

Circular wait:impose a total ordering of all

resourcetypes, and require that each process requests resources in an increasing

order of enumeration.

Page 13: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Circular wait First of all we create a map function that

maps each resource, for example: F(tape drive) = 1, F(disk drive) = 5, F(Printer) =12

Then we have two rules for Processes to request Resources:

1. Each process can request resources only in an increasing order

2. Whenever a process requests an instance of resource Rj, it has released any resources Ri such that F(Ri) >= F(Rj)

Page 14: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Circular wait Let the set of processes involved in the circular

wait condition be {P0,P1,..,Pn}, where Pi is waiting for a resource Ri, which is held by Pi+1 (Pn is waiting for Rn which held by P0)

Since Pi+1 is holding Ri while requesting resource Ri+1, we must have F(Ri) < F(Ri+1) for all I

But this condition means that F(R0)<F(R1)<…<F(Rn)<F(R0)

By transitivity F(R0) < F(R0) which is impossible

Page 15: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

7.5 Deadlock Avoidance Possible side effects of preventing

deadlock are low device utilization and reduce system throughput

An alternative method for avoiding deadlocks is to require additional information about how resources are to be required.

Page 16: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

7.5 Deadlock Avoidance With this knowledge of the complete

sequence of requests and releases for each process the system can decide for each request whether or not the process should wait in order to avoid possible future deadlock

A deadlock avoidance algorithm dynamically examines the resource-allocation state to ensure that a circular wait condition can never happen

Page 17: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Safe State

A state is safe if the system can allocate resources to each processes in some order (safe sequence) and still avoid a deadlock

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

Page 18: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Safe State To illustrate, we consider a system has 12 magnetic

tape drives and 3 processes. P0 needs 10 tapes, P1 needs 4 and P2 needs 9.

Currently, P0 has 5, P1 has 2 and P2 has 2

Max needs Current needsP0 10 5 P1 4 2P2 9 2

At T0, the system is in safe state, since <P1,P0,P2> satisfied safe state condition

What if P2 currently ask for one more tape and has that one?

Page 19: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Safe, Unsafe, Deadlock

Page 20: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

7.5 Deadlock Avoidance The algorithm is simply to ensure

that the system will always remain in safe state.

Therefore, if a process requests a resource that is currently available, it may still have to wait.

Thus, resource utilization may be lower

Page 21: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Avoidance algorithms

Single instance of a resource type. Use a resource allocation graph

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

Page 22: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Resource-Allocation Graph Scheme Claim edge Pi → Rj indicated that process Pj

may request resource Rj in the future; represented by a dashed line.

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

Request edge converted to an assignment edge when the resource is allocated to the process.

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

Page 23: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Resource-Allocation Graph

Page 24: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Unsafe State In Resource-Allocation Graph

Page 25: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Resource-Allocation Graph Algorithm Suppose that process Pi requests a

resource Rj

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

Page 26: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Banker’s Algorithm Multiple instances. Each process must a claim

maximum use in advance. 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.

Page 27: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Banker’s Algorithm

Two algorithms need to be discussed:

1. Safety state check algorithm

2. Resource request algorithm

Page 28: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Data Structures for the Banker’s Algorithm

Page 29: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Safety Algorithm

Page 30: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Resource-Request Algorithm for Process Pi

Page 31: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Example of Banker’s Algorithm

Page 32: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Example of Banker’s Algorithm

Page 33: Deadlock Characterization Deadlock can arise if four conditions hold simultaneously: 1. Mutual Exclusion 2. Hold and Wait 3. No Preemption 4. Circular

Example of Banker’s Algorithm