Professional Documents
Culture Documents
Chapter 7: Deadlocks
• Introduction to Deadlock.
• Deadlock Characterization.
• Methods for Handling Deadlocks.
• Deadlocks Avoidance.
• Deadlocks Detection and Recovery.
• Mutual exclusion.
• Hold and wait.
• No preemption.
• Circular wait.
• Process
• Pi requests instance of Rj Pi
Rj
• Pi is holding an instance of Rj Pi
Rj
Basic Facts
• No deadlock.
• Because 𝑃2 and 𝑃4 will finish their jobs
over time.
• Then, 1 instance from 𝑅1 and 1 instance
from 𝑅2 will be free for 𝑃1 and 𝑃3.
non-sharable
sharable
1 2
That is:
• If Pi resource needs are not immediately available, then
Pi can wait until all Pj have finished.
• When Pj is finished, Pi can obtain needed resources,
execute, return allocated resources, and terminate.
• When Pi terminates, Pi +1 can obtain its needed resources,
and so on.
Basic Facts
• If a system is in safe state no deadlocks.
• If a system is in unsafe state possibility of deadlock.
• Avoidance ensure that a system will never enter an unsafe
state.
Avoidance Algorithms:
Resource-Allocation Graph:
• Claim edge Pi → Rj indicated that process Pi may request
resource Rj; represented by a dashed line.
Claim edge
𝑃𝑖 𝑅𝑗
Resource-Allocation Graph:
Banker’s Algorithm:
• Multiple instances of resources.
• Each process must a priori claim maximum use.
• When a process requests a resource it may have to wait.
• When a process gets all its resources it must return them in a
finite amount of time.
No
Yes
No
Yes
The system is in a safe state since the sequence < P1, P3, P0, P2, P4>
satisfies safety criteria.
The system is in a safe state since the sequence < P2, P0, P1, P3, P4>
satisfies safety criteria.
Deadlocks Detection:
If a system does not employ either a deadlock-prevention or a
deadlock avoidance algorithm, then a deadlock situation may occur.
In this environment, the system may provide:
• An algorithm that examines the state of the system to determine
whether a deadlock has occurred.
• An algorithm to recover from the deadlock.
Detection-Algorithm Usage:
• When, and how often, to invoke depends on:
➢ How often a deadlock is likely to occur?
➢ How many processes will need to be rolled back?
▪ One for each disjoint cycle.
• If detection algorithm is invoked arbitrarily, there may be
many cycles in the resource graph and so we would not be
able to tell which of the many deadlocked processes “caused”
the deadlock.
Resource Preemption:
• If preemption is required to deal with deadlocks, then three
issues need to be addressed:
1. Selecting a victim – Which resources and which processes
are to be preempted? (minimize cost).
2. Rollback – return to some safe state, restart process for
that state.
3. Starvation – same process may always be picked as
victim, include number of rollback in cost factor.
https://www.facebook.com/ahmed.hagag.71/