Sunteți pe pagina 1din 4

King Saud University

College of Computer and Information Sciences


Information Technology Department
First Semester 1432/1433
IT 421

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).

Snapshot at time T0:


Max
P0
P1
P2
P3
P4

a)

A
6
2
2
2
4

B
7
2
6
2
6

Calculate the available vector.


Available
A B C
1 2 0

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

Calculate the Need matrix.

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

Work = Work + Allocationi


(1, 2, 0) + (1, 1, 0) = (2, 3, 0)

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

b) Write all the cycle(s) in the graph.


P3->R1->P1->R2(1st instance)->P3
P3->R1->P1->R2(2nd instance)->P3
c) Does a deadlock exist? Why?
Yes, a deadlock does exist.
P3 is waiting for an instance of R1, the only instance of which is held by P1 and P1 is
waiting for an instance of R2, both instances of which are held by P3. Since neither an
instance of R1 nor that of R2 will ever become available, hence, neither P1 nor P3 will
ever be able to continue executing. Therefore, a deadlock exists.
Q3) A computer has three commonly used resources designated A, B and C. Up to three
processes designated X, Y and Z run on the computer and each makes periodic use of two
of the three resources.

Process X acquires A, then B, uses both and then releases both.


Process Y acquires B, then C, uses both and then releases both.
Process Z acquires C, then A, uses both and then releases both.

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:

X acquires A and requests B.


Y acquires B and requests C.
Z acquires C and requests A.

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.

S-ar putea să vă placă și