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

Ex 07

The document presents a series of problems related to the Banker's algorithm for resource allocation in a system with multiple processes and resource types. It demonstrates how to determine if the system is in a safe state at different time intervals based on the allocation and need of resources by each process. The analysis concludes that the system remains safe under various scenarios of resource requests by the processes.

Uploaded by

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

Ex 07

The document presents a series of problems related to the Banker's algorithm for resource allocation in a system with multiple processes and resource types. It demonstrates how to determine if the system is in a safe state at different time intervals based on the allocation and need of resources by each process. The analysis concludes that the system remains safe under various scenarios of resource requests by the processes.

Uploaded by

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

Problem 1.

• A system has 16 tapes, and 4 processes P0, P1, P2 , P3with corresponding


requests:
◦ P0 requests at most 10 tapes
◦ P1 requests at most 4 tapes
◦ P2 requests at most 9 tapes
◦ P3 requests at most 5 tapes
• At t0, P0 has 5 tapes, P1 and P2 each has 2 tapes , P3 has 3 tapes
◦ 4 tapes available
◦ Is the system safe with Banker’s algorithm?

available = [4]
max = [10, 4, 9, 5]
allocation = [5,2,2,3]
need = [5,3,7,2]
finish = [False, False, False, False]

work = available = [4]

i = 1, need[1] <= work[1] , (3 < 4) => work = work + allocation[1] = 4 + 2 =


6, finish[1] = True
i = 0, need[0] <= work[0] , (5 < 6) => work = work + allocation[0] = 6 + 5 =
11, finish[0] = True
i = 2, need[2] <= work[2] , (7 < 11) => work = work + allocation[2] = 11 + 2
= 13, finish[2] = True
i = 3, need[3] <= work[3] , (2 < 13) => work = work + allocation[3] = 13 + 3
= 16, finish[3] = True
finish = [True, True, True, True] => System is safe

• At t1, P0 has 7 tapes, P1 and P2 each has 2 tapes , P3 has 3 tapes


◦ 2 tapes available
◦ Is the system safe with Banker’s algorithm?

available = [2]
max = [10, 4, 9, 5]
allocation = [7,2,2,3]
need = [3,2,7,2]
finish = [False, False, False, False]

work = available = [2]

i = 1, need[1] <= work , (2 <= 2) => work = work + allocation[1] = 2 + 2 =


4, finish[1] = True
i = 0, need[0] <= work , (3 <= 4) => work = work + allocation[0] = 4 + 7 =
11, finish[0] = True
i = 2, need[2] <= work , (7 <= 11) => work = work + allocation[2] = 11 + 2 =
13, finish[2] = True
i = 3, need[3] <= work , (2 <= 13) => work = work + allocation[3] = 13 + 3 =
16, finish[3] = True
finish = [True, True, True, True] => system is safe

• At t2, P0 has 7 tapes, P1 has 3 tapes, P2 each has 2 tapes, P3 has 3 tapes
◦ 1 tapes available
◦ Is the system safe with Banker’s algorithm?

available = [1]
max = [10, 4, 9, 5]
allocation = [7,3,2,3]
need = [3,1,7,2]
work = available = [1]
finish = [False, False, False, False]
i = 1, need[1] <= work, (1 <= 1) => work = work + allocation[1] = 1 + 3 = 4,
finish[1] = True
i = 0, need[0] <= work, (3 <= 4) => work = work + allocation[0] = 4 + 7 =
11, finish[0] = True
i = 2, need[2] <= work, (7 <= 11) => work = work + allocation[2] = 11 + 2 =
13, finish[2] = True
i = 3, need[3] <= work, (2 <= 13) => work = work + allocation[3] = 13 + 3 =
16, finish[3] = True
finish = [True, True, True, True] => system is safe

• At t3, P0 has 7 tapes, P1 has 3 tapes, P2 each has 2 tapes, P3 has 4 tapes
◦ 0 tape available
◦ Is the system safe with Banker’s algorithm?

available = [0]
max = [10, 4, 9, 5]
allocation = [7,3,2,4]
need = [3,1,7,0]
work = available = [0]
finish = [False, False, False, False]

i = 3, need[3] <= work , (0 <= 0) => work = work + allocation[3] = 0 + 4 =


