1. Operating Systems
1
A Case Study
On
Deadlock
4th
CSE ‘S’
Prepared by
Yash P. Dhol
Asimhusen N.Saiyad
Jay Pandya
Submitted to
Prof. Nikul Virapariya
2. Operating Systems
2“Crises and deadlocks when they occur have at least this advantage that
they force us to think.”- Jawaharlal Nehru (1889 - 1964)
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”.
An example from US Kansas law:
“When two trains approach each other at a crossing, both shall come to a full
stop and neither shall start up again until the other has gone.”
For another example,
Two processes each want to record a scanned document on a CD.Process A
requests permission to use the scanner and is granted it.Process B is
programmed differently and requests the CD recorder first and is also granted
it.Now A asks for the CD recorder, but the request is denied until B releases
it.Unfortunately, instead of releasing the CD recorder B asks for the
scanner.At this point both processes are blocked and will remain so forever.
This situation is called a deadlock.
Deadlocks can occur in a variety of situations besides requesting dedicated
I/O devices.
For example If process A locks record R1 and process B locks record R2, and
then each process tries to lock the other one's record, we also have a
deadlock. Thus deadlocks can occur on hardware resources or on software
resources.
Preemptable and Nonpreemptable Resources
Resources come in two flavors: Preemptable and Nonpreemptable. A
preemptable resource is one that can be taken away from the process with no
ill effects. Memory is an example of a preemptable resource. On the other
hand, a nonpreemptable resource is one that cannot be taken away from
process (without causing ill effect). For example, CD resources are no
preemptable at an arbitrary moment. Reallocating resources can resolve
deadlocks that involve preemptable resources. Deadlocks that involve
nonpreemptable resources are difficult to deal with.
Necessary Conditions for Deadlock:
Mutual Exclusion Condition:
Explanation: At least one resource (thread) must be held in a non-shareable
mode, that is, only one process at a time claims exclusive control of the
resource.If another process requests that resource, the requesting process
must be delayed until the resource has been released.
Hold and Wait Condition:
3. Operating Systems
3
Explanation: There must exist a process that is holding a resource already
allocated to it while waiting for additional resource that are currently being
held by other processes.
No-Preemptive Condition:
Explanation: Resources cannot be removed from the processes are used to
completion or released voluntarily by the process holding it.
Circular Wait Condition:
Explanation: There exists a set {T1, …, Tn} of waiting threads
• T1 is waiting for a resource that is held by T2
• T2 is waiting for a resource that is held by T3
• …
• Tn is waiting for a resource that is held by T1
T1
T2
R2 R1
5. Operating Systems
5
Ways of Handling Deadlock:
• Deadlock Prevention
• Deadlock Detection
• Deadlock Recovery
• Deadlock Avoidance
Deadlock Prevention:
Remove the possibility of deadlock occurring by denying one of the four
necessary conditions:
– Mutual Exclusion (Can we share everything?)
6. Operating Systems
6
Removing the mutual exclusion condition means that no process
will have exclusive access to a resource.
– Hold & Wait
The hold & wait or resource holding conditions may be removed
by requiring processes to request all the resources they will need
before starting up.
– No preemption
The No preemption condition may also be difficult or impossible
to avoid as a process has to be able to have a resource for a
certain amount of time, or the processing outcome may be
inconsistent.
However, inability to enforce preemption may interfere with
a priority algorithm.
– Circular Wait
The final condition is the circular wait condition. Approaches that
avoid circular waits include disabling interrupts during critical
sections.
Deadlock Detection:
Deadlock Detection with One Resource of Each
Type
7. Operating Systems
7
1. Process A holds R and wants S.
2. Process B holds nothing but wants T.
3. Process C holds nothing but wants S.
4. Process D holds U and wants V.
5. Process E holds T and wants V.
6. Process F holds W and wants S.
7. Process G holds V wants U.
Deadlock Recovery:
• Recovery through preemption
– take a resource from some other process
• Recovery through rollback
– checkpoint a process periodically
– use this saved state
– restart the process if it is found deadlocked
• 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
8. Operating Systems
8
– choose process that can be rerun from the beginning
Deadlock Avoidance:
This approach to the deadlock problem anticipates deadlock before it actually
occurs.
This method differs from deadlock prevention, which guarantees that deadlock
cannot occur by denying one of the necessary conditions of deadlock.
If the necessary conditions for a deadlock are in place, it is still possible to
avoid deadlock by being careful when resources are allocated.
Perhaps the most famous deadlock avoidance algorithm, due to Dijkstra
[1965], is the Banker’s algorithm.
So named because the process is analogous to that used by a banker in
deciding if a loan can be safely made.
Safe State The key to a state being safe is that there is at least one way for all
users to finish. In other analogy, the state of figure 2 is safe because with 2
units left, the banker can delay any request except C's,
thus letting C finish and release all four resources. With four units in hand, the
banker can let either D or B have the necessary units and so on.
Customers Used Max
A 1 6
B 1 5
C 2 4
D 4 7
Available Units= 2
9. Operating Systems
9
Unsafe State Consider what would happen if a request from B for one more
unit were grantedin above We would have following situation
Available Units = 1
Customers Used Max
A 1 6
B 2 5
C 2 4
D 4 7
This is an unsafe state. If all the customers namely A, B, C, and D asked for
their maximum loans, then banker could not satisfy any of them and we would
have a deadlock.
• Three resource allocation states
– safe
– safe
– unsafe
10. Operating Systems
10
The Banker’s Algoritham for Multiple Resources:
E = (6, 3, 4, 2) P = (5, 3, 2, 2) A = (1, 0, 2, 0)
Notice that A = E - P
Going thru this algorithm with the foregoing data, we see that process D's
requirements are smaller than A, so we virtually terminate D and add its
resources back into the available pool:
E = (6, 3, 4, 2)
P = (5, 3, 2, 2) - (1, 1, 0, 1) = (4, 2, 2, 1)
A = (1, 0, 2, 0) + (1, 1, 0, 1) = (2, 1, 2, 1)
Now, A's requirements are less than A, so do the same thing with A:
P = (4, 2, 2, 1) - (3, 0, 1, 1) = (1, 2, 1, 0)
A = (2, 1, 2, 1) + (3, 0, 1, 1) = (5, 1, 3, 2)
At this point, we see that there are no remaining processes that can't be satisfied
from available resources, so the illustrated state is safe.