Deadlock is the situation which arrives to an process which is in waiting state for using the resource, which is being used by another process for long time.
Dead lock occurs when all the following conditions hold:
2)Hold and Wait
So, the dead lock can be prevented by ensuring at least one of the four above conditions does not hold.
Ways to prevent the deadlock are:
A)No Mutual Exclusion :
- In Mutual Exclusion ,if one process uses an non-shareable resource, another process willing to use the same resource ,it must wait until the first process releases the resource.
- Consider mutual exclusion in the case of ATM, people(process) who stand in the queue for withdrawing the money from ATM machine must wait for their turn in order to use the resource(ATM machine)
- To eliminate mutual exclusion, all the resources in the system must be sharable.
- It is impossible to dis-satisfy the mutual exclusion because there are some resources which are non-shareable.
Some examples of the non-shareable resources of the system are tap drive and printer.
B)No Hold and Wait Condition:
In the hold and wait condition, process holds all the resources it requires before its execution.
To Make this condition false, there are two approaches :
1)One approach is process requests and gets allocated to all the resources that are useful before the starting of the execution.
Disadvantage : It is time Consuming. Consider copying of file to drive and printing it .The process requests all the resources(DVD drive,printer) that are useful for execution and gets allocated. Printer is used at last for the printing the file after the copying file in the DVD drive to disk. Another processes which are wants to use the printer must wait until the whole execution of the first process completes .
2)Another approach is process requests some of the resources and uses them .Before requesting another resources process must release all the resources that are previously requested.
Disadvantage : It is also time consuming. Consider the copying of the file to drive and printing it. The process requests the resources that it wants one by one .If the process wants the resource which is used by the another process it has to wait each and every time to use the resource. This leads to the starvation of the process for the resource.
C) Preemption Condition : There are approaches for the elimination ,
1)If the process requesting the resource which is held by the another waiting process then the resource is preempted that is given to the requested process.
2)If the process1 requested for an resource which is using by the another process2,then resources held by the process1 is preempted to the other processes which are requested for the respective resources and the list of the resouces which are preempted are added to the list of the required resources with the new resource that is to be allocated. When all the resources in the list are available then the process1 restart its execution.
3)Resources are allocated to the processes with the highest priority value.
4)Another approach, by using TIME QUANTUM METHOD, each and every process is given some certain amount of time for execution after the completion of the time the process must release the resource and other process utilizes the resource within its given time.
D)No Circular Wait : First all the resources must be given an value in the increasing order. If the process is requested for an resource with a value then after the process must request the resources with value higher than the previous value.
consider the values of the resources as printer=2,scanner=5,camera=6.If the process requested for the scanner(it has value of 5) after its execution if it has to request another resource then process must request the resource higher than
5)This algorithm increases the complexity and also leads to the low utilization.