UNIT 5 OSV
UNIT 5 OSV
Looping
• Basic concepts of Deadlock
• Deadlock characteristics
• Deadlock ignorance
• Ostrich algorithm
• Deadlock detection and recovery
• Deadlock avoidance
• Banker’s algorithm
• Deadlock prevention
Basic concepts of Deadlock
Section - 1
What is Deadlock?
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.
Deadlocks are a set of blocked processes each holding a resource and
waiting to acquire a resource held by another process.
Hold P1 Request
Process Process
R1 DEADLOCK R2
Request Hold
Request P2 Hold
Resource Resource
Scanners
Scanners
CD Roms
CD Roms
Plotters
Plotters
Drives
Drives
T= A=
Tape
Tape
4 2 3 1 2 1 0 0
total no of each resource no of resources that are
available (free)
Scanners
Scanners
CD Roms
CD Roms
Process
Process
Plotters
Plotters
Drives
Drives
C= R=
Tape
Tape
P1 0 0 1 0 P1 2 0 0 1
P2 2 0 0 1 P2 1 0 1 1
P3 0 1 2 0 P3 2 1 0 0
no of resources held by each no of resources still needed by
process each process to proceed
Deadlock detection for multiple resources
Scanners
Scanners
CD Roms
CD Roms
Plotters
Plotters
Drives
Drives
T= A=
Tape
Tape
4 2 3 1 0 0 0 0
total no of each resource no of resources that are
available (free)
Scanners
Scanners
CD Roms
CD Roms
Process
Process
Plotters
Plotters
Drives
Drives
C= R=
Tape
Tape
P1 0 0 1 0 P1 2 0 0 1
P2 2 0 0 1 P2 1 0 1 1
P3 2 2 2 0 P3 2 1 0 0
no of resources held by each no of resources still needed by
process each process to proceed
Deadlock detection for multiple resources
Scanners
Scanners
CD Roms
CD Roms
Plotters
Plotters
Drives
Drives
T= A=
Tape
Tape
4 2 3 1 2 2 2 0
total no of each resource no of resources that are
available (free)
Scanners
Scanners
CD Roms
CD Roms
Process
Process
Plotters
Plotters
Drives
Drives
C= R=
Tape
Tape
P1 0 0 1 0 P1 2 0 0 1
DEADLOCK
P2 2 0 0 1 P2 1 0 1 1
P3 0 0 0 0 P3 2 1 0 0
no of resources held by each no of resources still needed by
process each process to proceed
Deadlock recovery
1. Recovery through pre-emption Hold P1 Request
In this method resources are temporarily taken away from its current
owner and give it to another process.
The ability to take a resource away from a process, have another process R1 R2
use it, and then give it back without the process noticing it is highly
dependent on the nature of the resource. P2 Hold
Recovering this way is frequently difficult or impossible.
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
5
A 3 9 A 3 9 A 9 9
6
B 0 - B 0 - B 0 -
C 7 7 7 C 0 - C 0 -
Free : 0 Free : 7 Free : 1
Unsafe state
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
A 3 9 A 4 9 A 4 9
B 2 4 1 B 2 4 B 4 4
2
C 2 7 C 2 7 C 2 7
Free : 3 Free : 2 Free : 0
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
A 1 6 A 1 6 A 1 6
B 1 5 B 1 5 B 5 5
C 0 - C 0 - C 0 -
4
D 7 7 D 0 - D 0 -
Free : 1 Free : 8 Free : 4
Banker’s algorithm for single resource (safe state)
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
A 1 6 A 6 6 A 0 -
B 0 - B 0 - B 0 -
C 0 5 - C 0 - C 0 -
D 0 - D 0 - D 0 -
Free : 9 Free : 4 Free : 10
P5
P3
P1
P4
P2
Process
Drives
Tape
0
1
3
3
1
0
Plotters
Drives
0
1
0
4
1
1
Plotters Scanners
0
1
1
2
0
0
total no of each resource
Scanners CD Roms
0
0
1
1
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
3
1
0
0
Drives
2
CD Roms
1
1
1
0
1
Plotters
Banker’s algorithm for multiple resource
1
0
0
1
1
Scanners
0
0
0
0
2
CD Roms
Tape
1
Plotters
2
Scanners
0
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
0
1
3
3
1
0
Plotters
Drives
0
1
0
4
1
1
Plotters Scanners
0
1
1
2
1
0
total no of each resource
Scanners CD Roms
0
0
1
1
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
3
1
0
0
Drives
2
CD Roms
1
1
1
0
1
Plotters
Banker’s algorithm for multiple resource
1
0
0
0
1
Scanners
0
0
0
0
2
CD Roms
Tape
1
Plotters
1
Scanners
0
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
0
1
3
3
0
Plotters
Drives
0
1
0
4
1
Plotters Scanners
0
1
1
2
0
total no of each resource
Scanners CD Roms
0
0
1
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
3
1
0
0
Drives
2
CD Roms
1
1
1
0
1
Plotters
Banker’s algorithm for multiple resource
1
0
0
0
1
Scanners
0
0
0
0
2
CD Roms
Tape
2
Plotters
2
Scanners
1
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
0
1
4
3
0
Plotters
Drives
0
1
1
4
1
Plotters Scanners
0
1
1
2
0
total no of each resource
Scanners CD Roms
0
0
1
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
3
0
0
0
Drives
2
CD Roms
1
1
0
0
1
Plotters
Banker’s algorithm for multiple resource
1
0
0
0
1
Scanners
0
0
0
0
2
CD Roms
Tape
1
Plotters
2
Scanners
1
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
-
-
0
1
3
0
Plotters
Drives
-
-
0
1
4
1
Plotters Scanners
-
-
0
1
2
0
total no of each resource
Scanners CD Roms
-
-
0
0
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
3
0
0
0
Drives
2
CD Roms
1
1
0
0
1
Plotters
Banker’s algorithm for multiple resource
1
0
0
0
1
Scanners
0
0
0
0
2
CD Roms
Tape
5
Plotters
3
Scanners
2
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
-
-
0
1
3
0
Plotters
Drives
-
-
0
1
4
2
Plotters Scanners
-
-
0
1
2
1
total no of each resource
Scanners CD Roms
-
-
0
0
2
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
3
0
0
0
Drives
2
CD Roms
1
1
0
0
0
Plotters
Banker’s algorithm for multiple resource
1
0
0
0
0
Scanners
0
0
0
0
0
CD Roms
Tape
5
Plotters
2
Scanners
0
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
-
-
-
0
1
3
Plotters
Drives
-
-
-
0
1
4
Plotters Scanners
-
-
-
0
1
2
total no of each resource
Scanners CD Roms
-
-
-
0
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
3
0
0
0
Drives
2
CD Roms
1
1
0
0
0
Plotters
Banker’s algorithm for multiple resource
1
0
0
0
0
Scanners
0
0
0
0
0
CD Roms
Tape
5
Plotters
3
Scanners
2
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
-
-
-
0
4
3
Plotters
Drives
-
-
-
0
2
4
Plotters Scanners
-
-
-
0
1
2
total no of each resource
Scanners CD Roms
-
-
-
0
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
0
0
0
0
Drives
2
CD Roms
1
0
0
0
0
Plotters
Banker’s algorithm for multiple resource
1
0
0
0
0
Scanners
0
0
0
0
0
CD Roms
Tape
2
Plotters
3
Scanners
2
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
-
-
-
-
0
3
Plotters
Drives
-
-
-
-
0
4
Plotters Scanners
-
-
-
-
0
2
total no of each resource
Scanners CD Roms
-
-
-
-
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
0
0
0
0
Drives
2
CD Roms
1
0
0
0
0
Plotters
Banker’s algorithm for multiple resource
1
0
0
0
0
Scanners
0
0
0
0
0
CD Roms
Tape
6
Plotters
4
Scanners
2
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
-
-
-
-
2
3
Plotters
Drives
-
-
-
-
1
4
Plotters Scanners
-
-
-
-
1
2
total no of each resource
Scanners CD Roms
-
-
-
-
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
0
Tape Scanners
0
0
0
0
Drives
2
CD Roms
0
0
0
0
0
Plotters
Banker’s algorithm for multiple resource
0
0
0
0
0
Scanners
0
0
0
0
0
CD Roms
Tape
4
Plotters
3
Scanners
2
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
-
-
-
-
-
3
Plotters
Drives
-
-
-
-
-
4
Plotters Scanners
-
-
-
-
-
2
total no of each resource
Scanners CD Roms
-
-
-
-
-
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
0
Tape Scanners
0
0
0
0
Drives
2
CD Roms
0
0
0
0
0
Plotters
Banker’s algorithm for multiple resource
0
0
0
0
0
Scanners
0
0
0
0
0
CD Roms
Tape
6
Plotters
4
Scanners
2
CD Roms
available (free) resources
Tape
P5
P3
P1
P4
P2
Process
Drives
Tape
0
1
3
3
1
0
Plotters
Drives
0
1
0
4
1
1
Plotters Scanners
0
1
1
2
0
0
total no of each resource
Scanners CD Roms
0
0
1
1
0
CD Roms
Tape
5
Drives
3
Plotters
P5
P3
P1
P4
P2
Process
resources hold
2
2
Tape Scanners
3
1
0
0
Drives
2
CD Roms
1
1
1
0
1
Plotters
Banker’s algorithm for multiple resource
1
0
0
1
1
Scanners
0
0
0
1
2
CD Roms
Tape
1
Plotters
2
Scanners
0
CD Roms
available (free) resources
Deadlock prevention
Section - 6
Deadlock prevention
Deadlock can be prevented by attacking the one of the four conditions that leads to deadlock.
1. Attacking the Mutual Exclusion Condition
▪ No deadlock if each resource can be assigned to more than one process.
▪ We can not assign some resources to more than one process at a time such as CD-Recorder, Printer etc…
▪ So this solution is not feasible.
2. Attacking the Hold and Wait Condition
▪ Require processes to request all their resources before starting execution.
▪ A process is allowed to run if all resources it needed is available. Otherwise nothing will be allocated and it will just
wait.
▪ Problem with this strategy is that a process may not know required resources at start of run.
▪ Resource will not be used optimally.
3. Attacking the No Preemption Condition
▪ When a process P0 request some resource R which is held by another process P1 then resource R is forcibly taken
away from the process P1 and allocated to P0.
▪ Consider a process holds the printer, halfway through its job; taking the printer away from this process without
having any ill effect is not possible.
▪ This is not a possible option.
Deadlock prevention
Deadlock can be prevented by attacking the one of the four conditions that leads to deadlock.
4. Attacking the Circular Wait Condition
▪ Provide a global numbering of all the resources.
1) Printer
2) Scanner
3) Plotter
4) Tape Drive
5) CD Rom
▪ Now the rule is that: processes can request resources whenever they want to, but all requests must be made in
numerical order.
▪ A process need not acquire them all at once.
▪ Circular wait is prevented if a process holding resource n cannot wait for resource m, if m > n.
▪ A process may request 1st a CD ROM, then tape drive. But it may not request 1st a tape drive, then CD ROM.
▪ Resource graph can never have cycle.
Example
Consider a system consisting of four resources of same type that are shared by three
processes, each of which needs at most two resources. Show the system is deadlock free.
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
A 1 2 A 2 2 A 0 -
B 1 2 B 1 2 B 1 2
C 1 2 C 1 2 C 2 7
Total : 4 & Free : 1 Free : 0 Free : 2
Process Has / Hold Max required Process Has / Hold Max required Process Has / Hold Max required
A 0 - A 0 - A 0 -
B 2 2 B 0 - B 0 -
C 7 7 C 1 2 C 2 2
Free : 1 Free : 3 Free : 2
Questions asked in GTU
1. What is RAG? Explain briefly.
2. What is Deadlock? List the conditions that lead to deadlock. How Deadlock can be prevented?
3. Which are the necessary conditions for Deadlock? Explain Deadlock recovery in brief.
4. Consider the snapshot of the system with Five Processes and Four types of resources
A,B,C,D. Allocation Max
▪ Currently available set of resources is
Process A B C D A B C D
(1,5,2,0).
P0 0 0 1 2 0 0 1 2
▪ Answer the following Questions using
P1 1 0 0 0 1 7 5 0
bankers algorithm.
1. Find the content of Need Matrix. P2 1 3 5 4 2 3 5 6
2. Is the System in Safe State? P3 0 6 3 2 0 6 5 2
3. If request from Process P1 arrives P4 0 0 1 4 0 6 5 6
for (0,4,2,0) can the request
be granted immediately