This is the last post about deadlocks and in this we will be learning about how to recover from deadlocks. When a deadlock detection algorithm detects a deadlock in the system we must recover from it. Several alternatives can help us for recovering from deadlocks.
There are two ways to recover from the deadlocks, manually and automatically. If a deadlock appears in the system we can either tell the operator to handle it or let the system recover from it by himself.
There are two options for breaking a deadlock.
- Abort some therads to break circular wait.
- Preempt some resources from one or more deadlock threads.
Process And Thread Termination
There are two ways to abort or terminate a process when there is a deadlock in a system. Either terminate all the process in the deadlock or one at a time.
- Terminating all the process at the same time can be expensive. There may be many processes in the deadlock that may have done a lot of computing which will go to waste if we terminate all the processes. These computations have to be computed again by the porcesses.
- Terminating one process at a time until the deadlock is eleminated. This too have its own ups and downs as we have to know what process in the deadlock should we terminate to avoid less computation to go to waste and also the detection algorithm is again invoked to check if there is still a deadlock in the system.
Terminating a deadlock process is not an easy thing to do. There may be some process in the deadlock that is writing to a file, terminating the process can leave the file in an incorrect state. Or a process could be holding a mutex lock.
Resource Preemption
This way of deadling with deadlocks is to preempt the resources from one process to give these resources to some other process until the deadlock is terminated. We take resources from a waiting process and give it to some other process. We can then either terminate the process or we have to roll it back to some previous state so it can ask for resources again to complete its execution.
Following are some factors to consider when preempting resources.
Selection Of Vcitim
Selection of victim is the most important task in preempting a process. To preempt a process we have to consider how much time it consumed or how many resources it is holding right now.
Rollback
When a process is preempted it cannot function properly so we have to roll it back to some safe state so it can be restarted later.
Starvation
We must ensure that no one process is selected victim for more than the limit amount in order to prevent the process from entering into starvation state.