Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 12

Lecture 19

Deadlock Prevention
Deadlock Prevention

 Mutual Exclusion – not required for sharable


resources; must hold for non-sharable resources.

 Hold and Wait – must guarantee that whenever a


process requests a resource, it does not hold any other
resources.
o Get all the resources after requesting and then start
Deadlock Prevention (Cont.)
 No Preemption –
o If a process that is holding some resources requests,
another resource cannot be immediately allocated to
it, then all currently resources being held are released.
o If a process request for the particular resource, OS
will check if resource available then it will be allocated
to the process, if not, then check either other process
that is waiting for another resource and holding the
same resource required by the process. If it so, then
OS will preempt the resource and allocate to the
process.
Deadlock Prevention (Cont.)
 Circular Wait –Allocate priorities on the resources
and processes demanding higher priority resources
will inherit higher priorities and circular wait / circular
chain is broken. Then no dead lock happens.
 E.g, class attendance = 1
lecture = 2
print notes = 4
Safe State
 When a process requests an available resource, system
must decide if immediate allocation leaves the system in a
safe state.
 System is in safe state if there exists a safe sequence of all
processes.
 Sequence <P1, P2, …, Pn> is safe if for each Pi, the
resources that Pi can still request can be satisfied by
currently available resources. That is:
o If Pi resource needs are not immediately available, then Pi can wait until all Pj have
finished.
o When Pj is finished, Pi can obtain needed resources, execute, return allocated resources,
and terminate.
o When Pi terminates, Pi+1 can obtain its needed resources, and so on.
Example
 Assume units available:
Maximum Needs Current Needs
P0 10 5
P1 4 2
P2 9 2
 Assume P2 was allocated an additional unit:
Maximum Needs Current Needs
P0 10 5
P1 4 2
P2 9 3

not in safe state anymore…


Basic Facts
 If a system is in safe state  no deadlocks.

 If a system is in unsafe state  possibility of deadlock.

 Avoidance  ensure that a system will never enter an unsafe state.


Example
 Unsafe state but still no deadlock:
Maximum Needs Current Needs
P0 10 5
P1 4 2
P2 9 3

Suppose process P2 release some resources. In this case, all processes can
execute
Resource-Allocation Graph Algorithm

 Claim edge Pi  Rj indicated that process Pj


may request resource Rj; represented by a
dashed line.
 Claim edge converts to request edge when a
process requests a resource.
 Request edge converted to an assignment edge
when the resource is allocated to the process
 When a resource is released by a process,
assignment edge reconverts to a claim edge.
Resource-Allocation Graph For Deadlock Avoidance
Unsafe State In Resource-Allocation Graph
Banker’s Algorithm (deadlock avoidance)

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.

You might also like