Sei sulla pagina 1di 8

www.jntuworld.

com

www.jwjobs.net

7.DEAD LOCKS
Dead lock :In a multiprogramming environment several processes may compete for a finite number 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-sharable mode. 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 be released 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 waiting processes such that p0 is waiting for a resource that is held by the p1, p1 is

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

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. This graph 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 signifies that process Pi is requesting one instance of resource Rj. A direct edge Rj->Pi indicates that process Pi is holding one instance of resource 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 dead locked. 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 avoidance 3) 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 be shared 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.

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

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 its execution. 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, it can 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 needs to 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. If its 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 is difficult. 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 last process 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->R2 i.e F(R1) < F(R2) . .. Rn-1 -> Pn->Rn i.e F(Rn-1) <F(Rn)

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

=>F(Rn)<F(R1)< F(R2)<..F(Rn-1)< F(Rn) F(Rn)< F(Rn) which is not possible. So circular wait never occurs and thete by dead lock never occurs. 2.DEAD LOCK AVOIDANCE:A method for avoiding dead lock is to required additional information about how resources are to be requested. Here each process declare the maximum number of resources of each type that it may need. Given a priori information, for each process, about the maximum no of resources of each type that may be requested, it is possible to the system will never enter a dead lock state. This algorithm defines the dead lock avoidance approach. STATE:- state is defined by the no of available and allocated resources and maximum demand of the processes. 1)SAFE STATE:- A state is safe if the system can allocate resources to each process in some order and still avoid dead lock. A system is in a safe state only if there exists a safe sequence. Safe sequence:- A sequence of processes<p1,p2,p3.pn> is a safe sequence for the current allocation state, if for each Pi, the resources that Pi can still request can be satisfied by currently available resources plus the resources held by all the Pj with j<i . Pi may need to wait until Pj is completed. If no such sequence exists , then the system state is said to be UNSAFE. A safe state is not a dead lock state. If a state is un safe state then the dead lock may occurs. As long as the state is safe state O.S must avoid deadlock. In an unsafe state , the O.S can not prevent processes from requesting resources, such that dead lock may occurs. RESOURCE- ALLOCATION GRAPH ALGORITHM:Here we introduce a new type of edge, called a claim edge. A claim edge Pi->Rj indicates that process Pi may request resource Rj at some time in the future. It is represented by a dashed line. when a process Pi requests resource Rj, the claim edge Pi->Rj is converted to a request edge. Similarly when a resource Rj is released by Pi, the assignment edge Rj->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 cycle exits, 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:-

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

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 currently allocated 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 un safe. 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] = false b) need[i] <=work. If no such i exits go to step 4. Step3:- work:=work+ allocation[i ] Finish[i]:=true. Goto step 2. Step 4:- If finish[I]=true for all i, then the system is in a safe state. EX:allocation max available need A B C A B C A B C A B C P0 0 1 0 7 5 3 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3 Find the safe sequence. The safe sequence is <P1,P3,P4,P2,P0> RESOURCE-REQUESTALGORITHM:Let request be the requesting vector for process Pi. If request [ i,j]=k; then process Pi is requesting k instances of resource type Rj . 3 3 2 7 1 6 0 4 4 2 0 1 3 3 2 0 1 1

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

Step 1:- If request[ i]<=need[i] then go to step2. otherwise raise an error condition, since the process has exceeded its maximum claim. Step2:- If request[i]<= available go to step3. other wise Pi must wait, since the resources are not available. Step 3:- Have the system pretend to have allocated the requested resources to process Pi by modifying the state as follows. Available:= available request[i] Allocation[i]:=allocation[i] + request[i] Need[i]= max[i]- allocation[i]. If the resulting resource- allocation state is safe the transaction is completed and process Pi is allocated its resources. How ever if the new state is un safe, then Pi must wait for request[i] and the old resource allocation state is restored. DEAD LOCK DETECTION:If a system can not employ either deadlock prevention or a deadlock avoidance algorithm, then a dead lock situation may occurs. In this environment a system must provide. 1) An algorithm that examines the state of the system to determine whether a dead lock has occurred. 2) An algorithm to recover from the dead lock. i) single instance of each resource type (wait for graph):If all resources have a single instance, then we can define a deadlock detection algorithm using a graph called wait for graph. An edge from Pi->Pj in a wait for graph implies that process Pi is waiting for process Pj to release a resource that Pi needs. An edge Pi-> Pj exists if and only if the corresponding resource- allocation graph contains two edges. A dead 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.

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

Step2:- find an index i such that a) finish[i]=false b) request i <=work; if no such i exists go to step 4. Step 3:- work:= work+allocation [i] Finish[i]:= true Goto step2, Step 4:- if finish[i]=false, for some 1<=i<=n then the system is in a dead lock state. EX:- Allocation request available A B C A B C A B C Po 0 1 0 0 0 0 0 0 0 P1 2 0 0 2 0 2 P2 3 0 3 0 0 0 P3 2 1 1 1 0 0 P4 0 0 2 0 0 2 The safe sequence may be <p0,p2,p3,p1,p4> suppose if the request is changed as follows Request A B C P0 0 0 0 now the system is in dead lock state. P1 2 0 2 P2 0 0 1 P3 1 0 0 P4 0 0 2

Recovery from a dead lock:There are two options for breaking a dead lock. One solution is simply to abort(terminate) one or more processes to break the circular wait. The second option is to preempt some resources from one or more of the deadlocked processes. 1) Process termination:a) Abort all dead locked processes:- This method clearly will break the dead lock cycle, but at a great expense, since these processes may have computed for a long time, and the results of these partial computations must be discarded and probably must be recomputed. b) Abort one process at a time until the deadlock cycle is eliminated:This method incurs considerable overhead, since after each process is aborted, the dead lock, detection algorithm should be applied to check whether the deadlock is eliminated or not. But selecting a process to abort is also difficult. Many factors may determine which process is choose, including 1)what is the priority 2)How long the process has computed, and how much longer the process will compute before completing. 3)How many resources it has utilized.

www.jntuworld.com

www.jntuworld.com

www.jwjobs.net

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 be preempted? . 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 done with that process? Clearly, it can not continue with its normal execution, it is 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 be preempted 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 result the process never completes. The common solution is to include the number of rollback in the cost factor.

www.jntuworld.com

Potrebbero piacerti anche