Docsity

Deadlock Prevention and Avoidance: Necessary Conditions and Graph Representations - Prof. , Study notes of Computer Science

Toggle sidebar
bg1
1
page 1
CS 361 Concurrent programming
Drexel University
Fall 2004
Lecture 16
©Bruce Char and Vera Zaychik. All rights reserved by the author.
Permission is given to students enrolled in CS361 Fall 2004 to
reproduce these notes for their own use.
page 2
Deadlocks, revisited (Silbershantz,
ch. 8): Deadlock characterization
• Necessary conditions for deadlock
– Mutual exclusion:A resource must be protected by a mutual
exclusion lock (only one thread at a time can access it)
– Hold and wait: A thread must acquire one resource and be waiting
for another
– No preemption: a thread cannot pre-empt another for a resource.
Pre-emption means that a thread holding a resource give up its
access to another even though it has not yet done what it wanted
with the resource
– Circular wait: There must be a set of waiting threa ds T0, T1, T2,
… Tn, such that T0 is waiting for T1, T1 for T2, … and Tn is
waiting for T0.
page 3
Necessary conditions for deadlock
• All the conditions must hold for deadlock to
occur. The Hartley rules about locking
down semaphores avoid the 4th condition
hence guarantee deadlock avoidance.
• The conditions are not independent. For
example, if there is a ā€œcircular waitā€ there
must already be ā€œhold and waitā€.
page 4
Resource allocation graphs
• The resource-allocation graph is a way of
representing the relationships between resources
and the threads that access them.
• Graph terminology can be used to describe
deadlock situations.
• The graph representation allows people (or
computer programs doing deadlock analysis) to
use graph operations to determine if a situation
allows deadlock.
page 5
Directed graphs
• Resource allocation graphs are directed graphs. A
directed graph is a set of vertices V (sometimes
called nodes) and a set of edges E.
• Typically in a directed graph the vertices are
drawn as labeled circles or squares, and the edges
as lines connecting pairs of vertices, with an
arrowhead indicating the direction of the
connection.
page 6
Resource allocation graphs
• In a resource allocation graph the vertices are divided into two subsets R
(for resource types) and T (for threads).
• Edges exist between nodes of T and nodes of R.
• An edge (Ti -> Rj) is a request edge. It is used to indicate that thread i is
requesting access to resource type j and is bl ocked waiting for one of that
type to become available.
• An edge (Rj -> Ti) is an assignment edge. It means that Thread i has
gotten access to Resource of type j and is using it.
• While there is only one thread with id i, there may be several resources of
type j. For example, if Resource 1 are printers, there may be several
printers in the system. It may not matter which printer a thread gets
access to as long as it gets access to a printer eventu ally. This would be
modeled by a resource allocation graph with several T vertices, but only
one R vertex.
pf3
pf4
pf5

Partial preview of the text

Download Deadlock Prevention and Avoidance: Necessary Conditions and Graph Representations - Prof. and more Study notes Computer Science in PDF only on Docsity!

page 1

CS 361 Concurrent programming

Drexel University

Fall 2004

Lecture 16

©Bruce Char and Vera Zaychik. All rights reserved by the author. Permission is given to students enrolled in CS361 Fall 2004 to reproduce these notes for their own use.

page 2

Deadlocks, revisited (Silbershantz,

ch. 8): Deadlock characterization

  • Necessary conditions for deadlock
    • Mutual exclusion:A resource must be protected by a mutual exclusion lock (only one thread at a time can access it)
    • Hold and wait: A thread must acquire one resource and be waiting for another
    • No preemption: a thread cannot pre-empt another for a resource. Pre-emption means that a thread holding a resource give up its access to another even though it has not yet done what it wanted with the resource
    • Circular wait: There must be a set of waiting threads T0, T1, T2, … Tn, such that T0 is waiting for T1, T1 for T2, … and Tn is waiting for T0.

page 3

Necessary conditions for deadlock

• All the conditions must hold for deadlock to

occur. The Hartley rules about locking

down semaphores avoid the 4th^ condition

hence guarantee deadlock avoidance.

• The conditions are not independent. For

example, if there is a ā€œcircular waitā€ there

must already be ā€œhold and waitā€.

