deadlocks

23
1 Deadlocks Chapter 3 OPICS Resource Deadlocks The ostrich algorithm Deadlock detection and recovery Deadlock prevention Deadlock avoidance Reference: Operating Systems Design and Implementation (Second Edition) by Andrew S. Tanenbaum, Albert S. Woodhull

Upload: trory

Post on 13-Feb-2016

35 views

Category:

Documents


0 download

DESCRIPTION

Deadlocks. TOPICS Resource Deadlocks The ostrich algorithm Deadlock detection and recovery Deadlock prevention Deadlock avoidance . Chapter 3. Reference: Operating Systems Design and Implementation (Second Edition) - PowerPoint PPT Presentation

TRANSCRIPT

1

Deadlocks

Chapter 3

TOPICS Resource Deadlocks The ostrich algorithm Deadlock detection and recovery Deadlock prevention Deadlock avoidance

Reference:

Operating Systems Design and Implementation (Second Edition)

by Andrew S. Tanenbaum, Albert S. Woodhull

2

Resources(1)

• Examples of computer resources– printers– tape drives– Tables

3

Resources (2)

• Deadlocks occur when …– processes are granted exclusive access to devices– we refer to these devices generally as resources

• Preemptable resources– can be taken away from a process with no ill effects– Example: process swapping form main memory

• Nonpreemptable resources– will cause the process to fail if taken away– Example: print request by more than one proceses

4

Resources (3)

• Sequence of events required to use a resource1. request the resource2. use the resource3. release the resource

• Must wait if request is denied– requesting process may be blocked– may fail with error code

5

Deadlocks

• Suppose a process holds resource A and requests resource B– at same time another process holds B and requests A– both are blocked and remain so

6

Deadlock Modeling

• Modeled with directed graphs

– resource R assigned to process A– process B is requesting/waiting for resource S– process C and D are in deadlock over resources T and U

7

Four Conditions for Deadlock

Four conditions must hold for there to be a deadlock:1. Mutual exclusion condition

• each resource assigned to 1 process or is available2. Hold and wait condition

• process holding resources can request additional3. No preemption condition

• previously granted resources cannot forcibly taken away4. Circular wait condition

• must be a circular chain of 2 or more processes• each is waiting for resource held by next member of the

chain

8

A B CHow deadlock occurs

9

How deadlock can be avoided

(o) (p) (q)

10

Strategy to Deal with Deadlock

Strategies for dealing with Deadlocks1. Just ignore the problem altogether2. Detection and recovery3. Prevention

• Negating one of the four necessary conditions of deadlock

4. Dynamic avoidance • Careful resource allocation

11

Strategy 1: The Ostrich Algorithm

• Just ignore the problem• Reasonable if

– deadlocks occur very rarely – cost of prevention is high

• UNIX and Windows takes this approach• It is a trade off between

– convenience– correctness

12

Strategy 2: Detection and Recovery

Method 1:• Every time a resource is requested or released, the

resource graph is updated, and a check is made to see if any cycle exist.

• If a cycle exists, one of the process is the cycle is killed. If this does not break the deadlock, another process is killed and so on until the cycle is broken

Method2• Periodically check to see if there are any processes that

have been continuously blocked for more than say 1 hour. Such processes are then killed

13

Strategy 3: Deadlock Preventiona) Attacking the Mutual Exclusion Condition

• Some devices (such as printer) can be spooled– only the printer daemon uses printer resource– thus deadlock for printer eliminated

• Not all devices can be spooled

14

b) Attacking the Hold and Wait Condition

• Require processes to request resources before starting– A process is allowed to run if all resources it needed is

available. Otherwise it will just wait.

• Problems– May not know required resources at start of run– Resource will not be used optimally

• Variation: – process must give up all resources and – then request all immediately needed

15

c) Attacking the No Preemption Condition

• This is not a viable option• Consider a process given the printer

– halfway through its job– now forcibly take away printer– !!??

16

d) Attacking the Circular Wait Condition

Numerically ordered resources Resource Graph

• A process may request 1st a printer, then tape dirve. But it may not request 1st a plotter, then a scanner.

• Resource graph can never have cycle.

17

Deadlock Prevention Summary

18

Strategy 4: Deadlock Avoidance

• Carefully analyze each resource request to see if it can be safely granted.

• Need an algorithm that can always avoid deadlock by making right choice all the time.

• Banker’s algorithm (by Dijkstra)

19

Deadlock AvoidanceResource Trajectories

Two process resource trajectories

20

Safe and Unsafe States (1)

Demonstration that the state in (a) is safe

(a) (b) (c) (d) (e)

21

Safe and Unsafe States (2)

(a) (b) (c) (d) unsafe safesafe safe

22

The Banker's Algorithm for a Single Resource

Three resource allocation states

(a) (b) (c)

safe safe unsafe

23

Banker's Algorithm for Multiple Resources

Example of banker's algorithm with multiple resources