4, finish[3] = True
i = 0, need[0] <= work , (3 <= 4) => work = work + allocation[0] = 4 + 7 =
11, finish[0] = True
i = 1, need[1] <= work , (1 <= 11) => work = work + allocation[1] = 11 + 3 =
14, finish[1] = True
i = 2, need[2] <= work , (7 <= 14) => work = work + allocation[2] = 14 + 2 =
16, finish[2] = True
finish = [True, True, True, True] => system is safe

Problem 2
• 5 processes: P0 - P4; 3 resource types
◦ A (10 instances), B (5 instances), and C (7 instances)
• At time T0:
Allocation Max Available
A B C A B C A B C
P0 1 1 0 7 5 3 3 3 2
P1 1 0 0 3 2 2
P2 2 0 2 9 0 2
P3 2 1 1 2 2 2
P4 1 0 2 4 3 3
Is the system safe with banker’s algorithm?

available = [3,3,2]
need =[[6,4,3],
[2,2,2],
[7,0,0],
[0,1,1],
[3,3,1]]

work = available = [3,3,2], finish = [False, False, False, False, False]

i = 1, need[1] <= work, (2,2,2) <= (3,3,2) => work = work + allocation[1] =
(3,3,2) + (1,0,0) = (4,3,2), finish[1] = True
i = 3, need[3] <= work, (0,1,1) <= (4,3,2) => work = work + allocation[3] =
(4,3,2) + (2,1,1) = (6,4,3), finish[3] = True
i = 4, need[4] <= work, (3,3,1) <= (6,4,3) => work = work + allocation[4] =
(6,4,3) + (1,0,2) = (7,4,5), finish[4] = True
i = 0, need[0] <= work, (6,4,3) <= (7,4,5) => work = work + allocation[0] =
(7,4,5) + (1,1,0) = (8,5,5), finish[0] = True
i = 2, need[2] <= work, (7,0,0) <= (8,5,5) => work = work + allocation[2] =
(8,5,5) + (2,0,2) = (10,5,7), finish[2] = True
finish = [True, True, True, True, True] => system is safe

PROBLEM 3
• 5 processes: P0 - P4; 3 resource types
◦ A (10 instances), B (5 instances), and C (7 instances)
Allocation Max Available
A B C A B C A B C
P0 0 1 2 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 1 9 0 2
P3 2 1 1 2 2 2
P4 0 0 1 4 3 3
• If P1 requests for (1,0,2) => can it be granted

step 1:
request[1] < need[1], (1,0,2) < (1,2,2) => go to step 2:
step 2:
request[1] < available, (1,0,2) < (3,3,2) => go to step 3
step 3:
allocation[1] = allocation[1] + request[1] = (2,0,0) + (1,0,2) = (3,0,2)
available = available - request[1] = (3,3,2) - (1,0,2) = (2,3,0)
call BanhkerAlogithm
work = available = [2,3,0]
finish = [False, False, False, False, False]
need =[[6,4,1]
[0,2,0]
[6,0,1]
[0,1,1]
[4,3,2]]
i = 1 , need[1] <= work, (0,2,0) <= (2,3,0) => work = work +
allocation[1] = (2,3,0) + (3,0,2) = (5,3,2), finish[1] = True
i = 3, need[3] <= work, (0,1,1) <= (5,3,2) => work = work +
allocation[3] = (5,3,2) + (2,1,1) = (7,4,3), finish[3] = True
i = 4, need[4] <= work, (4,3,2) <= (7,4,3) => work = work +
allocation[4] = (7,4,3) + (0,0,1) = (7,4,4), finish[4] = True
i = 0, need[0] <= work, (6,4,1) <= (7,4,4) => work = work +
allocation[0] = (7,4,4) + (0,1,2) = (7,5,6), finish[0] = True
i = 2, need[2] <= work, (6,0,1) <= (7,5,6) => work = work +
allocation[2] = (7,5,6) + (3,0,1) = (10,5,7), finish[2] = True
finish = [True, True, True, True, True] => system is safe
=> P1's request is granted

• If P4 requests for (1,0,0) => can it be granted?


