unit_5-os
TRANSCRIPT
-
7/29/2019 Unit_5-OS
1/8
7.DEAD LOCKS
Dead lock :-
In a multiprogramming environment several processes may compete for a finitenumber of resources. A process request resources, if the resources are not available at that
time, the process enters in to a wait state. It may happen that waiting processes will never
again change the state, because the resources they have requested are held by other
waiting processes. This situation is called as dead lock.EX1:-
Consider a system with three tape drives. Suppose that there are three processes,each holding one of these three tape drives. If each process now requests for another tape
drive , the system is in dead lock state.
EX2:-If pi is holding printer, and pj is holding tape drive. Now if pi requests for tape drive
and pj requests for printer the system is in dead lock state.
FIG:-
DEAD LOCK CHARACTERIZATIONS:-
NECESSARY CONDITIONS FOR DEAD LOCK:-
A dead lock situation can arise if the following four conditions hold
simultaneously in a system.
1) MUTUAL EXCLUSION:- At least one resource must be held in a non-sharablemode. i.e only one process can hold this resource at a time . other requesting
processes should wait till it is released.2) HOLD & WAIT:- there must exist a process that is holding at least one resource
and is waiting to acquire additional resources that are currently being held by
other processes.
3) NO PREEMPTION:- Resources can not be preempted, that is a resource can bereleased voluntarily by the process holding it, after that process has completed its
task.
4) CIRCULAR WAIT:- There must exist a set {p0,p1,p1.pn} of waitingprocesses such that p0 is waiting for a resource that is held by the p1, p1 is
w.jntuworld.com
www.jntuworld.com
www.jwjobs.net
-
7/29/2019 Unit_5-OS
2/8
waiting for the resource that is held by the p2. And so on. pn is waiting for a
resource that is held by the p0.
FIG:-
RESOURCE ALLOCATION GRAPH:-
To under stand the dead lock state more clearly we use this graph. Thisgraph consists of a set of vertices V and set of edges E. the set of vertices Vis
partitioned in to two types.
P={p1,p2,p3,..pn} and R={r1,r2,r3..rn}
A direct edge from process Pi to resource Rj is denoted by Pi-> Rj. It signifiesthat process Pi is requesting one instance of resource Rj.
A direct edge Rj->Pi indicates that process Pi is holding one instance ofresource Rj.
Here each process Pi shown as a circle and each resource type Rj as a square. If the
resource allocation graph contains no cycle the no process in the system is deadlocked. If on other hand the graph contains cycle , then a dead lock may occur.
A directed edge Pi-> Rj is called request edge. A directed edge Rj->Pi is called
assignment edge.
FIG:-
METHODS FOR HANDLING DEAD LOCKS:-
( Or) HOW TO HANDLE A DEAD LOCKS:-
There are 3 methods for handling a dead locks.
1) Deadlock prevention.2) Deadlock avoidance3) Recovery from dead locks.
1)Dead lock prevention:- We can prevent the occurrence of a dead lock by ensuring
that at least one necessary condition can not hold.
a) mutual exclusion:-
If the resources are non sharable mutual exclusion holds. i.e a printer can not beshared by more than one user. If suppose a read only file can be shared by many, thus do
not involve in a dead lock. But by nature all resources are not shared resources. So , we
can not prevent dead lock by denying the mutual exclusion condition.
w.jntuworld.com
www.jntuworld.com
www.jwjobs.net
-
7/29/2019 Unit_5-OS
3/8
b)Hold & Wait:- Here if a process holds some resources and waits to acquire another set
of resources then dead lock may occurs. To prevent Hold & Wait condition we use two
protocols.
i)Hold but no wait protocol:-
In this all the resources, a process needs will be given to it before it begins itsexecution. Sot he process never waits for additional resources.
Disadvantages:- 1. But some resources are free for most of the time. i.e resource
utilization is very slow.
2. some times it may happen that a process never gets all its needed resources at any time
. there by the process continuous to wait. This situation is called starvation.
ii)Do not hold any thing to request:- If a process needs some additional resources, itcan request any no.of resources, if it does not hold any thing. If if holds any thing and
again wants to request, then it has to omit the resource and then it can request for both
the resources.
Disadvantages:- If a resource is released before it is utilized completely, the state needsto be remembered. It is very difficult.
2.Resource utilization is also low.
c)No Preemption:- If a process do not agree to pre-empt(release) a resource before its
job is completed then dead lock may occur.
1) To prevent No Preemption , we restrict the process to release all its resources. Ifits request is not satisfies immediately.
Disadvantages:- Too expanse , because the process needs to be restarted freshly. That
is the execution till that time is waste.2) Dont release all the resources, but release only those , which are needed by the
other process at which your resources are available.
Disadvantages:- If a resources is preempted, the state should be remembered which isdifficult.
d) Circular Wait:- If a process request a resource which is held by the next process,
that process is waiting for a resource held by its next process.. so on and the lastprocess is waiting for a resource held by the first process. In this case a dead lock may
occurs.
Rn->P1->R1->P2->R2->P3Pn
To prevent circular wait condition
All resources are given an unique number.A process can request a resource, whose number F[R] is greater than the resource
number which it is holding.Proof:-
Rn->P1->R1
i.e F(Rn) < F(R1)
R1->P2->R2i.e F(R1) < F(R2)
.
..
Rn-1 -> Pn->Rn
i.e F(Rn-1)
-
7/29/2019 Unit_5-OS
4/8
=>F(Rn)Pi is reconverted to a claim edge Pi->Rj. Before process Pi starts
execution all its claim edge must already appear in the resource allocation graph.
Here we check for safety by using a cycle- detection algorithm. If no cycleexits, then the allocation of the resource will leave the system in safe state. If a cycle is
found the state is in un safe state.DISADVANTAGES:- this algorithm is not applicable to a resource allocation system
with multiple instances of each resource type.
FIG:-
w.jntuworld.com
www.jntuworld.com
www.jwjobs.net
-
7/29/2019 Unit_5-OS
5/8
Bankers algorithm:-To avoid the occurrence of dead lock the process of serving request is
restricted. This is done through an algorithm called as BANKERS algorithm.
Basic terms:-
Available:- A vector of length m indicates the number of available resources of each
type. If available [j]=k, there are k instance of resource type Rj is available
Max:- An nXm matrix defines the maximum demand of each process. If max [i,j]=k then
process Pi may request at most k instances of resource type Rj.
Allocation:- An nXm matrix defines the number of resources of each type currentlyallocated to each process. If allocation [i,j]=k then process Pi is currently allocated k
instances of resource type Rj.Need:- An nXm matrix indicates the remaining resource need of each process. If
need[i,j]=k then process Pi may need k more instances of resource type Rj to complete its
task. Need [i,j]=max[i, j]-allocation[i,j]
a) SAFETY ALGORITHM:- this algorithm states whether the state is safe or unsafe.
Step1:- Let work and finish be vectors of length m and n respectetively. Initialize
Work = available and
finish[i] := false. For i = 1,2,3.n.Step2:- Find an i such that
a)
finish[i] = falseb) need[i]
-
7/29/2019 Unit_5-OS
6/8
Step 1:- If request[ i] Pj exists if and
only if the corresponding resource- allocation graph contains two edges. Adead lock exists in the system if and only if the wait-for graph contains a
cycle. To detect dead lock, the system need to maintain an algorithm which
searches the wait for graph for a cycle time to time.FIG:-
ii) Several instances of a resource type:-Step1:- Let work and finish be vectors of length m and n, respectively.
Initialize
Work:=available and
For i=1,2,3.n if allocation i not equal to 0 then
Finish[i]=false; other wise
Finish[i]=true.
w.jntuworld.com
www.jntuworld.com
www.jwjobs.net
-
7/29/2019 Unit_5-OS
7/8
Step2:- find an index i such that
a) finish[i]=false
b) request i
-
7/29/2019 Unit_5-OS
8/8
4)How many more are needed.
5)How many processes will need to terminate.
2) Resource preemption:-We successively preempt some resources from processes and give these
resources to other processes until the dead lock cycle is broken.
a) Selecting a victim:- Which resource and which processes are to bepreempted? . Answer is to determine the order of preemption to minimum
cost.
b) Roll back:- If we preempt a resource from a process, what should be donewith that process? Clearly, it can not continue with its normal execution, itis missing some needed resources. We must rollback the process to some
safe state and restart it from that state.
Since in general it is difficult to determine which is a safe state, the
simplest solution is TOTAL ROLLBACK.
c) Starvation:- How can we guarantee that resources will not always bepreempted from the same process?.
In a system where victim selection is primarily based on cost factors,
it may happen that the same process is always picked as a victim. As a resultthe process never completes. The common solution is to include the number of
rollback in the cost factor.
w.jntuworld.com www.jwjobs.net