Lecture 19 (3)
Lecture 19 (3)
Session No.:19
Course Name: Operating System
Course Code: R1UC403B
Instructor Name: Chaya Rawal
Duration: 50 Minutes
Date of Conduction of Class:
Galgotias University 1
Recap : Resource Allocation Graph
• RAG Overview:
• Nodes represent processes (P) and resources (R)
• Edges:
• P → R: request edge
• R → P: assignment edge
• Key Concept:
• If a cycle exists and each resource has only one instance, deadlock may occur
Galgotias University 2
Question based on previous lecture
Galgotias University 3
Opening Question
Galgotias University 4
At the end of this session students will be able to
Galgotias University 5
1 Introduction to Banker's Algorithm
5 Learning Activity 2
Galgotias University 6
Avoidance Algorithms
• Single instance of a resource type
• Use a resource-allocation graph
● Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of
resource type Rj
Galgotias University 15
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi ≤ Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state
Galgotias University 16
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of
resource type Rj
1.If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has
exceeded its maximum claim
2.If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since resources are not
available
3.Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
● If safe ⇒ the resources are allocated to Pi
● If unsafe ⇒ Pi must wait, and the old resource-allocation state is restored
Galgotias University 17
Activity 1 – Role Play
•Assign roles: Each student (or small group) becomes a process (P0–P4)
•Give each group their Max, Allocated, and Need values
•Write initial Available resources on the board
How it works:
•Each group evaluates: “Can we complete with current Available?”
•If yes: "Complete" the process, release resources to Available
•Continue until all processes are complete or stuck
Galgotias University 18
Numerical Example
Considering a system with five processes P0 through P4 and three resources of type A, B, C.
Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time
t0 following snapshot of the system has been taken:
Galgotias University 19
Numerical Example
What will be the content of the Need matrix?
• Need [i, j] = Max [i, j] – Allocation [i, j]
So, the content of Need Matrix is:
Galgotias University 20
Numerical Example
Is the system in a safe state? If Yes, then what is the safe sequence?
• Applying the Safety algorithm on the given system,
Galgotias University 21
Numerical Example
Galgotias University 22
Numerical Example
Galgotias University 23
Numerical Example
Galgotias University 24
Numerical Example
Galgotias University 25
Numerical Example
Galgotias University 26
Numerical Example
What will happen if process P1 requests one additional instance of resource type A and two
instances of resource type C?
We must determine
whether this new
system state is safe.
To do so, we will
again execute Safety
algorithm on the new
data structure.
Galgotias University 27
Activity 2 – Pen & Paper
A single processor system has three resource types X, Alloc Request
Need
Y and Z, which are shared by three processes. There
are 5 units of each resource type. Consider the X Y Z X Y Z
following scenario, where the column Alloc denotes
the number of units of each resource type allocated to
each process, and the column request denotes the P0 1 2 1 1 0 3
number of units of each resource type requested by a
process in order to complete execution. Which of P1 2 0 1 0 1 2
these processes will finish LAST?
1.P0 P2 2 2 1 1 2 0
2.P1
3.P2
4.None of the above since the system is in a deadlock
Galgotias University 28
Conclusion with attainment of LOs in alignment to
the learning activities:
Galgotias University 29
Any Query ?