deadlocksdeadlocks cns 3060. imagine a narrow set of stairs, only wide enough for one person to...

130
Deadlocks Deadlocks CNS 3060

Post on 22-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

DeadlocksDeadlocksDeadlocksDeadlocks

CNS 3060CNS 3060

Page 2: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Imagine a narrow set of stairs, only wide enough for one person to pass.

However, the landings are big enough for several people to stand.

Page 3: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Wait

Page 4: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock

Page 5: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Preemptable and Non-preemptable Preemptable and Non-preemptable ResourcesResources

A preemptable resource is one that can be temporarily taken way from the process owning it without any ill effects.

A non-preemptable resource is one that cannot be taken away without causing the computation to fail.

Page 6: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Using Semaphores to Acquire Using Semaphores to Acquire and Use a Resourceand Use a Resource

wait(&semaphore_1); use_resource_1; signal(&semaphore_1)

Page 7: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Using Semaphores to Acquire and Using Semaphores to Acquire and Use Two ResourcesUse Two Resources

wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

Page 8: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Two Processes Using Semaphores Two Processes Using Semaphores to Acquire and Use Two Resourcesto Acquire and Use Two Resources

wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2)

Process A Process B

Page 9: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2)

Process A Process B

1 10

Time slice expires…

semaphore 1 semaphore 2

Two Processes Using Semaphores Two Processes Using Semaphores to Acquire and Use Two Resourcesto Acquire and Use Two Resources

Page 10: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2)

Process A Process B

10 0D E A D L

O C K !

semaphore 1 semaphore 2

Two Processes Using Semaphores Two Processes Using Semaphores to Acquire and Use Two Resourcesto Acquire and Use Two Resources

Page 11: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

wait(&semaphore_2); wait(&semaphore_1); use_resource_1; use resource_2; signal(&semaphore_1); signal(&semaphore_2)

Process A Process B

Can you fix it so no deadlock occurs?

Two Processes Using Semaphores Two Processes Using Semaphores to Acquire and Use Two Resourcesto Acquire and Use Two Resources

Page 12: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

wait(&semaphore_1); wait(&semaphore_2); use_resource_1; use resource_2; signal(&semaphore_2); signal(&semaphore_1)

Process A Process B

Two Processes Using Semaphores Two Processes Using Semaphores to Acquire and Use Two Resourcesto Acquire and Use Two Resources

Page 13: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Some Examples of Some Examples of DeadlockDeadlock

Some Examples of Some Examples of DeadlockDeadlock

Page 14: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Some Examples of DeadlockDeadlock on a file request

A System built for contractors has two files (among others) - one for suppliers - one for inventory

It also has two applications (among others) - a purchasing application that orders material - a sales application that creates a supply list

Page 15: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Some Examples of DeadlockDeadlock on a file request

Inventory Suppliers

Purchasing

Sales

Purchasing accesses thesupplier file to place anorder for lumber.

Sales accesses theinventory file to reserve parts to builda home ordered today.

Purchasing now tries toaccess the Inventory fileto verify the quantity of lumber on hand before placing the order

Sales tries to access the supplier file to check the schedule of a subcontractor.

Page 16: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Some Examples of DeadlockDeadlock on a print spooler

A print spooler accepts input from many users, but will notstart a print job until it has a complete print file.

spool

print job

print jo

b

The day that a big project is due everyonein class submits their print files at the sametime. The spooler accepts pages from everyoneso several page ones show up, then several page twos, and so on. Quickly the spool filefills. It cannot print anything because is hasno complete jobs.

Page 17: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Four Conditions for Four Conditions for DeadlockDeadlock

Four Conditions for Four Conditions for DeadlockDeadlock

1. Mutual exclusion condition: Each resource is either being used by exactly one process or it is available.

2. Hold and wait condition: Processes currently holding resources granted earlier can request additional new resources.

3. No preemption condition: Resources previously granted cannot be taken away from the process. The process must release them voluntarily. Once a process requests a resource that is not available, it cannot withdraw its request.

4. Circular wait condition: There must be a circular chain of two or more processes, each of which is waiting for a resource held by the next member of the chain.

