Chapter 05 Deadlocks Modified New 2023
Chapter 05 Deadlocks Modified New 2023
What is deadlock?
Necessary and Sufficient Deadlock Conditions
Dealing with Deadlock Problem
Deadlock Prevention
Deadlock Avoidance
Deadlock Detection
2
Deadlocks
1. Mutual exclusion
◦ One or more than one resource must be held by a process in a non-
sharable (exclusive) mode.
2. Hold and Wait
◦ A process holds a resource while waiting for another resource.
3. No Preemption
◦ There is only voluntary release of a resource - nobody else can make a process
give up a resource.
4. Circular Wait
◦ Process A waits for Process B waits for Process C .... waits for Process A.
6
Deadlock- Resource Allocation
a visual example
A utility program
◦ Copy a file from tape to disk
◦ Print the file to printer
Resources
◦ Tape
◦ Disk
◦ Printer
A deadlock
◦ A holds tape and disk, then requests for a printer
◦ B holds printer, then requests for tape and disk
7
Deadlock- Resource Allocation Graph
Set E contains …
Set V contains….
Set G contains….
How many of instances of each resource are there?
What is each process holding and requesting?
9
Deadlock- Resource Allocation Graph
Allow
Recovery deadlock
to
happen
Fix the problem afterwards
Detection and Dynamic Avoidance Ensure
deadlock
Careful allocation never occurs
13
Deadlock… How to handle … Ignore the
problem
No preemption:
◦ Allow preemption - if a needed resource is held by another process, which is also
waiting on some resource, steal it. Otherwise wait.
◦ Release any resource already being held if the process can't get an additional
resource.
◦ Utilization is low, starvation is possible and sometimes impossible to pre-
emption. (dark side)
Circular wait:
◦ Global Numbering of resources
◦ Only request in ascending order most of the time but descending also prevent
deadlock.
◦ Only one resource at a time for a process. If ask another– release the first one.
17
Deadlock- How to handle…..
Prevention…
.
An edge from p(j) to p(i) implies that p(j) is waiting for p(i) to release
“Wait-for” Graph
22
Deadlock- How to handle…..
Deadlock Avoidance
If we have prior knowledge of how resources will be requested,
it's possible to determine if we are entering an "unsafe" state.
Possible states are:
◦ Deadlock-No forward progress can be made.
◦ Unsafe state -A state that may allow deadlock.
◦ Safe state -A state is safe
◦ enough resources for the first to finish
The rule is simple: If a request allocation would cause an unsafe state,
do not honor that request.
NB: All deadlocks are unsafe, but all unsafe states are NOT deadlocks.
Deadlock- How to handle….. Deadlock 23
Avoidance.. Resource trajectory
horizontal or vertical,
never diagonal (if 1
CPU)
Joint state
Banker’s Algorithm
Multiple instances.
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.
27
Deadlock- How to handle…. Deadlock
Avoidance… Safety Algorithm
Do these examples:
Now try it again with only a slight change in the request by P1.
P1 requests one additional resource of type A, and two more of type C.
Request1 = (1,0,2).
Is Request1 < available? And is the system in a safe state.
Exercise
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
Deadlock- How to handle…. Deadlock 31
Avoidance… Safety Algorithm
32