Unit-Iii: Deadlocks & Memory Management
Unit-Iii: Deadlocks & Memory Management
P0 P1
wait (A); wait(B)
wait (B); wait(A)
Bridge Crossing Example
Pi requests instance of Rj
Pi
Rj
Pi is holding an instance of Rj
Pi
Rj
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Resource Allocation Graph With A Cycle But No Deadlock
Basic Facts
If graph contains no cycles no deadlock.
Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can
still request can be satisfied by currently available resources + resources
held by all the Pj, with j<I.
◦ 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.
Example for Safe State
Consider a system with 12 tape drives. Assume there are three processes
P1,P2,P3.Assume we know the maximum number of tape drives that each
process may request.
P1=10, P2=4, P3=9
Suppose at time tnow ,9 tape drives are allocated as follows p1 : 5, p2 : 2,
p3 : 2
So, we have three more tape drives which are free.
This system is in a safe state because it we sequence processes as: <p2, p1,
p3>, then p2 can get two more tape drives and it finishes its job, and returns
four tape drives to the system.
Then the system will have 5 free tape drives. Allocate all of them to p1, it
gets 10 tape drives and finishes its job. p1 then returns all 10 drives to the
system.
Then p3 can get 7 more tape drives and it does its job.
Example of Unsafe State
Consider the above example. At time tnow+1,, p3 requests one
more tape drive and gets it. Now, the system is in an unsafe
state.
There are two free tape drives, so only p2 can be allocated all
its tape drives. When it finishes and returns all 4 tape drives,
the system will have four free tape drives.
p1 is allocated 5, may request 5 more →has to wait
p3 is allocated 3, may request 6 more →has to wait
We allocated p3 one more tape drive and this caused a
deadlock
Basic Facts
If a system is in safe state no deadlocks.
Detection algorithm
Recovery scheme
Single Instance of Each Resource Type