Page 18: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock ModelingDeadlock ModelingDeadlock ModelingDeadlock Modeling

process

resource

The resource hasbeen assigned tothe process.

resource

process

The process isblocked, waitingfor the resource.

This is called a Resource Graph

Page 19: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock ModelingDeadlock ModelingDeadlock ModelingDeadlock Modelingprocess

resource resource

process

r1 r2

p1

p2

process p1 ownsresource r1

process p1 is blockedwaiting for resource r2

process p2 is blockedwaiting for resource r1

process p2 ownsresource 2

Circular Wait Condition!

Page 20: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

This Process-Resource model provides a “snapshot” of the relationships between processes and resources at some instant in time.

We need a model that addresses the dynamics of the relationships … i.e. what happens over time.

Page 21: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

S is the set of states {si}

where each state s is represented with a resourcegraph representing the processes and resourcesin the machine.

The initial state, s0, represents the situationWhere none of the resources have been allocated.

In this model we are interested in state transitions.The pattern in which resources are requested,allocated, and released will determine whether or not the system is deadlocked.

Page 22: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A

Request resource RRequest resource SDo some workRelease resource RRelease resource S

Process B

Request resource SRequest resource TDo some workRelease resource SRelease resource T

Process C

Request resource TRequest resource RDo some workRelease resource TRelease resource R

Assume a Round Robin Scheduling AlgorithmWith events occurring in the following order:

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

Page 23: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

Page 24: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

Page 25: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

Page 26: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

Page 27: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

Page 28: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

D E A D L O C K !

Page 29: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

We know that the operating system is not bound torun processes in any particular order. In this case, ifthe operating system knew of the impending deadlock,it could have scheduled the work differently!

Page 30: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

Page 31: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

potential deadlock – block this call!

Page 32: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

potential deadlock – block this call!

Page 33: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

potential deadlock – block this call!

Page 34: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource R

A B C

R S T

potential deadlock – block this call!

Blocked.

blocks

Page 35: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A requests resource RProcess B requests resource SProcess C requests resource TProcess A requests resource SProcess B requests resource TProcess C requests resource RProcess A does its workProcess A releases resource RProcess A releases resource S

A B C

R S T

potential deadlock – block this call!

Blocked.

blocks

There is no deadlock ---Process C does its work and releases its resourcesProcess B is now unblocked

Page 36: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Modeling Deadlock withState Diagrams

Page 37: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

In a set of processes P, any process pi in P would causea transition from state sj, if

* pi requests a resource (shown as ri) * pi is allocated a resource (shown as ai) * pi deallocates a resource (shown as di)

s j

a i

r i

d i

Page 38: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

s j

a 3

r 4

d 1

Suppose that at state sj, some process p2 is blockedbecause all of the transitions out of sj are causedby other processes. That is, process p2 can notdo anything to force a change in state.

Page 39: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

If a process pi is blocked in state sj,

And there is no series of state transitions which will leadto a state in which pi is unblocked, then pi is deadlocked.

The state sj is called a deadlock state.

s j

a 3

r 4

d 3

Page 40: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A State Transition Model for One Process and Two Resources

ars 0 s 1 s 2 s 3 s 4

r a

d

d

r – requesta – allocated - deallocate

processowns 1resource

processowns 2resources

Page 41: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A State Transition Model for Two Processes and Two Resources

r0s 00 s 10 s 20

s 30 s 40

r0 – process 0 requesta0 – process 0 allocated0 – process 0 deallocate

s 01 s 11 s 21 s 31 s 41

a0 r0a0

d0d0

r0 r0a0a0

d0d0

r1r1 r1

r1 r1

s 02 s 12 s 22 s 32

r0 r0a0

d0

a1a1 a1

a1d1

d1 d1 d1

s 03 s 13 s 23 s 33

r0 r0a0

d0r1

r1 r1 r1

s 03 s 13 r0

a1a1d1 d1

Page 42: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A State Transition Model for Two Processes and Two Resources

r0s 00 s 10 s 20

s 30 s 40

r0 – process 0 requesta0 – process 0 allocated0 – process 0 deallocate

s 01 s 11 s 21 s 31 s 41

a0 r0a0

d0d0

