Complete Deadlock Unit3 Os Sanjaygoswami
Complete Deadlock Unit3 Os Sanjaygoswami
Here
Process P1 holds resource R1 and waits for resource R2 which
is held by process P2.
Process P2 holds resource R2 and waits for resource R1 which
is held by process P1.
None of the two processes can complete and release their
resource.
Thus, both the processes keep waiting infinitely.
Mutual Exclusion-
By this condition,
There must exist at least one non sharable resource in the
system which can be used by only one process at a time.
If there exists no such resource, then deadlock will never occur.
Printer is an example of a resource that can be used by only
one process at a time.
No Preemption-
By this condition,
Once the resource has been allocated to the process, it can not
be preempted.
It means resource can not be snatched forcefully from one
process and given to the other process.
The process must release the resource voluntarily by itself.
Important Note-
Process
Pi is holding an instance of Rj
Example: Here, there are 3 processes P1, P2 and P3. There are
4 resources R1 and R3 with one instance each, R2 with two
instances, R4 with four instances.
SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 7
Example:
Deadlock Handling-
The various strategies for handling deadlock are-
1. Ensure that the system will never enter
a deadlock state:
a) Deadlock Prevention
b) Deadlock Avoidance
2. Deadlock Detection and Recovery
(allow the system to enter a deadlock
state and then recover)
Deadlock Prevention-
Approach-02:
In this approach,
No Preemption-
Circular Wait-
This condition can be violated by not allowing the processes
to wait for resources in a cyclic manner.
To violate this condition, the following approach is followed-
Approach-
A natural number is assigned to every resource.
Each process is allowed to request for the resources either in
only increasing or only decreasing order of the resource
number.
SanjayGoswami, Deadlock (Unit-3 OS), UCER Page 11
In case increasing order is followed, if a process requires a
lesser number resource, then it must release all the resources
having larger number and vice versa.
This approach is the most practical approach and
implementable.
However, this approach may cause starvation but will never
lead to deadlock.
Deadlock Avoidance
Avoidance Algorithm:
Single instance of a resource type
Use a resource-allocation graph
Multiple instances of a resource type
Use the banker’s algorithm
Banker Algorithm-
1. Available
Available[R1] = K
It means K instances of resource type R1 are currently
available.
2. Max
Max[P1][R1] = K
It means process P1 is allowed to ask for maximum K
instances of resource type R1.
3. Allocation
Allocation[P1][R1] = K
It means K instances of resource type R1 have been
allocated to the process P1.
4. Need
5, 3, 2 + 2, 1, 1 => 7, 4, 3
7, 4, 3 + 0, 0, 2 => 7, 4, 5
7, 5, 5 + 3, 0, 2 => 10, 5, 7
P1 1 1 2 4 3 3 3 1 0
P2 2 1 2 3 2 2
P3 4 0 1 9 0 2
P4 0 2 0 7 5 3
P5 1 1 2 11 2 3
Process R1 R2 R3
P1 3 2 1
P2 1 1 0
P3 5 0 1
P4 7 3 3
P5 10 1 1
Deadlock Detection
Deadlock Recovery
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.