A process in operating systems uses different resources and uses resources in the following way.
Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.
Consider an example when two trains are coming toward each other on the same track and there is only one track, none of the trains can move once they are in front of each other. A similar situation occurs in operating systems when there are two or more processes that hold some resources and wait for resources held by other(s). For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
Deadlock can arise if the following four conditions hold simultaneously (Necessary Conditions)
There are three ways to handle deadlock
Deadlock Characteristics
As discussed in the previous post, deadlock has following characteristics.
Deadlock Prevention
We can prevent Deadlock by eliminating any of the above four conditions.
Eliminate Mutual Exclusion
It is not possible to dis-satisfy the mutual exclusion because some resources, such as the tape drive and printer, are inherently non-shareable.
Eliminate Hold and wait
Eliminate No Preemption
Preempt resources from the process when resources required by other high priority processes.
Eliminate Circular Wait
Each resource will be assigned with a numerical number. A process can request the resources increasing/decreasing. order of numbering.
For Example, if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3 lesser than R5 such request will not be granted, only request for resources more than R5 will be granted.
Deadlock Avoidance
Deadlock avoidance can be done with Banker’s Algorithm.
Banker’s Algorithm
Bankers’s Algorithm is resource allocation and deadlock avoidance algorithm which test all the request made by processes for resources, it checks for the safe state, if after granting request system remains in the safe state it allows the request and if there is no safe state it doesn’t allow the request made by the process.
Inputs to Banker’s Algorithm:
The request will only be granted under the below condition:
Example:
Total resource in system:
Available system resource are:
Processes (currently allocated resource):
Process (maximum resources):
Need = maximum resource - currently allocated resources.
Processes (need resources):
Note: Deadlock prevention is more strict than Deadlock Avoidance.
Deadlock Detection
Deadlock Recovery
A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is time and space consuming process. Real-time operating systems use Deadlock recovery.
Recovery method
10 videos|99 docs|33 tests
|
1. What is a deadlock in operating systems? |
2. What are the necessary conditions for a deadlock to occur? |
3. How can deadlocks be prevented in an operating system? |
4. What is the difference between deadlock avoidance and deadlock detection? |
5. How does the Banker's algorithm help in deadlock avoidance? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|