r0 r0a0a0

d0d0

r1r1 r1

r1 r1

s 02 s 12 s 22 s 32

r0 r0a0

d0

a1a1 a1

a1d1

d1 d1 d1

s 03 s 13 s 23 s 33

r0 r0a0

d0r1

r1 r1 r1

s 04 s 14 r0

a1a1d1 d1

no resources held

one resource held

two resources held

deadlock state

Page 43: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock StrategiesDeadlock StrategiesDeadlock StrategiesDeadlock Strategies

The Ostrich Algorithm:

Ignore the problem. If you ignore it, it may ignore you.

Detection and Recovery:

Let deadlocks occur, detect them and take action.

Dynamic avoidance:

OS is careful how it allocates resources

Deadlock Prevention:

Structurally negate one of the conditions necessary tocause deadlock.

Page 44: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

The Ostrich AlgorithmThe Ostrich AlgorithmThe Ostrich AlgorithmThe Ostrich AlgorithmGood or Bad?

Many potential deadlock situations occur because almost allresources on a system are finite.

For example, The number of open files that a system can manageis limited by the size of the i-node table. Suppose that 100 processes all require 100 open files before they can do their work. After each has opened 99, the i-node table becomes full. Now, none of the processes can open another file and a deadlock occurs.

How often will this situation occur?

Does it occur often enough that it is worth fixing?Fixing such problems is expensive and usually involvesplacing inconvenient restrictions in what a process can do.

Page 45: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

ReminderReminderReminderReminder

process

resource

The resource hasbeen assigned tothe process.

resource

process

The process isblocked, waitingfor the resource.

Page 46: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock Detection Deadlock Detection Deadlock Detection Deadlock Detection Is there a deadlock in the drawing below?

A

F

C

R

S

W

D

B

E

G

T

U W

Cycles are pretty easy tofind with visual inspection.Can they be found algorithmically?

Page 47: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Resource graphs are an example of a directed graph.many algorithms exist to detect cycles in a directed graph.

Repeat the following steps for each node in the graph

1. Initialize a list to the empty list. Designate all arcs as unvisited

2. Add the current node to the list. Check to see if the node appears in the list more than once. If it does a cycle exists.

3. From the given node, see if there are any outgoing arcs. If so, go to step 4. Otherwise go to step 5.

4. Pick one of the outgoing nodes at random and mark it. Follow it to the new current node and go to step 2.

5. Dead end. Remove the node from the list and go back to the previous one and make it the current node. Go to step 3. If this node is the initial node, there are no cycles.

Page 48: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock Detection Deadlock Detection

Is there a deadlock in the drawing below?

A

F

C

R

S

W

D

B

E

G

T

U W

Start here andgo left to right,top to bottom

Page 49: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

R

Page 50: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

R

This is the onlyoutging arc, so A is the next node

A

x

Page 51: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

R

This is the onlyoutging arc, so S is the next node

A

S

x

x

Page 52: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

R

S has no outgoingArcs, so back-upTo A

A

x

x

Page 53: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

R

A has no unmarked outgoing arcs, so back-up To R

x

x

Page 54: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

R has no unmarked outgoing arcs, so we are done inspectingthis node.

x

x

Page 55: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

Move to the next nodeand repeat the algorithm.

It should be pretty obviousthat this iteration stops quickly, finding no cycle.

Page 56: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

Move to the next nodeand repeat the algorithm.

Page 57: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

BxThis is the only

outgoing arcT

Page 58: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

Bx This is the only

outgoing arcT

x

Page 59: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

Until finally at this point we have …

x

x

x

x

x

x

B

T

E

W

G

U

D

Page 60: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

If we go this way we hit adead end … there are nooutgoing arcs

x

x

x

x

x

x

B

T

E

W

G

U

D

Page 61: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

So we back up and look atthe only unmarked arcfrom D.

x

x

x

x

x

x

B

T

E

W

G

U

D

x

Page 62: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A

F

C

R

S

W

D

B

E

G

T

U W

List L

At this point, T gets added to the list.When the list is inspected, we note that it isin the list twice. Therefore a cycle exists.

x

x

x

x

x

x

B

T

E

W