page 4

Resource allocation graphs

• The resource-allocation graph is a way of

representing the relationships between resources

and the threads that access them.

• Graph terminology can be used to describe

deadlock situations.

• The graph representation allows people (or

computer programs doing deadlock analysis) to

use graph operations to determine if a situation

allows deadlock.

Directed graphs

• Resource allocation graphs are directed graphs. A

directed graph is a set of vertices V (sometimes

called nodes) and a set of edges E.

• Typically in a directed graph the vertices are

drawn as labeled circles or squares, and the edges

as lines connecting pairs of vertices, with an

arrowhead indicating the direction of the

connection.

Resource allocation graphs

  • In a resource allocation graph the vertices are divided into two subsets R (for resource types) and T (for threads).
  • Edges exist between nodes of T and nodes of R.
  • An edge (Ti -> Rj) is a request edge. It is used to indicate that thread i is requesting access to resource type j and is blocked waiting for one of that type to become available.
  • An edge (Rj -> Ti) is an assignment edge. It means that Thread i has gotten access to Resource of type j and is using it.
  • While there is only one thread with id i, there may be several resources of type j. For example, if Resource 1 are printers, there may be several printers in the system. It may not matter which printer a thread gets access to as long as it gets access to a printer eventually. This would be modeled by a resource allocation graph with several T vertices, but only one R vertex.

page 7

