Docsity

Deadlocks Two - Operating Systems - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Deadlocks Two - Operating Systems - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

7: Deadlocks 1

OPERATING SYSTEMS

DEADLOCKS

7: Deadlocks 2

What Is In This Chapter?

  • What is a deadlock?
  • Staying Safe: Preventing and Avoiding Deadlocks
  • Living Dangerously: Let the deadlock happen, then

detect it and recover from it.

OPERATING SYSTEM

Deadlocks

7: Deadlocks 4

  • Traffic only in one direction.
  • Each section of a bridge can be viewed as a resource.
  • If a deadlock occurs, it can be resolved if one car backs up (preempt

resources and rollback).

  • Several cars may have to be backed up if a deadlock occurs.
  • Starvation is possible.

DEADLOCKS Bridge Crossing Example

7: Deadlocks 5

DEADLOCKS

NECESSARY CONDITIONS

ALL of these four must happen simultaneously for a deadlock to occur:

DEADLOCK

CHARACTERISATION

Mutual exclusion

One or more than one resource must be held by a process in a non-sharable (exclusive) mode.

Hold and Wait

A process holds a resource while waiting for another resource.

No Preemption

There is only voluntary release of a resource - nobody else can make a process give up a resource.

Circular Wait

Process A waits for Process B waits for Process C .... waits for Process A.

7: Deadlocks 7

  • If the graph contains no cycles, then no process is deadlocked.
  • If there is a cycle, then:

a) If resource types have multiple instances, then deadlock MAY exist. b) If each resource type has 1 instance, then deadlock has occurred.

DEADLOCKS RESOURCE ALLOCATION GRAPH

Resource allocation graph

P2 Requests P

R3 Assigned to P

7: Deadlocks 8

DEADLOCKS RESOURCE ALLOCATION GRAPH

Resource allocation graph

with a deadlock.

Resource allocation graph with a cycle but no deadlock.

7: Deadlocks 10

Do not allow one of the four conditions to occur.

Mutual exclusion:

a) Automatically holds for printers and other non-sharables. b) Shared entities (read only files) don't need mutual exclusion (and aren’t susceptible to deadlock.) c) Prevention not possible, since some devices are intrinsically non-sharable.

Hold and wait:

a) Collect all resources before execution. b) A particular resource can only be requested when no others are being held. A sequence of resources is always collected beginning with the same one. c) Utilization is low, starvation possible.

DEADLOCKS Deadlock Prevention

7: Deadlocks 11

Do not allow one of the four conditions to occur.

No preemption:

a) Release any resource already being held if the process can't get an additional resource. b) Allow preemption - if a needed resource is held by another process, which is also waiting on some resource, steal it. Otherwise wait.

Circular wait:

a) Number resources and only request in ascending order. b) EACH of these prevention techniques may cause a decrease in utilization and/or resources. For this reason, prevention isn't necessarily the best technique. c) Prevention is generally the easiest to implement.

DEADLOCKS Deadlock Prevention

7: Deadlocks 13

NOTE: All deadlocks are unsafe, but all unsafes are NOT deadlocks.

SAFE

DEADLOCK

UNSAFE

Only with luck will processes avoid deadlock.

O.S. can avoid deadlock.

DEADLOCKS Deadlock Avoidance

7: Deadlocks 14

Let's assume a very simple model: each process declares its maximum

needs. In this case, algorithms exist that will ensure that no unsafe state is

reached.

EXAMPLE:

There exists a total of 12 tape drives. The current state looks like this:

In this example, < p1, p0, p2 >

is a workable sequence.

Suppose p2 requests and is

given one more tape drive.

What happens then?

Process Max Needs Allocated Current Needs

P0 10 5 5

P1 4 2 2

P2 9 2 7

DEADLOCKS Deadlock Avoidance

There are multiple instances of the resource in these examples.

7: Deadlocks 16

  1. Initialize work = available

Initialize finish[i] = false, for i = 1,2,3,..n

  1. Find an i such that:

finish[i] == false and need[i] <= work

If no such i exists, go to step 4.

  1. work = work + allocation[i]

finish[i] = true goto step 2

  1. if finish[i] == true for all i, then the system is in a safe state.

DEADLOCKS Deadlock

Avoidance

Safety Algorithm

7: Deadlocks 17

Do these examples:

Consider a system with: five processes, P0  P4, three resource types, A, B, C.

Type A has 10 instances, B has 5 instances, C has 7 instances.

At time T0 the following snapshot of the system is taken.

Is the system in a safe state?

DEADLOCKS Deadlock

Safety Algorithm^ Avoidance

P4 0 0 2 4 3 1

P3 2 1 1 0 1 1

P2 3 0 2 6 0 0

P1 2 0 0 0 2 0

P0 0 1 0 7 4 3 3 3 2

A B C A B C A B C

 Alloc   Req   Avail 

Max Needs = allocated + can-be-requested

7: Deadlocks 19

Need an algorithm that determines if deadlock occurred.

Also need a means of recovering from that deadlock.

DEADLOCKS Deadlock Detection

SINGLE INSTANCE OF A RESOURCE TYPE

  • Wait-for graph == remove the resources from the usual graph and collapse edges.
  • An edge from p(j) to p(i) implies that p(j) is waiting for p(i) to release.

7: Deadlocks 20

SEVERAL INSTANCES OF A RESOURCE TYPE

Complexity is of order m * n * n.

We need to keep track of:

available - records how many resources of each type are available. allocation - number of resources of type m allocated to process n. request - number of resources of type m requested by process n.

Let work and finish be vectors of length m and n respectively.

DEADLOCKS Deadlock Detection