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
2- What is a deadlock?
- e.g., two processes A, B want to record a
scanned document on a FD. -
B
A
2
2
1
1
FD
Scanner
Deadlock!!
- Deadlock can also occur across machines.
- More complicated situations can cause deadlocks
involving three or more devices and users.
33.1 Resources
- What is a resource?
- A resource is anything that can be used by
only a single process at any instant of time. - Active resource
- provide as service, e.g., processor and
network adapter. - Passive resource
- system capabilities that is required by
active resources, e.g., memory. - Exclusive resource
- only one process at a time can use it. e.g.,
speaker, processor. - Shared Resource
- can be used by multiple processes, e.g.,
memory. - Single resource
- exists only one in the system, e.g., speaker.
- Multiple resource
- e.g., processors in a multi-processor system.
43.1.1 Preemptable and Nonpreemptable Resources
- Preemptable resource
- can be taken away from a process owning it with
no ill effect, e.g. memory (when a process
exceeds its time quantum and is swapped out) - Nonpreemptable resource
- cannot be taken away from its current owner
without causing the computation to fail. e.g. CD
recorder (If a process has begun to burn a CD,
taking the CD recorder away from it will result
in a garbled CD) - Sequence of events required to use a resource
- request the resource
- use the resource
- release the resource
53.2 Introduction to Deadlocks
- Deadlock can be defined Formally as follows 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. - All processes are waiting.
- None of them will ever cause any of the events
that could wake up any of the other members of
the set. - All processes continue waiting for ever.
- In general, the event that each process is
waiting for is the release of some resource
currently possessed by another member of the set.
63.2.1 Conditions for Deadlock
- Coffman et al. (1971)
- 1. Mutual exclusion condition. Each resource is
either currently assigned to exactly one process
or is available. - 2. Hold and wait condition. Processes currently
holding resources granted earlier can request new
resources. - 3. No preemption condition. Resources previously
granted cannot be forcibly taken away from a
process. They must be explicitly released by the
process holding them. - 4. Circular wait condition. There must be a
circular chain of two or more processes, each of
which is waiting for a resource held by the next
member of the chain. - All 4 conditions must hold for a deadlock to
occur. If one of them is absent, no deadlock is
possible.
73.2.3 Deadlock Modeling
- Modeled with directed graphs
Process
Resource
- 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
8How deadlock occurs? e.g. Process A requests R
S, B requests S, T, C requests T,Rscheduling
round robin
Deadlock occurred!!
9How deadlock can be avoided?If granting a
particular request might lead to deadlock, the
operating system can simply suspend the process
without granting request.
10Strategies for dealing with Deadlocks
- Just ignore the problem altogether. Maybe if you
ignore it, it will ignore you. - Detection and recovery. Let deadlocks occurs,
detect them, and take action. - Dynamic avoidance by careful resource allocation.
- Prevention, by structurally negating one of the
four conditions necessary to cause a deadlock.
113.3 The Ostrich Algorithm
- Pretend there is no problem
- It is reasonable if
- deadlocks occur very rarely
- cost of prevention is high
- It is a trade-off between
- convenience
- correctness
123.4 Deadlock Detection and Recovery
General task of detection Is the system
deadlocked? If so which processes are
involved? 3.4.1 Deadlock detection with One
Resource of Each Type only one resource of each
type exists. e.g. a system might have one
scanner, one CD recorder, one tape drive.
13A system has 7 processes (A-G) and 6 resources
(R-W) Process A holds R and wants S Process B
holds nothing and wants T Process C holds nothing
and wants S Process D holds U and wants S and
T Process E holds T and wants V Process F holds
W and wants S Process G holds V and wants U. Is
the system deadlocked? If so which processes are
involved?
14- Cycle detection in resource graph with
depth-first search algorithm - For each node, N in the graph, perform the
following 5 steps with N as the starting node. - Initialize L to the empty list, and designate all
the arcs as unmarked. - Add the current node to the end of L and check to
see if the node now appears in L two times. If it
does, the graph contains a cycle (listed in L)
and the algrithm terminates. - From the given node, see if there are any
unmarked outgoing arcs. If so, goto step 5 if
not, go to step 6. - Pick an unmarked outgoing arc at random and mark
it. Then follow it to the new current node and go
to step 3. - We have reached a dead end. Go back to the
previous node, that is, the one that was current
just before this one, make that one the current
node, and go to step 3. If this node is the
initial node, the graph does not contain any
cycles and algorithm terminates.
15- What this algorithm does?
- Takes each node in turn as the root and a
depth-first search on it. - If it ever comes back to a node it has already
encountered , then it has found a cycle. - If it exhausts all the arcs from any given node,
it backtracks to the previous node. - If it backtracks to the root and cannot go
further, the subgraph reachable from the current
node does not contain any cycles. - If this property holds for all node, no
deadlock!!
163.4.2 Detection with Multiple Resource of Each
Type
- using vectors and matrices multiple resources
request
hold
P1 P2
- Deadlock detection algorithm is based on
comparing vectors. - For vectors U, V, U?V holds if and only if Ui?Vi,
for 1? i ?m.
17Deadlock detection algorithm
- Initialize all processes as unmarked.
- Look for an unmarked process, Pi, for which the
i-th row of R is less than or equal to A. - If such a process is found, add the i-th row of C
to A, mark the process, and go back to step 1. - If no such process exists, the algorithm
terminates. - When the algorithm finishes, all unmarked
processes, if any, are deadlocked. - What the algorithm does in step 1 is looking for
a process that can be run to completion. - Such a process is characterized as having
resources demands that can be met by the
currently available resources. - The selected process run till completion and
releases the resources - If all processes are able to run, no deadlock.
- If some can never run, deadlock!!
18Example A system has three processes, P1, P2,
and P3, four resource classes, tape drives (4),
plotters (2), scanners (3), and CR-ROMs (1).
Current allocation matrix is shown by C, and
request matrix is shown by R. Will this this
system meet any deadlock?
- P1 cannot run, because there is no CD-ROM
available. - P2 cannot run too, because there is no scanner
available. - P3 can run. When it finishes, it return all its
resource, giving A(2, 2, 2, 0). - At this point P2 can run. When it finishes, it
return all its resource, giving A(4, 2, 2, 1). - Then, P1 can run. NO DEADLOCK!!
193.4.3 Recovery from Deadlock
- Recovery through preemption
- take a resource from some other process
- depends on nature of the resource
- e.g. printer
- Recovery through rollback
- Using checkpoints. A process is checked
periodically. The checkpoints contain the memory
image, the resource state. New checkpoints should
be written to new files. - When a deadlock is detected, it is easy to
identify which resources are used by which
process, a process that owns the needed resource
is rolled back to a point in time before it
acquired the resource.
20- 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 carefully that can be rerun from
the beginning - e.g., killing a process that is updating
database may not be always run safely.
213.5 Deadlock Avoidance3.5.1 Resource Trajectories
- Two process resource trajectories
223.5.2 Safe and Unsafe States
- A state is said to be safe if it is not
deadlocked and there is some scheduling table in
which every process can run to completion even if
all of them suddenly request their maximum number
of resources immediately. - Total number of resources 10
- Run B exclusively, until it asked for and got two
more resources (see (b)). - When B completes (see (c)), the scheduler run C
(see (d)). - Run C NO DEADLOCK!!
- Demonstration that the state in (a) is safe
23- Suppose A requests and gets another resource (see
(b)). Can we find a sequence that is guaranteed
to work? - Run B exclusively, until it asked for and got two
more resources (see (c)). - When B completes, the state is shown in (d).
-
DEADLOCK!!
(a) (b)
(c)
(d)
- Demonstration that the sate in (b) is not safe
24The Banker's Algorithm for a Single Resource
(a)
(b)
(c)
- Three resource allocation states
- safe
- safe
- unsafe
25Banker's Algorithm for Multiple Resources
- Example of banker's algorithm with multiple
resources
263.6 Deadlock Prevention
- Conditions for Deadlocks
- Mutual exclusion condition
- Hold and wait condition
- No preemption condition
- Circular wait condition
- 3.6.1 Attacking the Mutual Exclusion Condition
- Dont assign resource exclusively to a process.
- Feasible spooling printer output
- Not feasible spooling of process table
- Avoid assigning a resource if not absolutely
necessary - As few processes as possible actually claim the
resources
27- 3.6.2 Attacking the Hold and Wait Condition
- Prevent processes that hold resources from
waiting for more resources - Only one way processes request all resources
before starting execution. - If all resources are available, a process can
start otherwise it must wait. - Problems
- Processes often do not know how many resources
they will need until they have started running. - Resources will not be optimally used in this
method. - Variation (different way to break the
hold-and-wait condition) - A process must release all resources it holds,
then request everything it needs all at a time.
28- 3.6.3 Attacking the No Preemption Condition
- Forcibly taking away the resource that a process
is holding and assign to another. - Some resources are nonpreemptable, e.g., printer.
- 3.6.4 Attacking the Circular wait condition
- To have a rule that a process is entitled only
to a single resource at any moment. If it needs a
second one, it must release the first one.
Unacceptable, e.g., copy a huge file from tape to
printer. - Another way global numbering all resources.
Processes can request resources whenever they
want to, but all requests must be made in
numerical order. It might be impossible to find
an ordering that satisfies everyone.
29(a)
(b)
30- Summary of approaches to deadlock prevention
31- Example. 1 The following shows a resource graph.
Do illegal graphs exist, that is graphs that
structurally violate the model introduced in
section 3.2 ? If so, mark them with X.
32- Example. 2 Process A holds resource W and
requests for resource U, Process B holds resource
U and requests for resource V, Process C holds
nothing and request for V, Process D holds
nothing and requests for W, Process E holds
resource V and requests for resource W. Draw the
resource graph and identify whether the system is
deadlocked or not. And if deadlocked, which
processes are involved?
The system is deadlocked. Processes A, B, and E
are deadlocked over resources W, U, and V.
33Example. 3 A system has 7 processes (A-G) and 6
resources (R-W). Process A holds R and wants S
Process B holds nothing and wants T Process C
holds nothing and wants S Process D holds U and
wants S and T, Process E holds T and wants W
Process F holds W and wants S, Process G holds V
and wants U. Draw the resource graph. Is the
system deadlocked? If so which processes are
involved?
34- Example. 4 A system has three processes, P1, P2,
and P3, and four resource classes, which are 4
tape drives, 2 plotters, 3 scanners, and 1
CD-ROM, respectively. - Find the existing resource vector, E.
- At an instant of time, 1 scanner is assigned to
P1, 2 tape drives and 1 CD-ROM are assigned to
P2, and 1 plotter and 2 scanners are assigned to
P3. Find the current allocation matrix, C, and
available resource vector, A. - After a quantum of time passed, P1 further
requests 2 tape drives and 1 CD-ROM, P2 requests
for 1 tape drive and 1 scanner, P3 request for 2
tape drives and 1plotters. Find the request
matrix, R. - In above situation, is the entire system
deadlocked? If yes, what resources are involved?
If not, what is the possible sequence to run
these processes?
35- Example. 5 A system has four processes, P1, ,
P4, and five allocatable resources. The current
allocation and maximum needs are as follows - Allocated Maximum Available resource vector
- P1 1 0 2 1 1 1 1 2 1 3 A(0 0 x 1 1)
- P2 2 0 1 1 0 2 2 2 1 0
- P3 1 1 0 1 0 2 1 3 1 0
- P4 1 1 1 1 0 1 1 2 2 1
- What is the smallest value of x for which this is
a safe state?
If x0, the system encounters a deadlock
immediately. If x1, P4 can run. When it
finishes, A(1 1 2 2 1). In the situation, no
other processes can run, the system is
deadlocked. If x2, P4 can run. When it finishes,
A(1 1 3 2 1). Then P3 can run. When P3 finishes,
A(2 2 3 3 1). Then P2 can run, and the P1
run. Therefore, x2
36- Example. 6 A computer system has six tape drives,
with n processes competing for them. Each process
may need two drives. For which values of n is the
system deadlock free?
Ans. If n3, each process can have 2 drives, no
deadlock. If n4, the distribution of drives can
be (2 2 1 1). The first two processes can run
first, and the last two can run. No deadlock. If
n5, the distribution of drives can be (2 1 1 1
1). The first process can run, then others can
run. No deadlock. If n6, the distribution of
drives can be (1 1 1 1 1 1). When a process
request for another, the system is
deadlocked. Therefore, when n lt 6, the system is
deadlock free.
37- Example. 7 Two processes, A and B, each needs
three records, a, b, and c, in a database. If A
asks for them in the order a, b, and c, and B
asks for them in the same order, deadlock is not
possible. However, if B asks for them in the
order c, b, and a, then the deadlock is possible.
What fraction of all the combinations are
guaranteed to be deadlock free?
Ans. There are six cases for process B to ask for
them as follows. a b c deadlock free a c b
deadlock free b a c possible deadlock b
c a possible deadlock c a b possible
deadlock c b a possible deadlock Chance of
avoiding a deadlock 2/61/3 Chance of getting a
deadlock 4/62/3