Unit 3B (Kca 203)
Unit 3B (Kca 203)
Resource-allocation graph.
Wait-for graph: .A wait-for graph in computer science is a
directed graph used for deadlock detection in operating systems . In this algorithm, we only
have processes as vertices in the graph. If the Wait-for-Graph contains a cycle then we can say
the system is in a Deadlock state.
Resource allocation graph(RAG)
Resource allocation graph explain to us what is the state of the system in terms
of processes and resources.
RAG contains vertices and edges. In RAG vertices are two type –
1. Process vertex – Every process will be represented as a process vertex and is represented
with a circle.
2. Resource vertex – Every resource will be represented as a resource vertex and represents
as a box. It is also two type –
Single instance type resource – It represents as a box, inside the box, there will be one
dot.So the number of dots indicate how many instances are present of each resource type.
Multi-resource instance type resource – It also represents as a box, inside the box, there
will be many dots present.
Now coming to the edges of RAG.There are two types of edges in RAG –
1. Assign Edge – If you already assign a resource to a process then it is called Assign edge.
2. Request Edge – It means in future the process might want some resource to complete the
execution, that is called request edge.
Resource allocation graph
P1 P2 P1 P2
r1 r2
P3 P3
Resource allocation graph With deadlock
Without deadlock
Wait-for graph and Resource-
allocation graph conversion
Any resource allocation graph with a single
copy of resources can be transferred to a
wait-for graph.
P1 P1
P3 P2 P3 P2
Strategies for handling deadlocks
Deadlock prevention. Prevents deadlocks by restraining requests made to ensure
that at least one of the four deadlock conditions cannot occur.
Deadlock detection and recovery. Allows deadlocks to form; then finds and
breaks them. Recovery is done through preemption, checkpoint and rollback , by
abort or killing the process.
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3
Process Need
A B C
P1 7 4 3
P2 1 2 2
P3 6 0 0
P4 0 1 1
P5 4 3 1
Ans. 2: Apply the Banker's Algorithm:
Available Resources of A, B and C are 3, 3, and 2.
Now we check if each type of resource request is available for each process.
Step 1: For Process P1: Need <= Available
7, 4, 3 <= 3, 3, 2 condition is false.
So, we examine another process, P2.
Step 2: For Process P2: Need <= Available
1, 2, 2 <= 3, 3, 2 condition true
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2
Similarly, we examine another process P3.
Step 3: For Process P3: Need <= Available
6, 0, 0 < = 5, 3, 2 condition is false.
Similarly, we examine another process, P4.
Step 4: For Process P4: Need <= Available
0, 1, 1 <= 5, 3, 2 condition is true
New Available resource = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3
Similarly, we examine another process P5.
Step 5: For Process P5: Need <= Available
4, 3, 1 <= 7, 4, 3 condition is true
New available resource = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5
Now, we again examine each type of resource request for processes P1 and P3.
Step 6: For Process P1: Need <= Available
7, 4, 3 <= 7, 4, 5 condition is true
New Available Resource = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5
So, we examine another process P2.
Step 7: For Process P3:
P3 Need <= Available
6, 0, 0 <= 7, 5, 5 condition is true
New Available Resource = Available + Allocation
7, 5, 5 + 3, 0, 2 => 10, 5, 7
Hence, we execute the banker's algorithm to find the safe state and the safe
sequence like P2, P4, P5, P1 and P3.
Ans. 3: For granting the Request (1, 0, 2), first
we have to check that Request <= Available,
that is (1, 0, 2) <= (3, 3, 2), since the condition
is true. So the process P1 gets the request
immediately.