Deadlock avoidance, its detection and recovery
Difference between deadlock prevention and deadlock avoidance
Deadlock Avoidance:
- What it does: It’s like driving a car and always checking the road ahead to make sure you never get stuck in traffic. Before giving a resource to a process, the system checks if doing so could lead to a deadlock in the future. If it’s safe, the process gets the resource. If not, the process has to wait.
- How it works: The system carefully manages resources by constantly keeping track of what’s available, what’s being used, and what each process might need in the future. This way, it can avoid any situation where processes might get stuck waiting for each other.
Deadlock Prevention:
- What it does: Instead of constantly checking like in avoidance, deadlock prevention sets up rules from the beginning to make sure a deadlock can never happen. It’s like setting up traffic lights and one-way streets to ensure cars never get stuck in a loop.
- How it works: The system makes sure that at least one of the conditions needed for a deadlock never happens. For example, it might make sure that processes request all their needed resources at once, or that they release resources if they can’t get everything they need right away.
What exactly is deadlock avoidance?
In deadlock avoidance, the primary goal is to ensure that the system never enters a deadlock state. This approach relies on knowing the current state of the system and carefully managing the allocation of resources to processes to maintain a safe state.
1. System State:
- The current state of the system includes:
- The number of processes.
- The total number of resources available in the system.
- The number of resources currently allocated to each process.
- The maximum need of each process (i.e., the maximum number of resources each process may request).
2. Safe State:
- Now we aim to schedule processes and resources so that our system is safe. A state is considered safe if there exists a sequence of process execution where each process can complete its execution without causing a deadlock.
- Resources are allocated only if they will keep the system in a safe state. If allocating a resource might lead to an unsafe state (and potentially a deadlock), the system will delay the resource allocation.
Bankers' algorithm
It is a classic example where the system simulates the allocation of resources and checks if the resulting state would be safe. If it’s safe, the allocation proceeds; otherwise, the process must wait.
In the image above, we see that the first process needs 7 units of resource A, but since only 3 units of A are currently available, we can't allocate resources to this process right now. So, we move on to the next process. For the second process, the number of required resources is available, so we allocate the remaining resources to it. Once process 2 completes its execution, it will release all the resources it's holding. We then repeat this procedure for the remaining processes, checking if they can be allocated the resources they need. this way we can find the safe state for the system.
Deadlock detection
In systems that do not use deadlock avoidance or deadlock prevention, we rely on deadlock detection to find out if a deadlock has occurred.
- How Deadlock Detection Works (Detection Algorithm): The system periodically runs an algorithm to check if any deadlocks exist. This algorithm examines the state of resource allocation and processes to see if there's a circular dependency, which is a sign of a deadlock. Here we can also use the Bankers algorithm to figure out if the system is in a safe state or not. If it isn't in a safe state, then it is in deadlock and vice-versa.
- If Deadlock is Detected: If the algorithm finds that the system is in a deadlock, it triggers a recovery process. Recovery might involve actions like forcibly terminating some processes or all processes dealing with a deadlock or preempting resources from some processes to break the deadlock.
- If No Deadlock is Found: If the system is not in a deadlock, the detection algorithm will be run again after a certain time interval to continuously monitor the system.

Comments
Post a Comment