CH 3 Deadlock - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

CH 3 Deadlock

Description:

... rollback Rdb s & commit/rollback Recovery: kill process Deadlock avoidance Deadlock avoidance Safe states Unsafe states Banker s algorithm ... – PowerPoint PPT presentation

Number of Views:182
Avg rating:3.0/5.0
Slides: 51
Provided by: George648
Category:

less

Transcript and Presenter's Notes

Title: CH 3 Deadlock


1
CH 3 Deadlock
  • When 2 (or more) processes remain blocked forever!

2
How can this happen?
  • Process a
  • Down x
  • Gets x
  • Down y
  • Blocks
  • Process b
  • Down y
  • Gets y
  • Down x
  • Blocks

Both are blocked forever!
3
Resources
  • Things for which we request exclusive access.
  • Ex. db, files, shared memory, printer, cd/dvd
    writer, tape drive, etc.
  • Types
  • Preemptable can be taken away w/out ill effects
  • Non preemptable cannot be take away w/out
    causing a failure!

4
Resource examples
  • Memory preemptable
  • CPU preemptable
  • CD writing non preemptable
  • Printing non preemptable
  • We will only consider non preemptable (the harder
    problem).

5
Using a semaphore to protect one resource.
6
Using two semaphores to protect two resource.
7
(No Transcript)
8
Deadlock
  • A set of processes is deadlocked if each process
    in the set is waiting for an event that only
    another process in the set can cause.

9
Conditions for deadlock
  • Mutual exclusion
  • Hold and wait
  • No preemption
  • Circular wait
  • (Need all of these (all necessary).)

10
Resource allocation graph
resource
process
11
(Directed)Graphs
  • A graph G (V,E) where
  • V vertex set and
  • E edge set
  • Ordered pairs of vertices
  • Ex. Let G (V,E) where
  • V C, D, T, U and
  • E ltD,Ugt, ltU,Cgt, ltC,Tgt, ltT,Dgt

12
Representing digraphs
  • The simplest method is to use a 2D array where
    gij 1 indicates that an edge exists from i
    to j.
  • Initially, set all elements of g to 0.
  • Let processes be represented by a single,
    lowercase letter a..z.
  • Let resources be represented by integers in the
    range 1..50.

13
Representing digraphs
a
10
b
2
  • The simplest method is to use a 2D array where
    gij 1 indicates that an edge exists from i
    to j.
  • Input to your program consists of lines read in
    from an ASCII text file. Edges in the graph are
    represented by each line in the file. For
    example, consider the following
  • 10 a
  • b 2
  • The line 10 a is an edge from resource 10 to
    process a in the graph indicating that process a
    holds resource 10.
  • The line b 2 is an edge from process b to
    resource 2 in the graph indicating that process b
    wants (is requesting) resource 2.
  • Note that this graph does not contain any cycles.
  • So how do we use g to represent this graph?

14
Representing digraphs
a
10
b
2
  • The simplest method is to use a 2D array where
    gij 1 indicates that an edge exists from i
    to j.
  • Input to your program consists of lines read in
    from an ASCII text file. Edges in the graph are
    represented by each line in the file. For
    example, consider the following
  • 10 a
  • b 2
  • The line 10 a is an edge from resource 10 to
    process a in the graph indicating that process a
    holds resource 10.
  • The line b 2 is an edge from process b to
    resource 2 in the graph indicating that process b
    wants (is requesting) resource 2.
  • Note that this graph does not contain any cycles.
  • So how do we use g to represent this graph?
  • g10a 1
  • gb2 1

15
Another example
c
1
  • Here is another example
  • d 1
  • 1 c
  • c 2
  • 2 d
  • which could also be represented by
  • c 2
  • d 1
  • 1 c
  • 2 d
  • Note that the order of lines in the input file is
    arbitrary. These graphs contain a cycle.

d
2
16
Dealing w/ deadlock
  1. Ignore it (ostrich algorithm).
  2. Detect and recover.
  3. Avoid by careful resource allocation.
  4. Disallow one or more of the conditions necessary
    for deadlock.

17
1. Ostrich algorithm
  • Ignore it pretend it doesnt happen!

18
2. Detect and recover
  1. Detection with one resource of each type
  2. Detection with multiple resources of each type

19
Detection with one resource of each type
  • Detect cycle in digraph
  • For each vertex v in graph,
  • search the subtree rooted at v
  • see if we visit any vertex twice (by keeping a
    record of already visited vertices).

20
T
21
Detection with multiple resources of each type
  • Skip.

22
Recovery
  1. Preemption
  2. Rollback (using checkpoints)
  3. Kill process