G

U

D

T

x

Page 63: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock ModelingDeadlock ModelingDeadlock ModelingDeadlock Modeling

process

resource

The resource hasbeen assigned tothe process.

resource

process

The process isblocked, waitingfor the resource.

One way to model “units” of a resource is like this …

This resource has 2 unitsthat can be assigned.

Page 64: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Graph Reduction

Given a directed resource graph

1. Find a process that is currently using a resource and not waiting for one. This process can be removed from the graph. This is because the process would eventually finish and its resources returns to the pool of available resources.

2. Find a process that’s only waiting for resource classes that aren’t fully allocated. This process isn’t contributing to deadlock since it will eventually get its resources and finish. Then its resources can be returned to the available pool.

3. Go back to step 1 until all possible connections have been removed.

If there are any connections between processes and resources leftWhen you are done, then the system will deadlock.

Page 65: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Graph ReductionGraph ReductionExample One

Page 66: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

R1

R2 R3P1

P2P3

P3 is currently usingR3

and is not waiting for anyadditional resources.

P1 is using R1

and is waiting for R2

P2 is using R2

and is waiting for R3

Page 67: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

R1

R2 R3P1

P2P3

Eventually P3 will finish using R3,So we can unlink the resource. It can Now be allocated to P2

Page 68: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

R1

R2 R3P1

P2P3

P2 now has all of the resources itNeeds, so it can run to completion.Unlink it.

Page 69: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

R1

R2 R3P1

P2P3

R2 can now be given to P1

P1 has all of the resources it needs so eventually it willcomplete and we can unlink it.

Page 70: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

R1

R2 R3P1

P2P3

Since there are no connections left, there is no deadlock.

Page 71: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Graph ReductionGraph ReductionExample Two

Page 72: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

R1

R2 R3P1

P2P3

P3 is currently usingR3

and is not waiting for anyadditional resources.

P2 is using R2

and is waiting for R3

and R1

P1 is using R1

and is waiting for R2

Page 73: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

R1

R2 R3P1

P2P3

Unlink it so thatR3 can be given to P2

We can’t reduce the graph any further!

P2 can’t completeit’s waiting for R1

P1 can’t completeit’s waiting for R2

Page 74: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock Detection with Multiple Resources of the same Type

Deadlock Detection with Multiple Resources of the same Type

Consider an example where there are multipleprocesses running on a system that has• 4 tape drives• 2 plotters• 3 scanners• 1 CD Rom drive

Page 75: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Step 1: Define a vector, E, of resources that exist on the system

E = (4 2 3 1)

tape drives

plotters

scanners

CDRom Drives

Resources in existence

Graph Reduction – an approach that can be programmed

Page 76: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1)

Step Two: Define a vector A, that contains the number ofavailable resources of each type that are available at anyinstance in time.

A = (A1 A2 A3 A4)

Available resourcesResources in existence

Page 77: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1)

Step Three: Define a matrix C, the current allocation matrix. Each row in the matrix represents one process in the system.The columns represent the resources of each class. Each elementdefines the number of that type assigned to the process.

A = (A1 A2 A3 A4)

Resources in existence Available resources

Current allocation matrix

C11 C12 C13 C14

C21 C22 C23 C23

…Cn1 Cn2 Cn3 Cn4

This row defines the resourcesallocated to process 2

tape plot scan CDIf C23 were 2, it would indicate thatprocess 2 held 2 scanners.

process 1

process 2

process n

Page 78: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1)

Step Four: Define a matrix R, the request matrix. Each row in the matrix represents one process in the system. The columns represent the resources of each class. Each element then definesthe number of resources of that class requested by a process.

A = (A1 A2 A3 A4)

Resources in existence Available resources

Current allocation matrix

C11 C12 C13 C14

C21 C22 C23 C23

…Cn1 Cn2 Cn3 Cn4

Request matrix

R11 R12 R13 R14

R21 R22 R23 R23

…Rn1 Rn2 Rn3 Rn4

tape plot scan CD

This row represents the resourcesrequested by process 2.

If this element were 1, it wouldindicate that process 2 needs 1 plotter

process 1

process 2

process n

Page 79: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (A1 A2 A3 A4)

