Exercise 1: Given Matrices
Exercise 1: Given Matrices
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
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.