Lecture 12: Deadlock Ch' 3'3 - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Lecture 12: Deadlock Ch' 3'3

Description:

A set of processes is deadlocked if each process in the set ... The Ostrich Algorithm. Reasonable if. deadlocks occur very rarely. cost of prevention is high. ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 29
Provided by: jas583
Category:

less

Transcript and Presenter's Notes

Title: Lecture 12: Deadlock Ch' 3'3


1
Lecture 12 Deadlock (Ch. 3.3)
2
Deadlocks
  • Formal definition 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.

3
Four Conditions for Deadlock
  • Mutual exclusion condition
  • each resource assigned to 1 process or is
    available
  • Hold-and-wait condition
  • process holding resources can request additional
    resources
  • No preemption condition
  • previously granted resources cannot forcibly
    taken away
  • 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

4
Modeling Deadlock with Graphs
  • Process circle, Resource square
  • 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

5
Using Resource Allocation Graph
A B C
6
How deadlock can be avoided
Scheduling B is delayed
(o) (p)
(q)
7
Strategies for Dealing with Deadlock
  • Just ignore the problem altogether.
  • Prevention
  • negating one of the four necessary conditions.
  • Detection and recovery.
  • Avoidance
  • careful resource allocation.

8
The Ostrich Algorithm
  • Reasonable if
  • deadlocks occur very rarely.
  • cost of prevention is high.
  • UNIX and Windows take this approach.
  • It is a tradeoff between
  • convenience
  • correctness

General-purpose OSs do not do much towards
deadlock detection, recovery, avoidance, or
prevention.
9
Deadlock Prevention
10
Idea invalidate one of the four conditions for
deadlock
  • Mutual exclusion condition
  • Hold-and-wait condition
  • No preemption condition
  • Circular wait condition

11
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
  • Principle
  • avoid assigning resource when not absolutely
    necessary
  • as few processes as possible actually claim the
    resource

12
Attacking the Hold and Wait Condition
  • Require processes to request resources before
    starting
  • a process never has to wait for what it needs
  • Problems
  • may not know required resources at start of run
  • also ties up resources other processes could be
    using
  • Variation before requesting a new resource,
  • process must give up all resources
  • then request all immediately needed

13
Attacking the No Preemption Condition
  • In general this is not a viable option
  • Consider a process given the printer
  • halfway through its job
  • now forcibly take away printer
  • !!??


14
Attacking the Circular Wait Condition
  • Every resource has a unique number
  • A process must request resources in increasing
    number order

15
Requesting in Order Prevents Deadlock
  • 2 resources (i gt j) , 2 processes A, B
  • Deadlock can only occur if
  • A holds i and requests j,
  • B holds j and requests i.
  • But if they requested resources in order, after A
    holds i it is not allowed to request j!
  • It should have asked for j earlier (and become
    blocked).

16
Summary of deadlock prevention
All these options are very restrictive, and not
very practical
17
Deadlock Detection and Recovery
18
Recovery from Deadlock (1)
  • Recovery through preemption
  • take a resource from some other process
  • depends on nature of the resource
  • Recovery through rollback
  • checkpoint a process periodically
  • use this saved state
  • restart the process if it is found deadlocked

19
Recovery from Deadlock (2)
  • Recovery through killing processes
  • crudest but simplest way to break a deadlock
  • kill one of the processes in the deadlock cycle
  • the other processes get its resources
  • choose process that can be rerun from the
    beginning
  • None of the deadlock recovery options is very
    attractive.

20
Deadlock Avoidance
21
Deadlock Avoidance
  • Can we make decisions one at a time and always
    avoid deadlock?
  • Need some advance knowledge
  • Each process has to declare, in advance, what
    resources, and what is the maximum number of
    resource units, it will need

22
Safe and Unsafe States
  • The State of the system includes
  • How many resources are allocated to each process
  • What is the maximum number of resources a process
    could request
  • How many are available
  • A state is Safe if
  • It is not deadlocked
  • There exists a scheduling order in which all
    processes run to completion.

23
The state in (a) is safe
Only one resource, 10 copies exist
(a) (b)
(c) (d)
(e)
After B terminates we could Schedule C
We could schedule B
24
The state in (b) is not safe
(a) (b)
(c)
(d)
Schedule B
25
Unsafe ! Deadlocked
  • If state is safe ? deadlock can be avoided.
  • If state is unsafe ? not clear
  • We are making worst case assumptions, processes
    could use less than their maximum

26
Bankers Algorithm (Dijkstra, 1965)
  • When process requests resource X,
  • Assume the process gets X
  • Loop
  • Find unmarked process i with Ri A // i can run
  • If found A ? A Ci mark
    process i as terminated. // assume i has run,
    make its resources available
  • Else System is deadlocked, giving the resource
    was unsafe

27
The Banker's Algorithm for a Single Resource
(a)
(b)
(c)
  • Three resource allocation states
  • safe
  • safe
  • unsafe

28
Is Deadlock Avoidance Practical?
  • Bankers algorithm needs information about future
    needs (max values for each resource).
  • In general OS does not have this information.
  • Number of processes varies.
  • Resources can disappear.
Write a Comment
User Comments (0)
About PowerShow.com