Resources in existence Available resources

Current allocation matrix

C11 C12 C13 C14

C21 C22 C23 C23

…Cn1 Cn2 Cn3 Cn4

tape plot scan CD

Request matrix

R11 R12 R13 R14

R21 R22 R23 R23

…Rn1 Rn2 Rn3 Rn4

tape plot scan CD

Note that The following is always true …

n

i=1

C ij + A j = E j

tape plot scan CD tape plot scan CD

Page 80: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (A1 A2 A3 A4)

Resources in existence Available resources

Current allocation matrix

C11 C12 C13 C14

C21 C22 C23 C23

…Cn1 Cn2 Cn3 Cn4

tape plot scan CD

Request matrix

R11 R12 R13 R14

R21 R22 R23 R23

…Rn1 Rn2 Rn3 Rn4

tape plot scan CD

The deadlock detection algorithm is based on comparing vectors.For vector A to be less than or equal to vector B, every element of A must be less than or equal to the corresponding element of B.

tape plot scan CD tape plot scan CD

Page 81: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (A1 A2 A3 A4)

Resources in existence Available resources

Current allocation matrix

C11 C12 C13 C14

C21 C22 C23 C23

…Cn1 Cn2 Cn3 Cn4

tape plot scan CD

Request matrix

R11 R12 R13 R14

R21 R22 R23 R23

…Rn1 Rn2 Rn3 Rn4

tape plot scan CD

Each process is initially unmarked.As the algorithm progresses, if a process can complete, it is marked.Any unmarked processes after executing the algorithm are deadlocked.

Page 82: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (A1 A2 A3 A4)

Resources in existence Available resources

Current allocation matrix

C11 C12 C13 C14

C21 C22 C23 C23

…Cn1 Cn2 Cn3 Cn4

tape plot scan CD

Request matrix

R11 R12 R13 R14

R21 R22 R23 R23

…Rn1 Rn2 Rn3 Rn4

tape plot scan CD

1. Look for an unmarked process, Pi, for which the i-th row of the

request matrix R is less than or equal to A. Such a process can be run to completion, because it’s resource demands can be met.

Is each element of R lessthan the correspondingelement of A?

Page 83: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (A1 A2 A3 A4)

Resources in existence Available resources

Current allocation matrix

C11 C12 C13 C14

C21 C22 C23 C23

…Cn1 Cn2 Cn3 Cn4

tape plot scan CD

Request matrix

R11 R12 R13 R14

R21 R22 R23 R23

…Rn1 Rn2 Rn3 Rn4

tape plot scan CD

2. If such a process is found, we assume that the process can be run to completion and it’s resources returned to the system. To account for this, add the i-th row of C to A and mark the process. Go back to step 1.

ADD

x

Page 84: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (A1 A2 A3 A4)

Resources in existence Available resources

Current allocation matrix

C11 C12 C13 C14

C21 C22 C23 C23

…Cn1 Cn2 Cn3 Cn4

tape plot scan CD

Request matrix

R11 R12 R13 R14

R21 R22 R23 R23

…Rn1 Rn2 Rn3 Rn4

tape plot scan CD

3. If no unmarked process can be found that is runnable, terminate the algorithm. If there are any unmarked processes left, these processes are deadlocked.

Page 85: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (2 1 0 0)

Resources in existence Available resources

Current allocation matrix

0 0 1 02 0 0 10 1 2 0tape plot scan CD

Request matrix

2 0 0 11 0 1 02 1 0 0

tape plot scan CD

To illustrate, let there be three processes in the system. * Process 1 has one scanner * Process 2 has 2 tape drives and a CD Rom drive * Process 3 has a plotter and 2 scanners

Each process needs additional resources as shown in R.

tape plot scan CD tape plot scan CD

process 1

process 2

process 3

Page 86: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (2 1 0 0)

Resources in existence Available resources

Current allocation matrix

0 0 1 02 0 0 10 1 2 0tape plot scan CD

Request matrix

2 0 0 11 0 1 02 1 0 0

tape plot scan CD

To run the algorithm, look for a process whose request list can be satisfied.

Page 87: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (2 1 0 0)

