HANDLING THE DEADLOCK
There are three ways to handle a deadlock.They are
- Using Deadlock Prevention and Deadlock Avoidance Algorithms
- If the system has not opted for the above mentioned step then deadlock will surely occur.So after the occurrence system can use Deadlock Detection and Deadlock Recovery to recover from the deadlock.
- Lastly pretend that deadlock never occurs in the system. Of course this step is not the solution to the problem.
1.Deadlock Prevention and Deadlock Avoidance
Deadlock Prevention
By ensuring that at least one of these conditions cannot hold, we can prevent the occurrence of deadlock.
1.Mutual Exclusion
Mutual Exclusion must hold for Non-sharable resources.
eg. a printer cant be simultaneously shared by several process.
Sharable resource do not require mutually exclusive access and thus cant be involved in a deadlock.
eg. read only files are good example
If several process attempts to open read only file at the same time, they can be granted access simultaneously to the file.
We can't prevent deadlock by denying the mutual exclusion condition as some resources are intrinsically Non-sharable.
2.Hold and wait
To deny this condition, need to ensure that whenever a process requests for the resource, it doesn't hold any other resource.
3. No Preemption
Necessary condition for deadlock is that there be no pre-emption of resources that have already been allocated.
To ensure that this condition doesn't hold.One way is "if a process is holding some resource and request another resource that can't be immediately allocated to it, then all the resource currently being held are pre-empted.
The pre-empted resources are added to the list of resource for which the process is waiting.The process restarts only when it regains its old resources as well as the new ones that it is requesting.
Alternatively if a process requests some resources we first check whether they are available.If they are available,we allocate them.
If they are not, check whether they are allocated to some other process i.e waiting for additional resource.
If so then pre-empt the desired resource from the waiting process and allocate them to the requesting process.If the resources are neither available nor held by waiting process,requesting process must wait while it is waiting some of its resources may be pre empted but only if another process requests them.This strategy can't be applied to resources such as printers and tape drives.
4.Circular Wait
Fourth and final condition for deadlock is the circular wait.One way to ensure that this condition never holds is to impose a total ordering of all resource types.And each process should request the resource in increasing order of enumeration.
R={R1,R2,R3,....Rn} set of resource types.
Assign to each resource type a unique integer which allows us to compare two resources and to determine whether one precedes another in our ordering.
Suppose that ordering of tapes--1,disks--4 and printers--8.According to the protocol each process should request the resource in increasing order of enumeration.
So a process holding disk cant request for tapes it can only ask for printers.If it wants tapes resource it should release disk resource.In this way deadlock can be prevented.