Title: Deadlocks
1Deadlocks
3.1. Resource 3.2. Introduction
to deadlocks 3.3. The ostrich algorithm
3.4. Deadlock detection and recovery
3.5. Deadlock avoidance 3.6.
Deadlock prevention 3.7. Other issues
2Resources
- Examples of computer resources
- printers
- tape drives
- tables
- Processes need access to resources in reasonable
order - 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
3Resources (cont.)
- 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 - memory is a preemptable resource
- Nonpreemptable resources
- will cause the process to fail if taken away
- CD recorder is a nonpreemptable resource
- NOTE In general deadlocks involve nonpreemptable
resource. Deadlocks involving preemptable
resources can be resolved through reallocation of
resources.
4Resources (cont.)
- Sequence of events required to use a resource
- request the resource
- use the resource
- release the resource
- Must wait if request is denied
- requesting process may be blocked
- may fail with error code
5Introduction to 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 - Usually the event is release of a currently held
resource - None of the processes can
- run
- release resources
- be awakened
6Four Conditions for Deadlock
- Mutual exclusion condition
- each resource is unavailable (because it is
assigned to a process) or available - Hold and wait condition
- process holding resources can request additional
- 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
7Deadlock 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
8Strategies for dealing with Deadlocks
- just ignore the problem altogether
- theres other problems to worry about
- detection and recovery
- let deadlock happen then deal with it.
- dynamic avoidance
- careful resource allocation
- Prevention
- negating one of the four necessary conditions
9How deadlock occurs
A B
C
10How deadlocks can be avoided
(o) (p)
(q)
11The Ostrich Algorithm- just ignore the problem
altogether -
- Pretend there is no problem
- Reasonable if
- deadlocks occur very rarely, as compared to
system crashes because of hardware failure,
compile errors and OS bugs. - cost of prevention is high
- UNIX and Windows takes this approach
- It is a trade off between
- convenience
- correctness
12Detection and Recovery
- Detection
- With one resource of each type
- With Multiple resource of each type
- Recovery
- Through preemption
- Through rollback
- Through killing the process
13Detection with One Resource of Each Type
- Consider Scenario 1
- 7 process A, B, C, D, E, F, G
- 6 resources R, S, T, U, V, W
- A holds R and wants S
- B holds nothing but wants T
- C holds nothing but wants S
- D holds U and wants S and T
- E holds T and wants V
- F holds W and wants S
- G holds V and wants U
- Is the system deadlock? If it is, which processes
are involved?
14Resource Graph of Scenario 1
- Note the resource ownership and requests
- A cycle can be found within the graph, denoting
deadlock - Need an algorithm for detecting cycles in a
directed graph
15Detection with Multiple Resource of Each Type
- Data structures needed by deadlock detection
algorithm
16Example of deadlock detection with Multiple
Resource of Each Type
17Recovery from Deadlock
- Recovery through preemption
- take a resource from some other process
- depends on nature of the resource printers can
be taken away, but input from a live video camera
cannot - Recovery through rollback
- Rollback the process holding the resource needed
to avoid deadlock to an earlier moment when it
did not have the resource, then reassign the
resource to one of the deadlocked process. The
process whose state was rolled back will now need
to compete for the same resource.
18Recovery from Deadlock( cont.)
- 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 from the beginning with no ill-effects
19Deadlock Avoidance- allocate resources carefully
-
- Requires an algorithm that determines whether the
system is in a safe or unsafe state. - Bankers Algorithm for a single resource
- Bankers Algorithm for Multiple Resources
- A safe state is when the OS can guarantee that
all processes will finish no deadlocks - An unsafe state is when the OS cannot guarantee
that all processes will finish there may or may
not be deadlocks.
20Resource Trajectories
- Graphical representation of the algorithm that
determines safe and unsafe states.
21A Safe State
Available resource 10
(a) (b)
(c) (d)
(e)
22Unsafe States
(a) (b)
(c)
(d)
23The Banker's Algorithm for a Single Resource
(a)
(b)
(c)
Available resource 10
- Three resource allocation states
- (a) is safe
- (b) is safe
- (c) is unsafe
24Banker's Algorithm for Multiple Resources
Printer
Printer
25Deadlock Avoidance An impossible Solution
- Deadlock avoidance is possible only if
information about how many resources a process
would need is available in advance. As such
deadlock avoidance is impossible because - A process rarely knows in advance what and how
many resources it will need. - The number of processes changes dynamically as
users log in and out of the system - A resource thought to be available can suddenly
vanish if it fails
26Deadlock Prevention
- Deadlocks can be prevented if one of the four
conditions for a deadlock can be prevented.
27Attacking 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
28Attacking 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
- process must give up all resources
- then request everything it needs all at once
29Attacking 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
- !!??
30Attacking the Circular Wait Condition
- Assign a numerical ID to each resource. Process
can only request for resources in numerical
order, or, as a variation, a process may only
request a resource whose numerical ID is higher
than that of the resource it currently has
If i gt j then A cannot request for j If i lt j
then B cannot request for i
(a)
(b)
Problem the number of resource is so large it is
impossible it have an ordering that satisfies
everyone.
31Summary of Approaches to Deadlock Prevention
32Other IssuesTwo-Phase Locking
- Phase One
- process tries to lock all records it needs, one
at a time - if needed record found locked, start over
- (no real work done in phase one)
- If phase one succeeds, it starts second phase,
- performing updates
- releasing locks
- Note similarity to requesting all resources at
once - Algorithm works where programmer can arrange it
so that the program can be stopped and restarted
at any point during the first phase.
33Nonresource Deadlocks
- Possible for two processes to deadlock
- each is waiting for the other to do some task
- Can happen with semaphores
- each process required to do a down() on two
semaphores (mutex and another) - if done in wrong order, deadlock results
34Starvation
- Algorithm to allocate a resource
- may be to give to shortest job first
- Works great for multiple short jobs in a system
- May cause long job to be postponed indefinitely
- even though not blocked
- Solution
- First-come, first-serve policy