Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Ninjas!! Got stuck with the word deadlock? Don't worry; Coding Ninjas got you covered. As the article will start with what a deadlock is, followed by the necessary conditions for a deadlock to occur and how can a deadlock can be handled.
Imagine that you and your friend are doing a project together. You have different understandings of the topic and cannot move forward with the project as your opinions are different, so there will be a deadlock because the process of doing the project cannot proceed further.
A deadlock in an operating system is a condition where two or more processes are waiting for some event, but the event does not occur; in this case, those two processes are stuck in a deadlock state.
Deadlock occurs when a process cannot change its state indefinitely due to other processes using the resources it requests. Technically, there is no synchronization between the processes.
Consider two processes, Process_1 and Process_2. At the current moment, process_1 is waiting for Resource_2 while holding Resource_1.
While this is happening, Process_2 has been allocated to the resource Resource_2 and is waiting for Resource_1.
Fig. 1
As a result, Process_P1 awaits the release of its resource while Process_2 waits for Resource_1 allocated to Process_1 for release.
However, no resource is released. Therefore, both are waiting for each other to release the resource. There is no work done here because neither process releases the resource. This is referred to as a deadlock.
There are four necessary conditions for a deadlock.
Mutual Exclusion
Any resources you are using should be used mutually exclusively, meaning both processes cannot use the same resource simultaneously.
The short form of Mutual Exclusion is Mutex, which is a binary semaphore that is a type of communication method that manages access to shared resources. It uses inheritance to avoid the problem of extended priority inversion. By applying this method, tasks with higher priority can be blocked as long as possible.
Deadlocks never occur with shared resources like read-only files but with exclusive access to resources like tape drives and printers.
Fig.2
No Preemption
When a process completes its task, it can release a resource voluntarily. A process that holds some resources requests another that cannot be allocated to it immediately and, in that case, all resources will become available.
There is a process that is waiting for the list of preempted resources. If the process can recover its old resources and request a new one, it will be restarted.
Whenever another resource is available, it is given to the process requesting it. We release it and give it to the requesting process if it was being held by another process waiting for another resource.
Hold and wait
When this condition exists, the process must be stopped if it simultaneously holds one or more resources while waiting for another resource. In the below diagram, the Process_1 currently holding the Resource_1 is waiting for additional Resources_2.
Fig.3
Circular wait
In Fig 1, a Circular wait is occurring. It is a condition when the first process awaits a resource held by the second process, which waits for the resource held by the third process, etc. In the end, The last process is waiting for the resource that the first process holds. Therefore, every process is waiting for the other processes to release the resources, and no one is releasing them. But no one is releasing any resources. This is known as a circular wait.
Resource Utilization
Resources are utilized in the following ways:
Request: If the requested resources are unavailable, the process must wait until they become available.
Use: During the process, resources are used.
Release: By releasing the resource, the process relinquishes control. As a result, other processes can use it.
In this method, it is assumed that there will never be a deadlock. It is best suited to single-user systems where users merely browse the internet and do regular tasks. Most Windows and Linux users use this approach.
Deadlock prevention
WhenMutual Exclusion, hold and wait, and no preemption all have simultaneously, a deadlock occurs. There can never be a deadlock in a system if one of the four conditions can be violated at any time.
Deadlock Avoidance
During deadlock avoidance, the operating system tests each step it performs to see if the system is in a safe or unsafe state. Once a safe state is reached, the process continues. The OS must take one step back when the system is dangerous. One of the methods for deadlock avoidance is the Deadlock Avoidance Algorithmwhich uses Banker's algorithm to avoid a deadlock state.
Deadlock detection and Recovery
Processes can get stuck in a deadlock with this approach and then periodically check if a deadlock has occurred in the system. A deadlock can be removed by applying recovery methods to the system.
Two processes attempt to access a resource and cannot do so because the other prevents them from obtaining exclusive access to it. The two processes are thus unable to proceed. Deadlocks can only be broken by terminating one of the processes.
How do you avoid “hold and wait” conditions?
To avoid a "hold and wait" situation, a process should request all its required resources simultaneously and be blocked until all requests are granted simultaneously.
What is the Ostrich Algorithm?
The strategy is to ignore potential problems by claiming they are extremely rare. It is named after the ostrich effect, which means "sticking one's head in the sand and pretending there is no problem."
How can a system recover from deadlocks?
Resource Preemption: The basic principle of preemption is to remove deadlocks by sequentially transferring resources from one process to another until the deadlock cycle is broken. Three issues that need to be addressed are: 1) Selecting a victim 2) Rollback 3) Starvation Process Termination: By calling exit() on the operating system, a process ends after executing its final statement and requesting deletion from the OS. When this occurs, its parent process may receive a status value from the child process (via the wait() system call). Operating systems deallocate all of the resources -such as physical memory, virtual memory, open files, and buffers -of a process.the
What is the difference between Deadlock and Starvation?
Deadlock
Starvation
This occurs when a process is blocked.
A process with a high priority is allowed to run while a low priority process is blocked.
It suffers from starvation.
Not every starvation suffers from a deadlock.
It is an infinite process.
It is not an infinite process.
After a deadlock, there might be the mutual exclusion and hold and wait. Preemption and circular wait do not take place simultaneously.
This is due to uncontrolled priorities and resource management.
Conclusion
Operating systems often experience deadlocks. Various operating systems may ignore them using the Ostrich approach, while others may handle them differently.
Having a deadlock that involves only one process is impossible. The Banker's algorithm, the safety algorithm, the Detection algorithm, deadlock ignorance, etc., are all used to resolve this deadlock condition. This will prevent future deadlocks by allocating resources differently.
So its time for you now to refer to other articles based on a similar topic, i.e., deadlockinoperating system-
We hope that this blog has helped you enhance your knowledge regarding deadlocks. With this fantastic operating systemcourse, test series, and problems available onCoding Ninjas Studio, you can make learning more enjoyable and stress-free. Are you planning to ace the interviews of reputed product-based companies like Amazon, Microsoft, Google, and more? Attempt our mocktestseries and participate in the contests hosted on Coding Ninjas Studio now!
Do upvote our blog to help other ninjas grow. Good luck with your preparation!