Documente Academic
Documente Profesional
Documente Cultură
Assignment #7
Model Answer
Homework policy:
1) Copying any part of other people's code, solution sets, or from any other sources is
strictly prohibited.
2) If your source is the web, write your references in details.
3) Solutions quality (understandable and organized document, not handwriting).
Q1) Given the following state for the Bankers Algorithm.
5 processes P0 through P4
3 resource types A (6 instances), B (9 instances) and C (5 instances).
a)
A
6
2
2
2
4
B
7
2
6
2
6
C
3
2
3
2
3
Allocation
A
1
1
0
2
1
B
1
1
3
1
1
C
1
2
0
1
1
b)
Need
A B C
P0
5 6 2
P1
1 1 0
P2
2 3 3
P3
0 1 1
P4
3 5 2
c)
Is the system in a safe state? If so, show one sequence of processes which allows
the system to complete. If not, explain why. Show your computation step-by-step;
otherwise, you will receive no credit.
1. Initialize the Work and Finish vectors.
Work = Available = (1, 2, 0)
Finish = (false, false, false, false, false)
2. Find index i such that Finish[i] = false and Needi <= Work
i
1
Finish
(false, true, false, false, false)
3. At this point, there does not exist an index i such that Finish[i] = false and Needi
<= Work.
Since Finish[i] true for all i, hence the system is not in a safe state.
d)
Given the request (1, 2, 0) from Process P2. Should this request be granted? Why
or why not? Show your computation step-by-step; otherwise, you will receive no
credit.
1. Check that Request2 <= Need2.
Since (1, 2, 0) <= (2, 3, 3), hence, this condition is satisfied.
2. Check that Request2 <= Available.
Since (1, 2, 0) <= (1, 2, 0), hence, this condition is satisfied.
3. Modify the systems state as follows:
Available = Available Request2 = (1, 2, 0) (1, 2, 0) = (0, 0, 0)
Allocation2 = Allocation2 + Request2 = (0, 3, 0) + (1, 2, 0) = (1, 5, 0)
Need2 = Need2 Request2 = (2, 3, 3) (1, 2, 0) = (1, 1, 3)
4. Apply the safety algorithm to check if granting this request leaves the system in a
safe state.
1. Initialize the Work and Finish vectors.
Work = Available = (0, 0, 0)
Finish = (false, false, false, false, false)
2. At this point, there does not exist an index i such that Finish[i] = false and
Needi <= Work.
Since Finish[i] true for all i, hence the system is not in a safe state.
Therefore, this request from process P2 should not be granted.
Q2) A system has three processes (P1, P2, P3) and three reusable resources (R1, R2, R3).
There is one instance of R1, two instances of R2 and three instances of R3. P1 holds an R1
and an R3 and is requesting an R2. P2 holds an R3 and is requesting an R1 and an R2. P3
holds two R2 and an R3 and is requesting an R1.
a) Draw the resource allocation graph for this situation.
P2
R1
P1
P3
R2
R3
a) If two of these processes are running simultaneously on the machine, can a deadlock
occur? If so, describe the deadlock scenario.
No deadlock is possible since one of the two processes will always be able to run to
completion.
b) Describe a scenario in which deadlock occurs if all three processes are running
simultaneously on the machine.
All three processes make their first acquisition then hang waiting for a resource that will
never become available.
As a specific example, a deadlock will occur if the following sequence of events occurs:
c)Modify the algorithm for acquiring resources so that deadlock cannot occur with three
processes running.
If we change Z to acquire A then C, no deadlock can occur.