Example Figure 8.1 (Silbershantz, p

R

x

R

x

R

x x

R

x x x

T1 T2 T

General notes:

Four Resource types Three threads T1 requesting Resource of type 1, has access to a resource of type 3, etc. There are two instances of resource type 3, and three of resource type 4.

What else? page 8

Deadlock Theorem

• If the resource allocation graph has no cycles (a

circular path following the arrows), then there is

no possibility of deadlock.

• If there is a cycle, and there is only one instance of

each resource type, then there is deadlock.

• If there is a cycle, and there is more than one

instance of some resource types, then there may be

deadlock (or maybe not).

page 9

Example graph with unrecoverable

deadlock: Silbershantz Fig 8.3, p. 232

R

x

R

x

R

x x

R

x x x

T1 T2 T

T1, T2, and T3 are deadlocked. Graph has a cycle: T1-> R1 -> T2 -> R2 -> T3 -> R3 -> T T1 wants R1 but is waiting for T to give it up. T2 wanted R2 but is waiting for T3 to give it up. T3 wants R3 but the available supply is being held by T1 and T2 (who have grabbed those resource instances but are waiting to get the another type of resource as well). page 10

Example graph with recoverable

deadlock Silbershantz Fig 8.4, p. 232

R

x x

T

T

T

R

x x T

Graph has a cycle: T1 -> R1 -> T -> R2 -> T T1 and T3 are both blocked, but we can avoid permanent deadlock by releasing the resource currently held by T4 and giving it to T3, and by releasing the resource held by T2 and giving it to T1.

How to handle deadlocks

• Three techniques exist:

  • A protocol that guarantees that the system never enters a deadlock state.
  • Allowing deadlock states to be entered, but then have a way of recovering from it.
  • Ignore the problem and pretend that deadlocks never occur. Programs that ā€œfreeze upā€ because of deadlock would require you to kill and restart the application if a deadlock occurred. This may be economically acceptable even if it is not correct.

Deadlock prevention

• Deadlock prevention means writing a program so

that none of the necessary conditions for deadlock

ever occur during program execution.

• Deadlock prevention methods typically constrain

how requests for resources can be made.

• The rules for semaphore usage mentioned in the

Hartley textbook are a kind of deadlock

prevention.

page 19

Deadlock avoidance

• Deadlock avoidance algorithms prevent

deadlocks by restraining how requests can

be made, preventing at least one of the four

necessary conditions for deadlock from

occurring.

– Side effects from deadlock avoidance methods

can be: low device utilization, reduced system

throughput, and potential starvation.

page 20

One type of deadlock avoidance

algorithm: count resources

requested

• Each thread declares the maximum number

of resources of each type that it may need.

Each thread asks for clearance from a

central authority before it starts requesting

any resource. Threads must wait (without

holding any resource) until they are told to

proceed by the authority. Since there is no

hold-and-wait, there can be no deadlock.

page 21

An algorithm for deadlock

avoidance

• Works in an environment where there is

only one instance of each resource type: n

Threads with m different Resources. Only

one instance of each resource.

• Before execution of any thread, we work

out which resources each thread may ask

for.

page 22

Resource allocation graphs with

claim edges

  • As before, we have graphs with threads (nodes indicated by circles) and resources (nodes indicated by squares). An edge goes from a resource to a thread if the resource is allocated (held) by the thread. An edge goes from a thread to a resource if the resource is requested but not yet held by the thread.
  • A new kind of edge (given by a dotted line) goes from a thread node to a resource if the thread could ask for the resource.

R

R

T1 T

R1 is held by T1, requested by T2. R2 could be requested by T1 and T2, but neither has done so yet.

page 23

Algorithm for deadlock avoidance

  • If thread Ti requests resource Rj, then if putting in an allocation edge would create a cycle in the graph, don’t grant the request. It would create an unsafe state where deadlock might occur.

R

R

T1 T

We have the graph from the previous slide. T2 asks for R2. We see that changing the R2-T2 edge results in a graph with a cycle, so we deny the request.

page 24

Using the algorithm in a system

  • When the system is initialized, all the edges of the graph are claim edges, since nothing has been granted.
  • With each resource request, we turn one of the claim edges into an assignment edge (ā€œresource grantedā€ edge) and see whether this results in a graph with a cycle. If it doesn’t, we allow the request and keep the graph. If it does, we deny the request and undo the change of the edge.
  • When a thread is finished with a resource, we turn the assignment edge back into the claim edge.
  • What happens if a thread’s request is denied? It could busy-wait, occasionally trying the request again. Or it could block and wake up whenever a relevant resource is returned. This implies some kind of controller thread that keeps track of who wants what and does the wakeup (notify) signal to the blocked thread.

page 25

Other algorithms for deadlock

avoidance

• There are algorithms to handle more

complicated situations when the system has

several instances of a resource type instead

of just one.

• See Coffman et al., ā€œSystem Deadlocksā€,

Computing Surveys, Vol. 3, no. 2 (June

1971). Pp 67-78.

page 26

Deadlock detection and deadlock

recovery

  • Typically multi-threaded application programs do not try to handle deadlocks by detecting and recovering from them. They try to avoid deadlocks. This is because deadlock in an application typically noticeably degrades performance and/or functionality in an unacceptable fashion.
  • Operating systems however, may handle several different processes from several different users. It may be necessary for OSs to recover from processes that are hung (due to deadlocks or other problems). Deadlock detection and recovery is more important there. We leave the discussion of this (sections 8.6 and 8.7) to a course on operating systems.

page 27

Deadlock Detection

• If there is a process (or thread) that can monitor

resource utilization, then deadlock can be detected

if that process/thread looks at cycle detection of

the resource allocation graph.

• The detector process/thread must maintain the

resource allocation graph, and periodically invoke

a cycle detection algorithm on it.

• How frequently should the detection algorithm be

invoked? It depends on the frequency of deadlock

occurrence and the costs of deadlock (related to

how many processes can get stuck).

page 28

Deadlock recovery

• The deadlock detection process/thread

could recover from deadlock through two

techniques:

– Process/thread termination

– Resource preemption

Dangers of process/thread

termination

• Not good if killing the process/thread leaves

shared objects or variables in a state that violate

their design. For example, if a process/thread is

killed in the middle of updating a queue shared by

several processes or threads, then the queue could

be left in a corrupted state where links are missing

or invalid.

• Recall that Java threads can’t be killed safely

through use of stop().

Process/thread termination

• Several policies:

  • Kill all involved in a deadlock.
    • Potentially expensive – throwing away a lot of computation.
  • Partial termination
    • Kill one at a time until the deadlock cycle is eliminated.
      • More overhead, in that deadlock detection algorithm must be reinvoked each time something is killed. The standard algorithm is O(n^2), where n is the number of processes/threads, so this can get expensive if n is large.
    • Determine a set to kill to end the deadlock, and kill only those. Typically the set is chosen so as to minimize cost. ā€œCostā€ is a policy issue, and is discussed better in the context of an operating system course.