Deadlock is a state in which a process is waiting for the resource that is already used by another process and that another process is waiting for another resource.
There are some techniques used to avoid deadlocks. There are two states involved in it.
- Safe state
- Unsafe state
Safe state is that in which we run processes in sequence. In that case, all processes requesting resource will be fulfilled as no other process is running at a time.
In the unsafe state, there are multiple processes running and requesting resources that may cause a deadlock to occur.
So we prefer the safe state to avoid deadlock. There is also banker algorithm used to avoid deadlock. This algorithm works same as banks work. In a bank there are customers and the bank gives the loan. So bank works like that way in which all customers satisfied.
In banker algorithm, we calculate the resources needed by the process before it goes in the run state. In that case, the process runs and all resources allocated to that process and it is a non-preemptive process.
There are also ways by which we can prevent deadlock.
- Avoid mutual exclusion
- Avoid hold and wait
- Avoid no preemption
- Avoid circular wait
Example of deadlock avoidance:-
If we see real-life example then customers in the bank are similar to processes on the computer. All customers are fulfilled its needs like getting the loan from the bank and no customer waits.