23
Recovery preemption
  • Temporarily take back needed resource
  • Ex. Printer
  • Pause at end of page k
  • Start printing other job
  • Resume printing original job starting at page k1

24
Recovery rollback
  • Checkpoint save entire process state (typically
    right before the resource was allocated)
  • When deadlock is detected, we kill the
    checkpointed process, freeing the resource, and
    then later restart the killed process starting at
    the checkpoint.
  • Requires apps that can be restarted in this
    manner.

25
Rdbs commit/rollback
  • From p. 62, http//www.postgresql.org/files/docume
    ntation/pdf/8.0/postgresql-8.0-US.pdf

26
Recovery kill process
  • Requires apps that can be restarted from the
    beginning.

27
Deadlock avoidance
28
Deadlock avoidance
  • Safe state not deadlocked and there exists some
    scheduling order in which every process can run
    to completion even if all of them suddenly
    request their max number of resources immediately
  • Unsafe ! deadlocked

29
Safe states
30
Unsafe states
With only 4 free, neither A nor C can be
completely satisfied.
31
Bankers algorithm (SR for single resource type)
32
Bankers algorithm (SR)
  • Grant only those requests that lead to safe
    states.
  • Requires future information.

33
Bankers algorithm (SR)
  • (b) is safe because from (b) we can give C 2 more
    (free0) then C completes (free4) then give
    either B or D . . .

34
Bankers algorithm (SR)
  • (c) is unsafe because no max can be satisfied

35
Bankers algorithm (MR for multiple resource
types)
36
Bankers algorithm (MR)
N (need)
Eexisting (total) constant Ppossessed
(allocated/held) Aavailable (free)
?
37
Bankers algorithm (MR)
  • To check for a safe state
  • Look for a row, R, in N lt A.
  • Assume the process of the chosen row requests all
    resources, runs to completion, and terminates
    (giving back all of its resources to A).
  • Repeat steps 1 and 2 until either all processes
    either terminate (indicating that the initial
    state was safe) or deadlock occurs.

38
Bankers algorithm (MR)
  • To check for a safe state
  • Look for a row, R, in N lt A.

A (available) can completely satisfy Ds needs.
39
Bankers algorithm (MR)
Previous E(6342) P(5322) A(1020)
  • To check for a safe state
  • Look for a row, R, in N lt A.
  • Assume the process of the chosen row requests all
    resources, runs to completion, and terminates
    (giving back all of its resources to A).

After D terminates E(6342) P(4221) A(2121)
40
Bankers algorithm (MR)
Previous E(6342) P(5322) A(1020)
  • To check for a safe state
  • Look for a row, R, in N lt A.

E(6342) P(4221) A(2121)
41
Bankers algorithm (MR)
Previous E(6342) P(4221) A(2121)
  • To check for a safe state
  • Look for a row, R, in N lt A.
  • Assume the process of the chosen row requests all
    resources, runs to completion, and terminates
    (giving back all of its resources

After A terminates E(6342) P(1210) A(5132)
42
Bankers algorithm (MR)
Previous E(6342) P(4221) A(2121)
  • To check for a safe state
  • Look for a row, R, in N lt A.

E(6342) P(1210) A(5132)
43
Bankers algorithm (MR)
Previous E(6342) P(1210) A(5132)
  • To check for a safe state
  • Look for a row, R, in N lt A.

E(6342) P(1110) A(5232)
44
Bankers algorithm (MR)
Previous E(6342) P(1210) A(5132)
  • To check for a safe state
  • Look for a row, R, in N lt A.

E(6342) P(1110) A(5232)
45
Deadlock prevention
46
Deadlock prevention
  • Attack (disallow)
  • Mutual exclusion
  • Hold and wait
  • No preemption
  • Circular wait

47
Deadlock prevention attack mutex
  • Ex. Use spooling instead of mutex on printer
  • Not all problems lend themselves to spooling
  • Still have contention for disk space with spooling

48
Deadlock prevention attack hold wait
  • Request (wait for) all resources prior to process
    execution
  • Problems
  • We may not know a priori.
  • We tie up resources for entire time.
  • Before requesting a resource, we must first
    temporarily release all allocated resources and
    then try to acquire all of them again.

49
Deadlock prevention attack no preemption
  • Tricky at best.
  • Impossible at worst.

50
Deadlock prevention attack circular wait
  • Only allow one resource at a time.
  • Request all resources in (some globally assigned)
    numerical order.
  • But no numerical ordering may exist!
Write a Comment
User Comments (0)
About PowerShow.com