Resources in existence Available resources

Current allocation matrix

0 0 1 02 0 0 10 1 2 0tape plot scan CD

Request matrix

2 0 0 11 0 1 02 1 0 0

tape plot scan CD

So … process 3 can be run and eventually return it’s resources. Then vector A would be equal to

x0 1 2 0

A = (2 2 2 0)

Page 88: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (2 2 2 0)

Resources in existence Available resources

Current allocation matrix

0 0 1 02 0 0 10 1 2 0tape plot scan CD

Request matrix

2 0 0 11 0 1 02 1 0 0

tape plot scan CD

Now process 2’s requests can be satisfied. Its resources are added back into the Available Resources vector.

x

Page 89: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (4 2 2 1)

Resources in existence Available resources

Current allocation matrix

0 0 1 02 0 0 10 1 2 0tape plot scan CD

Request matrix

2 0 0 11 0 1 02 1 0 0

tape plot scan CD

Finally, Process 1 (the only one left) can get all of the resourcesThat it needs. It runs to completion. There is no deadlock.

xx

Page 90: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

How often should you run a deadlock detection algorithm?

Obviously, detecting deadlocks takes some system resource.

Page 91: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Recovery from DeadlockRecovery from DeadlockRecovery from DeadlockRecovery from Deadlock

There are no good alternatives to do deadlock recovery!

* Preemption: Take a resource away from a process.* Rollback: the state of a process must be checkpointed periodically. It is then possible to roll back to some state before it acquired the resource causing the deadlock. * Kill Processes: try to pick a process that can be run again from the beginning without any ill effects.

Page 92: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock PreventionDeadlock Prevention

Note that Deadlock prevention is different from detection.

Page 93: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

1. Mutual exclusion condition: Each resource is either being used by exactly one process or it is available.2. Hold and wait condition: Processes currently holding resources granted earlier can request additional new resources.3. No preemption condition: Resources previously granted cannot be taken away from the process. The process must release them voluntarily. Once a process requests a resource that is not available, it cannot withdraw its request.4. Circular wait condition: There must be a circular chain of two or more processes, each of which is waiting for a resource held by the next member of the chain.

For deadlock to occur, all of the following conditions must hold.

Deadlock prevention schemes all try to make sure that at least oneof these conditions is not true.

Page 94: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Attacking the Mutual Exclusion Condition

If no resource were ever assigned exclusively to a singleprocess, then there would be no deadlock problems.

Is this reasonable?

How might you avoid mutual exclusion on a printer?

It is difficult to avoid mutual exclusion. However, you canavoid assigning a resource unless and until it isabsolutely necessary. Try to make sure that as fewprocesses as possible can lay claim to the resource.

Page 95: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Attacking the Hold and Wait Condition

1. Require a process to request all of its resources when it is first created.

This would be impossible for an interactive system. However, a batch system could do this … only add a job to the ready list when all of the resources it needs are available. But, is there a problem?Poor utilization of resources !

2. Require a process to give up all resources it has allocated to it, before it can request any new ones.

This is okay for interactive systems, but it forces a process to compete for all resources, even when it needs an incremental resource.

Page 96: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Attacking the No Preemption Condition

This is not very promising … it is difficult to take a resourceaway from a process until the process has finished with it.

Page 97: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Attacking the Circular Wait Condition

There are two possible approaches:

Only allow a process to have one resource at a time.

Provide a global numbering system for resources. A processmay have as many resources as it wants, but requests must bein numerical order. For example, a process may request a printerand then a plotter, but never a plotter and then a printer!

Page 98: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock AvoidanceDeadlock Avoidance

Deadlock avoidance schemes control state transitions. They will only allow transitions due to allocations, when they can be certain that a deadlock cannot occur due to subsequent requests.

Page 99: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock AvoidanceDeadlock Avoidance

Process A

Pro

cess

BPlot how each process gets the cpu

Page 100: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A

Pro

cess

B

Process Arequests printer

Page 101: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A

Pro

cess

B

Process Arequests printer

Process Brequests plotter

Page 102: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A

Pro

cess

B

Process Arequests plotter

Process Brequests plotter

Process Arequests printer

Process Brequests printer

