Unit2 (B) Final Deadlock
Unit2 (B) Final Deadlock
Deadlocks
❖ Deadlocks
● System Model
● Deadlock Characterization
● Methods for Handling Deadlocks
● Deadlock Prevention
● Deadlock Detection, and Avoidance
● Recovery from Deadlock
Deadlock:
A 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.
System Model
● System consists of resources
● Resource types R1, R2, . . ., Rm
They are CPU cycles, memory space, I/O Devices
● Each resource type Ri has Wi instances For i = 1, 2,
…, n
● Each process utilizes a resource as follows:
1. Requests a resource
2. Use the resource
3. Releases the resource
❖ Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
In the case of Multiple Instances RAG, it becomes difficult to analyze from the RAG that the system is in
a safe state or not.
Basic Facts
Mutual section from the resource point of view is the fact that a resource can never be used by more
than one process simultaneously which is fair enough but that is the main reason behind the deadlock.
If a resource could have been used by more than one process at the same time then the process would
have never been waiting for any resource.
However, if we can be able to violate resources behaving in the mutually exclusive manner then the
deadlock can be prevented.
Spooling
For a device like printer, spooling can work. There is a memory associated with the printer which stores
jobs from each of the process into it. Later, Printer collects all the jobs and print each one of them
according to FCFS. By using this mechanism, the process doesn't have to wait for the printer and it can
continue whatever it was doing. Later, it collects the output when it is produced.
Although, Spooling can be an effective approach to violate mutual exclusion but it suffers from two
kinds of problems.
We cannot force a resource to be used by more than one process at the same time since it will not be
fair enough and some serious problems may arise in the performance. Therefore, we cannot violate
mutual exclusion for a process practically.
No Preemption
If a process that is holding some resources requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released
Circular Wait
Safe State
● For each Pi, the resources that Pi can still request can be satisfied by currently
available resources + resources held by all the Pj, with j < i
● If Pi resource needs are not immediately available, then Pi can wait until all Pj have
finished
● When Pj is finished, Pi can obtain needed resources, execute, return allocated
resources, and terminate
Basic Facts
● If a system is in safe state no deadlocks
➔ Avoidance => ensure that a system will never enter an unsafe state.
● Claim edge Pi Rj indicated that process Pi may request resource Rj; represented by a
dashed line
● Claim edge converts to request edge when a process confirm to requests a resource
● Request edge converted to an assignment edge when the resource is allocated to the
process
● When a resource is released by a process, assignment edge reconverts to a claim edge
● Resources must be claimed a priori in the system
Resource-Allocation Graph
Unsafe State In Resource-Allocation Graph
Banker’s Algorithm
When a process gets all its resources; it must return them in a finite amount of time
Data Structures for the Banker’s Algorithm
Let n = number of processes m =
number of resources types For i,j =
1,2,...,n
Available: Vector of length m. If Available[j] = k, there are k instances of resource
type Rj available
4. If Finish [i] == true for all i, then the system is in a safe state Resource-Request Algorithm for
Process Pi
5 processes P1 – P5;
What will happen if the resource request (1, 0, 0) for process P1 can the system
accept this request immediately?
In this approach, The OS doesn't apply any mechanism to avoid or prevent the deadlocks. Therefore the
system considers that the deadlock will definitely occur. In order to get rid of deadlocks, The OS
periodically checks the system for any deadlock. In case, it finds any of the deadlock then the OS will
recover the system using some recovery techniques.
The main task of the OS is detecting the deadlocks. The OS can detect the deadlocks with the help of
Resource allocation graph.
In single instanced resource types, if a cycle is being formed in the system then there will definitely be a
deadlock. On the other hand, in multiple instanced resource type graph, detecting a cycle is not just
enough. We have to apply the safety algorithm on the system by converting the resource allocation
graph into the allocation matrix and request matrix.
In order to recover the system from deadlocks, either OS considers resources or processes.
For Resource
We can snatch one of the resources from the owner of the resource (process) and give it to the other
process with the expectation that it will complete the execution and will release this resource sooner.
Well, choosing a resource which will be snatched is going to be a bit difficult.
System passes through various states to get into the deadlock state. The operating system canrollback
the system to the previous safe state. For this purpose, OS needs to implement check pointing at every
state.
The moment, we get into deadlock, we will rollback all the allocations to get into the previous safe
state.
Starvation: Avoid with starvation, I.e., don’t preemptive the process with same resource.
For Process
Kill a process
Killing a process can solve our problem but the bigger concern is to decide which process to kill.
Generally, Operating system kills a process which has done least amount of work until now.
System Stability: Deadlock detection and recovery techniques help to keep operating systems stable by
finding and resolving deadlocks as soon as possible.
Resource Optimisation: By detecting and resolving deadlocks, system resources can be used more
efficiently, reducing wastage.
Process Continuity: Deadlock detection guarantees that processes do not become stuck indefinitely,
allowing them to continue execution and finish their job.
Improved System Performance: By identifying and recovering from deadlocks, overall system
performance is improved because processes can continue uninterrupted