OS Unit-3.1
OS Unit-3.1
Process Process
Hold P1 Request
Request Hold
R1 DEADLOCK R2
P2 Hold
Request
Resource Resource
Preemptable and non-preemptable resource
▪ Preemptable:- Preemptive resources are those which can be
taken away from a process without causing any ill effects to the
process.
• Example:- Memory.
▪ Non-preemptable:- Non-pre-emptive resources are those which
cannot be taken away from the process without causing any ill
effects to the process.
• Example:- CD-ROM (CD recorder), Printer.
Deadlock v/s Starvation
Deadlock Starvation
All processes keep waiting for each other to High priority process keep executing and low
complete and none get executed. priority process are blocked.
Resources are blocked by the process. Resources are continuously utilized by the
higher priority process.
Necessary conditions are mutual exclusion, Priorities are assigned to the process.
hold and wait, no preemption, circular wait.
Also known as circular wait. Also known as lived lock.
It can be prevented by avoiding the It can be prevented by Aging.
necessary conditions for deadlock.
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
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 0 0 0 0
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 0 0 0 0
Hold P1 Request
Hold
R1 R2
P2 Hold
Deadlock recovery (cont…)
2. Recovery through rollback
• PCB (Process Control Block) and resource state are periodically
saved at “checkpoint”.
• When deadlock is detected, rollback the preempted process up
to the previous safe state before it acquired that resource.
• Discard the resource manipulation that occurred after that
checkpoint.
• Start the process after it is determined it can run again.
A A A
F1 F2
R R
First Second B
Checkpoints Checkpoints
Deadlock recovery (cont…)
3. Recovery through killing processes
• The simplest way to break a deadlock is to kill one or more
processes.
• Kill all the process involved in deadlock
• Kill process one by one.
– After killing each process check for deadlock
» If deadlock recovered then stop killing more process
» Otherwise kill another process
Safe and unsafe states
▪ A state is said to be safe if it is not deadlocked and there is some
scheduling order in which every process can run to completion
even if all of them suddenly request their maximum number of
resources immediately.
▪ Total resources are 10 Proce Has Max
ss
▪ 7 resources already allocated
A 3 9
▪ So there are 3 still free
B 2 4
▪ A need 6 resources more to complete it. C 2 7
▪ B need 2 resources more to complete it. Free : 3
▪ C need 5 resources more to complete it.
Safe states
Proce Has Max Proce Has Max Proce Has Max
ss ss ss
A 3 9 A 3 9 A 3 9
4
B 2 4 2 B 4 4 B 0 -
C 2 7 C 2 7 C 2 7
Free : 3 Free : 1 Free : 5
P2 0 1 0 0 P2 0 1 1 2
P3 1 1 1 0 P3 3 1 0 0
P4 1 1 0 1 P4 0 0 1 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 1 0 1 0
total no of each resource resources hold Available (free) resources
P2 0 1 0 0 P2 0 1 1 2
P3 1 1 1 0 P3 3 1 0 0
P4 1 1 1 1 P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 2 1 2 1
total no of each resource resources hold Available (free) resources
P2 0 1 0 0 P2 0 1 1 2
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 1 0 2 1
total no of each resource resources hold Available (free) resources
P2 0 1 0 0 P2 0 1 1 2
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 5 1 3 2
total no of each resource resources hold Available (free) resources
P2 0 1 0 0 P2 0 1 1 2
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 5 0 2 0
total no of each resource resources hold Available (free) resources
P2 0 2 1 2 P2 0 0 0 0
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 5 2 3 2
total no of each resource resources hold Available (free) resources
P2 - - - - P2 0 0 0 0
P3 1 1 1 0 P3 3 1 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 2 1 3 2
total no of each resource resources hold Available (free) resources
P2 - - - - P2 0 0 0 0
P3 4 2 1 0 P3 0 0 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 6 3 4 2
total no of each resource resources hold Available (free) resources
P2 - - - - P2 0 0 0 0
P3 - - - - P3 0 0 0 0
P4 - - - - P4 0 0 0 0
process
P5 0 0 0 0 P5 2 1 1 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 4 2 3 2
total no of each resource resources hold Available (free) resources
P2 - - - - P2 0 0 0 0
P3 - - - - P3 0 0 0 0
P4 - - - - P4 0 0 0 0
process
P5 2 1 1 0 P5 0 0 0 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 6 3 4 2
total no of each resource resources hold Available (free) resources
P2 - - - - P2 0 0 0 0
P3 - - - - P3 0 0 0 0
P4 - - - - P4 0 0 0 0
process
P5 - - - - P5 0 0 0 0
Banker’s algorithm for multiple resource
Tap Tap Tap
CD CD CD
e Plott Scan e Plott Scan e Plott Scan
Rom Rom Rom
Driv ers ners
s Driv ers ners
s Driv ers ners
s
e e e
6 3 4 2 5 3 2 2 1 0 2 0
total no of each resource resources hold Available (free) resources
P2 0 1 0 0 P2 0 1 1 2
P3 1 1 1 0 P3 3 1 0 0
P4 1 1 0 1 P4 0 0 1 1
process
P5 0 0 0 0 P5 2 1 1 0
Deadlock prevention
▪ Deadlock can be prevented by attacking the one of the four
conditions that leads to deadlock.
1. Attacking the Mutual Exclusion Condition
2. Attacking the Hold and Wait Condition
3. Attacking the No Preemption Condition
4. Attacking the Circular Wait Condition
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.
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.
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.
Attacking the circular wait condition
▪ Provide a global numbering of all the resources.
▪ 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.
1. Printer
2. Scanner
3. Plotter
4. Tape Drive
5. CD Rom
▪ 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.
Proces Has Max Proces Has Max Proce Has Max
s s ss
A 1 2 A 2 2 A 0 0
B 1 2 B 1 2 B 1 2
C 1 2 C 1 2 C 1 2
Total : 4, Free : 1 Free : 0 Free : 2
Proce Has Max Proce Has Max Proce Has Max Proce Ha
ss ss ss ss s
A 0 0 A 0 0 A 0 0 A 0
B 2 2 B 0 0 B 0 0 B 0
C 1 2 C 1 2 C 2 2 C 0
Free : 1 Free : 3 Free : 2 Free : 4
Questions
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.
Questions
4. Consider the snapshot of the system with Five Processes and Four
types of resources A,B,C,D.
Allocation Max
A B C D A B C D
P0 0 0 1 2 0 0 1 2
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6