Banker’s Algorithm
Data Structures for the Banker’s Algorithm
Let n = number of processes,
m = number of resource types
• Available: Vector of length m. If Available [j] = k, there are k instances of resource
type Rj currently available
• Max: n x m matrix. If Max [i,j] = k, then process Pi will request at most k instances
of resource type Rj.
• Alloc: n x m matrix. If Alloc[i,j] = k then Pi is currently allocated (i.e. holding) k
instances of Rj.
• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to
complete its task.
Need [i,j] = Max[i,j] – Alloc [i,j].
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively. Initialize:
Work := Available
Finish [i] == false for i = 1,2, …, n.
2. Find an i such that both:
Finish [i] == false
Needi Work
If no such i exists, go to step 4.
3. Work := Work + Allocationi
(Resources freed when process completes!)
Finish[i] := true
go to step 2.
4. If Finish [i] = true for all i, then the system is in a
safe state.
Resource-Request Algorithm for Process Pi
Requesti = request vector for Pi .
Requesti [j] = k means process Pi wants k instances of resource
type Rj.
1. If Requesti Needi go to step 2. Otherwise, error ( process
exceeded its maximum claim).
2. If Requesti Available, go to step 3. Otherwise Pi must
wait, (resources not available).
3. “Allocate” requested resources to Pi as follows:
Available := Available - Requesti
Alloci := Alloci + Requesti
Needi := Needi – Requesti
If safe the resources are allocated to Pi.
If unsafe restore the old resource-allocation state and
block Pi
Example 3
Example 3
Fined Need
matrix
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
Example 3
Need [i, j] = Max [i, j] – Allocation [i, j]
SYSTEM IS SAFE
What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
Recourse can be
granted after
checking safe state
What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
To do so, we again
execute Safety
algorithm on the
above data
structures.
P1 requests one additional
instance of resource type A
and two instances of
resource type C can be
granted