+ All Categories
Home > Documents > Deadlocks

Deadlocks

Date post: 22-Feb-2016
Category:
Upload: melina
View: 18 times
Download: 0 times
Share this document with a friend
Description:
Deadlocks. CS 3060. 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. Wait. Deadlock. Preemptable and Non-preemptable Resources. - PowerPoint PPT Presentation
Popular Tags:
66
Deadlocks CS 3060
Transcript
Page 1: Deadlocks

Deadlocks

CS 3060

Page 2: Deadlocks

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: Deadlocks

Wait

Page 4: Deadlocks

Deadlock

Page 5: Deadlocks

Preemptable and Non-preemptable Resources

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: Deadlocks

Using Semaphores to Acquire and Use a Resource

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

Page 7: Deadlocks

Using Semaphores to Acquire and Use Two Resources

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

Page 8: Deadlocks

Two Processes Using Semaphores to 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: Deadlocks

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 to Acquire and Use Two Resources

Page 10: Deadlocks

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 to Acquire and Use Two Resources

Page 11: Deadlocks

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 to Acquire and Use Two Resources

Page 12: Deadlocks

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 to Acquire and Use Two Resources

Page 13: Deadlocks

Some Examples of Deadlock

Page 14: Deadlocks

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: Deadlocks

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: Deadlocks

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 jobThe 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: Deadlocks

Four Conditions for Deadlock

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: Deadlocks

Deadlock 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: Deadlocks

Deadlock 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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

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: Deadlocks

Deadlock 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 37: Deadlocks

The 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 38: Deadlocks

Reminder

process

resource

The resource hasbeen assigned tothe process.

resource

process

The process isblocked, waitingfor the resource.

Page 39: Deadlocks

Deadlock Detection Is there a deadlock in the drawing below?

A

F

C

R

S

W

D

B

E

G

T

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

Page 40: Deadlocks

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 41: Deadlocks

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 42: Deadlocks

A

F

C

R

S

W

D

B

E

G

T

U W

List L

R

Page 43: Deadlocks

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 44: Deadlocks

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 45: Deadlocks

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 46: Deadlocks

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 47: Deadlocks

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 48: Deadlocks

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 49: Deadlocks

A

F

C

R

S

W

D

B

E

G

T

U W

List L

Move to the next nodeand repeat the algorithm.

Page 50: Deadlocks

A

F

C

R

S

W

D

B

E

G

T

U W

List L

BxThis is the only

outgoing arcT

Page 51: Deadlocks

A

F

C

R

S

W

D

B

E

G

T

U W

List L

Bx This is the only

outgoing arcTx

Page 52: Deadlocks

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 53: Deadlocks

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 54: Deadlocks

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 55: Deadlocks

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 56: Deadlocks

Deadlock 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 57: Deadlocks

Graph ReductionGiven 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 58: Deadlocks

Deadlock Prevention

Note that Deadlock prevention is different from detection.

Page 59: Deadlocks

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 60: Deadlocks

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 61: Deadlocks

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 62: Deadlocks

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 63: Deadlocks

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 64: Deadlocks

Deadlock 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 65: Deadlocks

The 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 66: Deadlocks

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.


Recommended