What is Deadlock?
A process in Operating systems uses different resources and uses resources in following way.
Requests a resource -----> Use the resources ----> Release the resources
Resources in a computer System can be -- files, databases, other processes, I/O, Library files, Hardware access etc
Deadlock is a situation where a set of processes are blocked because each process is holding a resources and waiting for another resource acquired by some other process.
Deadlock may be a common problem in multi-processing where several processes share a selected kind of mutually exclusive resource called a soft lock or software.
Example of Deadlock
A real-world example would be traffic, which goes only in one direction. Here, a bridge is taken into account as a resource.
So, when Deadlock happens, it may be easily resolved if one car backs up (Preempt resources and rollback). Several cars may be saved if a deadlock situation occurs. So starvation is feasible.
Conditions for Deadlock to occur
Deadlock can arise if following four conditions hold simultaneously (Necessary Conditions)
Mutual Exclusion -
One or more than one resource are non-shareable(Only one process can use at a time)
Hold and Wait -
A process is holding at least one resource and waiting for other resources.
No Preemption -
A resource cannot be taken from a process unless the process releases the resources.
Circular Wait -
A set of processes are waiting for each other in circular form.
A deadlock is often detected by a resource scheduler because it keeps track of all the resources that are allocated to different processes. After a deadlock is detected, it is resolved using the subsequent methods −
All the processes that are involved within the deadlock are terminated. this can not be a decent approach as all the progress made by the processes is destroyed.
Resources are often preempted from some processes and given to others till the deadlock is resolved.
Deadlock prevention algorithms make sure that a minimum of one in every of the required conditions (Mutual exclusion, hold and wait, no preemption and circular wait) doesn't hold true. However most prevention algorithms have poor resource utilization, and hence lead to reduced throughputs.
Deadlock avoidance is a technique used to avoid deadlock.
It requires information about how different processes would request different resources.
Safe State - if deadlock does not occur then safe state.
Unsafe State - if deadlock occurs then unsafe state.
The idea of avoiding a deadlock is simply not to allow the system to enter an unsafe state that may cause a deadlock.
Once a deadlock is detected, you'll break the deadlock. It is done through other ways, including, aborting one or more processes to interrupt the circular wait condition causing the deadlock and preempting resources from one or more processes which are deadlocked.