Page 103: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A

Pro

cess

B

Process Arequests plotter

Process Brequests plotter

Process Areleases printer

Process Areleases plotter

Process Arequests printer

Process Brequests printer

Process Breleases plotter

Process Breleases printer

Page 104: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A

Pro

cess

B

plt

plt

prt pltprt

prt

plt

prt

The system cannot enter any of these states, becauseboth processes would have the printer

Page 105: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A

Pro

cess

B

plt

plt

prt pltprt

prt

plt

prt

The system cannot enter any of these states, becauseboth processes would have the plotter

Page 106: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A

Pro

cess

B

plt

plt

prt pltprt

prt

plt

prt

Once you enter this section(no matter the path) you willbe in a deadlock situation!

Page 107: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process A

Pro

cess

B

plt

plt

prt pltprt

prt

plt

prt

Unsafe area!

Deadlock can be avoided by scheduling the processes as shown:

Page 108: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Deadlock avoidance requires that every process declareits maximum resource claim before it can begin running

Page 109: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (4 2 3 1) A = (2 1 0 0)

Resources in existence Available resources

Current allocation matrix

0 0 1 02 0 0 10 1 2 0tape plot scan CD

Request matrix

2 0 0 11 0 1 02 1 0 0

tape plot scan CD

Consider the vectors and matrices we used earlier to look atDeadlock. We can use these to define the Resource AllocationState of a process.

Page 110: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Safe StateSafe State

A state is safe if the system is not deadlocked, and can allocate resources to each process (up to its maximum) in some given order and still avoid deadlock.

Page 111: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

The Banker’s AlgorithmThe Banker’s AlgorithmThe Banker’s algorithm was developed by Dijkstra in 1965and provides a scheduling algorithm that can avoid deadlocks.It is modeled on the way a small town banker might deal witha group of customers to whom he has granted lines of credit.

If granting a request leads to a safe state, the request isgranted. If it does not, the request is denied. When arequest is denied the customer may come back laterto make the request again.

Page 112: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Consider the following case. The bank has four customers,each of whom has been granted a certain line of credit. Any customer may ask for a loan up to their maximum line of credit.The bank assumes that all of the customers will not ask for theirmaximum line of credit at the same time, so it does not need tohave the sum of these credit lines on hand. If a customer cannoteventually get the money they need, they go out of business andtheir loans are never repaid.

A 0 6

B 0 5

C 0 4

D 0 7

Customer hasmax

(line of credit)

Bank has 10

Page 113: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Assume that the bank customers go about their business,and periodically get loans from the bank of some amountup to their maximum line of credit. At some point wemight have a situation like that pictured below:

A 1 6

B 1 5

C 2 4

D 4 7

Customer has max

Bank has 2

Page 114: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Is this a safe state or an unsafe state?

A 1 6

B 1 5

C 2 4

D 4 7

Customer has max

Bank has 2

Page 115: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A 1 6

B 1 5

C 2 4

D 4 7

Customer has max

Bank has 2

The bank can loan C up to it’s full line of credit. Any other requests aredenied. At some time C Pays off its loans and the bank has 4. It couldthen satisfy requests by B or D up to their maximum amounts.

Page 116: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

However, what happens if B is granted one additionalunit of credit?

A 1 6

B 2 5

C 2 4

D 4 7

Customer has max

Bank has 1

Unsafe

Why?

Page 117: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

However, what happens if B is granted one additionalunit of credit?

A 1 6

B 2 5

C 2 4

D 4 7

Customer has max

Bank has 1

Unsafe

If any customer walked in and demanded their full line of credit,the bank could not grant it.

Page 118: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Note that being in an unsafe state does not necessarily mean that Deadlock will occur. None of the customers may ever ask for their maximum Line of credit.

Page 119: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (6 3 4 2) A = (1 0 2 0)

Resources in existence Available resources

Current allocation matrix

3 0 1 10 1 0 01 1 1 01 1 0 10 0 0 0

tape plot scan CD

Max request matrix

1 1 0 00 1 1 23 1 0 00 0 1 02 1 1 0

tape plot scan CD

Banker’s Algorithm for Multiple Resources … Begin at some state s j

