CSE321 - 7. Deadlocks
CSE321 - 7. Deadlocks
CSE321 - 7. Deadlocks
Narzu Tarannum
Lecturer
BRACU
Covered in chapter 7
● Deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.
R1
P1R1 Need R2
P1 P2
P2R2 Need R1
R2
● Example
● System has 2 Resources: R1, R2.
● P1 and P2 each hold one resource and each needs another one.
1. Mutual exclusion
2. Hold and wait
3. No preemption
4. Circular wait
If one of them is not present in a system, no deadlock will arise
R1
1. Mutual exclusion:
At least one resource P1 P2
must be held in a
non-sharable mode; R1
P1 P2
process holding at
least one resource is P1 P2
waiting to acquire R2
additional resources
held by other
processes.
3. No preemption:
a resource can be
released only
voluntarily by the
process holding it,
after that process has
completed its task.
4. Circular wait:
There must be a R1
each of which is
waiting for a resource R2
G ( V, E)
Process Request edge Pi Rj
● Pi requests instance of Rj
Pi
Rj
● Pi is holding an instance of Rj
Pi
Rj
● In the figure At this point, two minimal cycles exist in the system:
● P1 → R1 → P2 → R3 → P3 → R2 → P1
● P2 → R3 → P3 → R2 → P2
● Deadlock prevention
● Deadlock avoidance
● Deadlock detection and recovery
● Ignorance
R1 R2
P1 P2
R1 R2 P1 P2 P1 P2
R1 R2 R1 R1
R2 R1 R2 R2
P1 P2
P1 R2 P2 R1
F(Tape drives) = 2
P1(2,4)
F(Disk drives) = 3
P1(3,4,5) F(Scanners) = 4
F(Printers) = 5
● Deadlock prevention
● Deadlock avoidance
● Deadlock detection and recovery
● Ignorance
Has Max
Total: 10 units A 3 9
B 2 4
C 2 7
Free: 3 units
Request = request vector for process Pi. If Requesti [j] = Process Allocation Max Available Need
k then process Pi wants k instances of resource type Rj.
A B C A B C A B C A B C
1. If Requesti Needi go to step 2. Otherwise, raise
error condition, since process has exceeded its P0 0 1 0 7 5 3 3 3 2 7 4 3
maximum claim.
2. If Requesti Available, go to step 3. Otherwise Pi P1 2 0 0 3 2 2 1 2 2
must wait, since resources are not available.
P2 3 0 2 9 0 2 6 0 0
3. Pretend to allocate requested resources to Pi by
modifying the state as follows: P3 2 1 1 2 2 2 0 1 1
Available = Available - Requesti; P4 0 0 2 4 3 3 4 3 1
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
Check a new additional request for P1 (1 0 2) can be granted now?
Process Allocation Max Available Need Process Allocation Max Available Need
A B C A B C A B C A B C A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3 P0 0 1 0 7 5 3 2 3 0 7 4 3
P1 2 0 0 3 2 2 1 2 2 P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0 P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1 P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1 P4 0 0 2 4 3 3 4 3 1
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
A B C A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 7 4 3
P1 2 0 0 3 2 2 1 2 2
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1