0% found this document useful (0 votes)
85 views5 pages

Exercise 1: Given Matrices

The document provides an example of applying the Banker's Algorithm to check if a system is in a safe state. It describes 5 processes (P0-P4) requesting 4 types of resources. It shows the allocation, max, and available matrices at the start. It then runs the safety algorithm, executing processes in the order of P0, P3, P4, P1, P2 since their needs are less than or equal to available resources each time. It also examines if additional resource requests from P1 can be granted, finding that a request for more type A resources cannot be since not enough are available.

Uploaded by

Punit Meena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views5 pages

Exercise 1: Given Matrices

The document provides an example of applying the Banker's Algorithm to check if a system is in a safe state. It describes 5 processes (P0-P4) requesting 4 types of resources. It shows the allocation, max, and available matrices at the start. It then runs the safety algorithm, executing processes in the order of P0, P3, P4, P1, P2 since their needs are less than or equal to available resources each time. It also examines if additional resource requests from P1 can be granted, finding that a request for more type A resources cannot be since not enough are available.

Uploaded by

Punit Meena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Banker’s Algorithm Example Solutions


Exercise 1
Assume that there are 5 processes, P0 through P4, and 4 types of resources. At T0 we have the
following system state:
Max Instances of Resource Type A = 3 (2 allocated + 1 Available)
Max Instances of Resource Type B = 17 (12 allocated + 5 Available)
Max Instances of Resource Type C = 16 (14 allocated + 2 Available)
Max Instances of Resource Type D = 12 (12 allocated + 0 Available)
Given Matrices
Allocation Matrix Max Matrix Available Matrix
(N0 of the allocated resources By a Max resources that may be used by Not Allocated
process) a process Resources
A B C D A B C D A B C D
P0 0 1 1 0 0 2 1 0 1 5 2 0
P1 1 2 3 1 1 6 5 2
P2 1 3 6 5 2 3 6 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Total 2 12 14 12
1. Create the need matrix (max-allocation)
Need(i) = Max(i) – Allocated(i) Need Matrix = Max Matrix
– Allocation Matrix
(i=0) (0,2,1,0) - (0,1,1,0) = (0,1,0,0)
(i=1) (1,6,5,2) - (1,2,3,1) = (0,4,2,1) A B C D
(i=2) (2,3,6,6) - (1,3,6,5) = (1,0,0,1) P0 0 1 0 0
(i=3) (0,6,5,2) - (0,6,3,2) = (0,0,2,0) P1 0 4 2 1
(i=4) (0,6,5,6) - (0,0,1,4) = (0,6,4,2) P2 1 0 0 1
Ex. Process P1 has max of (1,6,5,2) and allocated by (1,2,3,1) P3 0 0 2 0
Need(p1) = max(p1)- allocated(p1) = (1,6,5,2) – ( 1,2,3,1) = (0,4,2,1) P4 0 6 4 2

2. Use the safety algorithm to test if the system is in a safe state or not?
a. We will first define work and finish:
Finish matrix Work vector
Initially work = available = ( 1, 5 , 2, 0) 1 5 2 0
P0 False
Finish = False for all processes
P1 False
P2 False
P3 False
P4 False

b. Check the needs of each process [ needs(pi) <= Max(pi)], if this condition is true:
 Execute the process , Change Finish[i] =True
 Release the allocated Resources by this process
 Change The Work Variable = Allocated (pi) + Work
2

need0 (0,1,0,0) <= work(1,5,2,0)


Finish matrix P0 will release the allocated Work vector
P0 will be resources(0,1,1,0) 1 6 3 0
P0 – 1 True
executed because Work = Work
need(P0) <= Work P1 False (1,5,2,0)+Allocated(P0)
P0 will be True P2 False (0,1,1,0) = 1,6,3,0
P3 False
P4 False
Need1 (0,4,2,1) <= work(1,6,3,0) Condition Is False P1 will Not be executed
Need2 (1,0,0,1) <= work(1,6,3,0) Condition Is False P2 will Not be executed
Need3 (0,0,2,0) <= work(1,6,3,0) P3 will be executed
P3 will be Finish matrix P3 will release the allocated Work vector
executed because resources (0,6,3,2) 1 12 6 2
P0 – 1 True
need(P3) <= Work Work = Work
P3 will be True P1 False (1,6,3,0)+Allocated(P3)
P2 False ( 0,6,3,2) = 1,12,6,2
P3 -2 True
P4 False
Need4 (0,6,4,2) <= work(1,12,6,2) P4 will be executed
P4 will be Finish matrix P4 will release the allocated Work vector
executed because resources (0,0,1,4) 1 12 7 6
P0 – 1 True
need(P4) <= Work Work = Work
P4 will be True P1 False (1,12,6,2) + Allocated(P4)
P2 False (0,0,1,4) = 1,12,7,6
P3 -2 True
P4 -3 True
Need1 (0,4,2,1) <= work(1,12,7,6) P1 will be executed
Finish matrix P1 will release the allocated Work vector
P1 will be resources (1,2,3,1) 2 14 10 7
P0 – 1 True
executed because Work = Work
need(P1) <= Work P1 -4 True (1,12,7,6) + Allocated(P1)
P1 will be True P2 False (1,2,3,1) = 2,14,10,7
P3 -2 True
P4 -3 True
Need2 (1,0,0,1) <= work(2,14,10,7) P2 will be executed
P2 will be Finish matrix P2 will release the allocated Work vector
executed because resources (1,3,6,5) 3 17 16 12
P0 – 1 True
need(P2) <= Work Work = Work
P2 will be True P1 -4 True (2,14,10,7) + Allocated(P1)
P2 -5 True (1,3,6,5) = 3,17,16,12
P3 -2 True
P4 -3 True
The system is in a safe state and the processes will be executed in the following order:
P0,P3,P4,P1,P2
3