P = (5 3 2 2)

Possessed Resources

Page 120: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (6 3 4 2) A = (1 0 2 0)

Resources in existence Available resources

Current allocation matrix

3 0 1 10 1 0 01 1 1 01 1 0 10 0 0 0

tape plot scan CD

Max request matrix

1 1 0 00 1 1 23 1 0 00 0 1 02 1 1 0

tape plot scan CD

1. Find a row in the maximum request matirx whose unmet resources are less than A.

If you can’t find one, the system is deadlocked!

P = (5 3 2 2)

Possessed Resources

Here’s one.

2. Now assume that the process is given the resources and finishes. Mark the process as terminated and add its resources back into A

Page 121: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (6 3 4 2) A = (1 0 2 0)

Resources in existence Available resources

Current allocation matrix

3 0 1 10 1 0 01 1 1 01 1 0 10 0 0 0

tape plot scan CD

Max request matrix

1 1 0 00 1 1 23 1 0 00 0 1 02 1 1 0

tape plot scan CD

1. Find a row in the maximum request matirx whose unmet resources are less than A.

If you can’t find one, the system is deadlocked!

P = (5 3 2 2)

Possessed Resources

Here’s one.

2. Now assume that the process is given the resources and finishes. Mark the process as terminated and add its resources back into A

1 1 0 1

Page 122: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

E = (6 3 4 2) A = (2 1 2 1)

Resources in existence Available resources

Current allocation matrix

3 0 1 10 1 0 01 1 1 00 0 0 00 0 0 0

tape plot scan CD

Max request matrix

1 1 0 00 1 1 23 1 0 00 0 1 02 1 1 0

tape plot scan CD

Repeat steps 1 and 2 until all processes are marked terminated(the state is safe), or until a deadlock occurs (it is not safe).

P = (5 3 2 2)

Possessed Resources

Page 123: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Dijkstra’s algorithm is elegant in concept, but is almost useless in practice.Why?

1. It requires a process to know in advance what its maximumresource needs will be.

2. It assumes that the number of available resources is constantso does not account for failures.

3. It assumes that the number of processes is constant so does not account for users logging in and out of a system.

Page 124: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Sample Exam Questions

Page 125: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Suppose a system contains four processes (1, 2, 3, 4)

and four single unit resources (A, B, C, D).

If the following sequence of events occurs, which request(if any) is the first that will cause a deadlock to occur?

Process 1 2 1 4 3 2 4 3

Resource A B B C D C A A

Page 126: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Process 1 2 1 4 3 2 4 3

Resource A B B C D C A A

1

A

2

B

4

C

3

D

Deadlock

Page 127: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Assume that a system has the following resource units <6, 4, 4, 2>

The current allocation matrix The maximum claim matrix

2 0 1 0 3 2 1 11 1 0 0 1 2 0 21 1 0 0 1 1 2 01 0 1 0 3 2 1 00 1 0 1 2 1 0 0

Is this state safe?

Page 128: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

The current allocation matrix The maximum claim matrix

2 0 1 0 3 2 1 11 1 0 0 1 2 0 21 1 0 0 1 1 2 01 0 1 0 3 2 1 00 1 0 1 2 1 0 0

Exist <6, 4, 4, 2>

The available resources must beA <1, 1, 2, 1>

This request could be satisfied

Page 129: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

A system is composed of four processes {p1, p2, p3, p4}

and three types of serially re-usable resources {R1, R2, R3}

The number of units of the resources is <3, 2, 2>.

Show the resource graph that represents the system state,and reduce the graph as far as possible under the followingconditions:

Process p1 holds one unit of R1 and requests one unit of R2

p2 holds two units of R2 and requests one unit of R1 and R3

p3 holds one unit of R1 and requests one unit of R2

p4 holds two units of R3 and requests one unit of R1

Page 130: DeadlocksDeadlocks CNS 3060. Imagine a narrow set of stairs, only wide enough for one person to pass. However, the landings are big enough for several

Students working at individual PCs in a computer lab all sendtheir files to a server that spools the files on its hard disk. Under what conditions may a deadlock occur when the diskspace for the print spool is limited?

How can this deadlock be avoided?