 Hello everyone, now today we are going to continue with deadlock prevention. As we already studied deadlock prevention mechanism, so at the end of this session student will be able to elaborate deadlock prevention mechanism, so recall that there are four necessary conditions for deadlock to occur that is mutual exclusion, hold and wait, no preemption and circular wait. We already studied mutual exclusion and hold and wait deadlock prevention mechanism in last video and we started with no preemption and we studied the first protocol. So the first protocol for no preemption is no preemption of resources that have already been allocated. So do not print the resources which are already allocated, this is the case, but as we want for deadlock prevention we will allow preemption of the resources. So if process P1 is holding R2 and R3 and again it request for resource R1 then the process must which is held by process P2, R1 is already held by process P2, P1 will enter in waiting state for R1. So the protocol says that remove all the resources which is already held by the process and allocate only when all old and new resources are available. So it is necessary to remove R2 and R3 from the process P1 and P1 will start its execution if and only if R2, R3 and R1 are available, this is what the protocol 1. Now the protocol 2, here again process makes a request for the resources and it check whether that resource is available or not. If it is available then allocate, if no then check whether that resource is allocated to other process which is waiting, the process which is waiting. If that process is waiting then print all the resources from that waiting process and allocate it to the requested process. So for example in previous case as P1 is requesting for R1 but that R1 is already used by P2. Check whether that P2 is in waiting state. If that P2 is in waiting state then print all the resources of P2 and allocate particular R1 to P1 so that P1 can continue its execution. This is the difference between first and second protocol. But if the process is not in waiting state means P2 is not in waiting state then requesting process that P1 must wait. So while P1 is waiting, while process is waiting and if some of its resources may be preempted, if another process request for that resource means now P1 will enter into the waiting state and if that R2 and R3 is requested by some other process then automatically R2 and R3 will be preempted. So the waiting process resources may be preempted. So process is restarted only when all the requested and preempted resources are allocated to it. This is what the protocol 2. Now in which case, just pause the video and think that in which case these protocols can be applied. So these protocols can be applied in case of CPU registers and memory space. State of these resources can be easily saved and restored. We can easily save the state of the CPU registers and memory space. So this protocol 1 and 2 cannot be applied for the resources such as printer and tape drive. It can be allocated to only software resource. Now here CPU registers and memory are software resource or logical resource while printer and tape drive are the physical resource. Now the next, we are going to study the circular weight. Now in circular weight as we know the processes are waiting for each other and the last process is again waiting for the first process. So for this condition, for prevent this circular weight we require total ordering of all the resource types. Resources must be ordered. So each process must request a resource in an increasing order of enumeration. So suppose we have resources R1 to Rm then assign each resource type a unique integer number. We will assign for each resource type a unique integer number and allow to compare two resources and determine whether one precedes another in our ordering. So we will give numbering to each and every resource and that resources must be requested in an increasing order of enumeration. So we will define a one to one function that is R to N where N is nothing but the natural numbers and R is nothing but the resources. Now consider a system with set of three resource types that is tape drive, disk drive and printer. We have three resources then the function F can be defined as F tape drive 1, F of disk drive 5 and F of printer equal to 12. We have one tape drive, five disk drive and 12 printers. Then protocol one to avoid circular weight or to prevent circular weight. First each process can request a resource only in increasing order of enumeration. Process can initially request any number of instance of resource type, any number of instance of resource type RI. So then process can request an instance of resource type RJ if and only if RJ is greater than RI or we can say that RI is less than RJ means the next request always be greater than the current request or the previous request because processes must request the resources in increasing order of enumeration. So if several instances of same resource type are needed a single request for all of them must be issued. Suppose process wants to use tape drive and printer at the same time then it must first request tape drive and then it must request printer because tape drive equal to 1 we have given a number and printer equal to 12. So increasing order of tape drive first and printer next. Then second protocol whenever process request for resource type RJ it has released any resource RI such that F of RI is greater than equal to F of RJ means whenever process is making next request then it must release its previous resources. So if these two protocols are used then we can say that circular weight condition cannot hold into the system. How we will prove this? So we will prove this by contradiction. So consider set of processes P1, P2, P3 up to Pn, Pn number of processes we have where PI is waiting for RI which is held by PI plus 1 then PI plus 1 is waiting for the resource RI which is held by PI plus 2. The next process means P1 is waiting for P2, P2 is waiting for P3, P3 is waiting for P4 like that up to n. This is the condition P1 is waiting for P2, P2 is waiting for P3, P3 is waiting for P4 and then again P5 is waiting for P1. This is the condition that we now we are going to prove how circular weight cannot hold into the system. So how we will prove? Since we are saying that PI plus 1 is holding RI while requesting RI plus 1 we must have that RI less than RI plus 1. Obviously because we are saying that process must request resources in increasing order of enumeration. So f of RI must be less than f of RI plus 1 for all I. So this means that f of R0 should be less than f of R1, f of R1 must be less than R2, f of R2 must be less than f of R3 like that f of Rn must be less than f of R0. But by transitivity we can see here f of R0 less than f of R0 which is not possible. This is impossible. So we can say that this circular weight will never hold in the system if process request a resource with an increasing order of enumeration. So circular weight will never hold in the system. So f should be defined according to normal order of usage. So whenever the f is defined that f must be defined according to the normal order of usage. So the tap drive is usually needed before the printer so it should be reasonable to define tap drive less than f of printer. Now actually first assumption for circular weight is what that all resources must be requested in increasing order of enumeration and we have to give a unique number to each and every resource. So as we have to give unique number to each and every resource while giving numbering to that resource it must be think in such a way that before printer obviously we will require either tap drive or disk drive. So there is no point to access printer first because if we do not have any data then how we will print it. So it is desirable to have a tap drive first and then printer. So like that we have to give numbering to the resources after studying that resource which resource we will need first and which resource we will need next.