Exercise 2:
If the system is in a safe state, can the following requests be granted, why or why not? Please
also run the safety algorithm on each request as necessary.
a. P1 requests (2,1,1,0)
We cannot grant this request, because we do not have enough available
instances of resource A.
b. P1 requests (0,2,1,0)
There are enough available instances of the requested resources, so first let’s
pretend to accommodate the request and see the system looks like:
Allocation Max Available Need Matrix
A B C D
A B C D A B C D A B C D
P0 0 1 0 0
P0 0 1 1 0 0 2 1 0 1 3 1 0
P1 1 4 4 1 1 6 5 2 P1 0 2 1 1
P2 1 0 0 1
P2 1 3 6 5 2 3 6 6
P3 0 0 2 0
P3 0 6 3 2 0 6 5 2
P4 0 6 4 2
P4 0 0 1 4 0 6 5 6
Now we need to run the safety algorithm:
Initially Let’s first look at P0. Need0 (0,1,0,0) is less than
Work vector Finish matrix work, so we change the work vector and finish
matrix as follows:
1 P0 False
Work vector Finish matrix
3 P1 False
1 P0 True
1 P2 False
4 P1 False
0 P3 False
2 P2 False
P4 False
0 P3 False
P4 False
Need1 (0,2,1,1) is not less than work, so we need to move on to P2.
Need2 (1,0,0,1) is not less than work, so we need to move on to P3.
Need3 (0,0,2,0) is less than or equal to work. Let’s take a look at Need4 (0,6,4,2). This is
Let’s update work and finish: less than work, so we can update work and
Work vector Finish matrix finish:
1 P0 True Work vector Finish matrix
10 P1 False 1 P0 True
5 P2 False 10 P1 False
2 P3 True 6 P2 False
P4 False 6 P3 True
P4 True
4

We can now go back to P1. Need1 (0,2,1,1) is less than work, so work and finish can be updated:
Work vector Finish matrix
1 P0 True
14 P1 True
10 P2 False
7 P3 True
P4 True
Finally, Need2 (1,0,0,1) is less than work, so we can also accommodate this. Thus, the system is
in a safe state when the processes are run in the following order:
P0,P3,P4,P1,P2. We therefore can grant the resource request.
5

Exercise 3
Assume that there are three resources, A, B, and C. There are 4 processes P0 to P3. At T0
we have the following snapshot of the system:
Allocation Max Available 1.Create the need matrix.
Need
A B C A B C A B C A B C
P0 1 0 1 2 1 1 2 1 1 P0 1 1 0
P1 3 3 2
P1 2 1 2 5 4 4
P2 0 1 1
P2 3 0 0 3 1 1
P3 0 1 0
P3 1 0 1 1 1 1
2. Is the system in a safe state? Why or why not?
In order to check this, we should run the safety algorithm. Let’s create the work vector
and finish matrix:
Work vector Finish Need0 (1,1,0) is less than work, so let’s go
matrix ahead and update work and finish:
2 P0 False Work vector Finish
1 P1 False matrix
1 P2 False 3 P0 True
P3 False 1 P1 False
2 P2 False
P3 False
Need1 (3,3,2) is not less than work, so we have to move on to P2.
Need2 (0,1,1) is less than work, let’s Need3 (0,1,0) is less than work, we
update work and finish: can update work and finish:
Work vector Finish matrix Work vector Finish matrix
6 P0 True 7 P0 True
1 P1 False 1 P1 False
2 P2 True 3 P2 True
P3 False P3 True
We now need to go back to P1. Need1 (3,3,2) is not less than work, so we cannot continue.
Thus, the system is not in a safe state.

You might also like