Title: Lecture 12: Deadlock Ch' 3'3
1Lecture 12 Deadlock (Ch. 3.3)
2Deadlocks
- 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.
3Four 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
4Modeling 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
5Using Resource Allocation Graph
A B C
6How deadlock can be avoided
Scheduling B is delayed
(o) (p)
(q)
7Strategies for Dealing with Deadlock
- Just ignore the problem altogether.
- Prevention
- negating one of the four necessary conditions.
- Detection and recovery.
- Avoidance
- careful resource allocation.
8The 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.
9Deadlock Prevention
10Idea invalidate one of the four conditions for
deadlock
- Mutual exclusion condition
- Hold-and-wait condition
- No preemption condition
- Circular wait condition
11Attacking 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
12Attacking 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
13Attacking 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
- !!??
14Attacking the Circular Wait Condition
- Every resource has a unique number
- A process must request resources in increasing
number order
15Requesting 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).
16Summary of deadlock prevention
All these options are very restrictive, and not
very practical
17Deadlock Detection and Recovery
18Recovery 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
19Recovery 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.
20Deadlock Avoidance
21Deadlock 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
22Safe 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.
23The 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
24The state in (b) is not safe
(a) (b)
(c)
(d)
Schedule B
25Unsafe ! 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
26Bankers 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
27The Banker's Algorithm for a Single Resource
(a)
(b)
(c)
- Three resource allocation states
- safe
- safe
- unsafe
28Is 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.