4.1 Introduce Deadlock:
A Deadlock is a situation where each of the computer processes waits for a resource that is being assigned to another process. In this situation, none of the processes gets executed since the resource it needs, is held by some other process that is also waiting for some other resource to be released.
4.2 Describe necessary conditions for Deadlocks
- Mutual Exclusion
A resource can only be shared in mutually exclusive manner. It implies, if two process cannot use the same resource at the same time.
- Hold and Wait
A process waits for some resources while holding another resource at the same time.
- No preemption
The process which once scheduled will be executed till the completion. No other process can be scheduled by the scheduler meanwhile.
- Circular Wait
All the processes must be waiting for the resources in a cyclic manner so that the last process is waiting for the resource which is being held by the first process.
4.3 Explain methods for handling deadlock:
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption, and circular wait holds simultaneously. If it is possible to violate one of the four conditions at any time then the deadlock can never occur in the system. The idea behind the approach is very simple we have to fail one of the four conditions but there can be a big argument on its physical implementation in the system.
Deadlock Ignorance is the most widely used approach among all the mechanisms. This is being used by many operating systems mainly for end-user uses. In this approach, the Operating system assumes that deadlock never occurs. It simply ignores deadlock. This approach is best suitable for a single end-user system where the User uses the system only for browsing and all other normal stuff.
In deadlock avoidance, the operating system checks whether the system is in a safe state or in an unsafe state at every step which the operating system performs. The process continues until the system is in a safe state. Once the system moves to an unsafe state, the OS has to backtrack one step. In simple words, The OS reviews each allocation so that the allocation doesn’t cause a deadlock in the system.
Deadlock detection and recovery
This approach lets the processes fall in deadlock and then periodically check whether deadlock occurs in the system or not. If it occurs then it applies some of the recovery methods to the system to get rid of deadlock.