step 1:
request[4] < need[4], (1,0,0) < (4,3,2) => go to step 2
step 2:
request[4] < available, (1,0,0) < (3,3,2) => go to step 3
step 3:
allocation[4] = allocation[4] + request[4] = (0,0,1) + (1,0,0) = (1,0,1)
available = available - request[4] = (3,3,2) - (1,0,0) = (2,3,2)
call BanhkerAlogithm
work = available = [2,3,2]
finish = [False, False, False, False, False]
need =[[6,4,1]
[1,2,2]
[6,0,1]
[0,1,1]
[3,3,2]]
i = 1, need[1] <= work, (1,2,2) <= (2,3,2) => work = work +
allocation[1] = (2,3,2) + (2,0,0) = (4,3,2), finish[1] = True
i = 3, need[3] <= work, (0,1,1) <= (4,3,2) => work = work +
allocation[3] = (4,3,2) + (2,1,1) = (6,4,3), finish[3] = True
i = 4, need[4] <= work, (3,3,2) <= (6,4,3) => work = work +
allocation[4] = (6,4,3) + (1,0,1) = (7,4,4), finish[4] = True
i = 0, need[0] <= work, (6,4,1) <= (7,4,4) => work = work +
allocation[0] = (7,4,4) + (0,1,2) = (7,5,6), finish[0] = True
i = 2, need[2] <= work, (6,0,1) <= (7,5,6) => work = work +
allocation[2] = (7,5,6) + (3,0,1) = (10,5,7), finish[2] = True
finish = [True, True, True, True, True] => system is safe
=> P4's request is granted
• If P0 requests for (0, 2, 0) => can it be granted?
step 1:
request[0] < need[0], (0,2,0) < (6,4,1) => go to step 2
step 2:
request[0] < available, (0,2,0) < (3,3,2) => go to step 3
step 3:
allocation[0] = allocation[0] + request[0] = (0,1,2) + (0,2,0) = (0,3,2)
available = available - request[0] = (3,3,2) - (0,2,0) = (3,1,2)
call BanhkerAlogithm
work = available = [3,1,2]
finish = [False, False, False, False, False]
need =[[6,2,1]
[1,2,2]
[6,0,1]
[0,1,1]
[4,3,2]]
i = 3, need[3] <= work, (0,1,1) <= (3,1,2) => work = work +
allocation[3] = (3,1,2) + (2,1,1) = (5,2,3), finish[3] = True
i = 1, need[1] <= work, (1,2,2) <= (5,2,3) => work = work +
allocation[1] = (5,2,3) + (2,0,0) = (7,2,3), finish[1] = True
i = 0, need[0] <= work, (6,2,1) <= (7,2,3) => work = work +
allocation[0] = (7,2,3) + (0,3,2) = (7,5,5), finish[0] = True
i = 2, need[2] <= work, (6,0,1) <= (7,5,5) => work = work +
allocation[2] = (7,5,5) + (3,0,1) = (10,5,6), finish[2] = True
i = 4, need[4] <= work, (4,3,2) <= (10,5,6) => work = work +
allocation[4] = (10,5,6) + (0,0,1) = (10,5,7), finish[4] = True
finish = [True, True, True, True, True] => system is safe
=> P0's request is granted

• If P1 requests for (1,0,2) => can it be granted


◦ If P1’s request is granted, can the next request for (1,0,0) by P4 be
granted?
step 1:
request[4] < need[4], (1,0,0) < (4,3,2) => go to step 2
step 2:
request[4] < available, (1,0,0) < (2,3,0) => go to step 3
step 3:
allocation[4] = allocation[4] + request[4] = (0,0,1) + (1,0,0) = (1,0,1)
available = available - request[4] = (2,3,0) - (1,0,0) = (1,3,0)
call BanhkerAlogithm
work = available = [1,3,0]
finish = [False, False, False, False, False]
need =[[6,4,1]
[0,2,0]
[6,0,1]
[0,1,1]
[3,3,2]]
i = 1, need[1] <= work, (0,2,0) <= (1,3,0) => work = work +
allocation[1] = (1,3,0) + (2,0,0) = (3,3,0), finish[1] = True
not found i satisfy the condition => system is unsafe
=> P4's request is not granted

• If P1 requests for (1,0,2) => can it be granted


◦ If P1’s request is granted, can the request for (0,2,0) by P0 be
granted?
step 1:
request[0] < need[0], (0,2,0) < (6,4,1) => go to step 2
step 2:
request[0] < available, (0,2,0) < (1,3,0) => go to step 3
step 3:
allocation[0] = allocation[0] + request[0] = (0,1,2) + (0,2,0) = (0,3,2)
available = available - request[0] = (2,3,0) - (0,2,0) = (2,1,0)
call BanhkerAlogithm
work = available = [1,1,0]
finish = [False, False, False, False, False]
need =[[6,2,1]
[0,2,0]
[6,0,1]
[0,1,1]
[4,3,2]]
not found i satisfy the condition => system is unsafe
=> P0's request is not granted

You might also like