You are on page 1of 5

Advanced Operating System (HW3)

Department : IITA Student ID : BIA110004 Name : 哈瓦尼

Chapter 7
7.5 Prove that the safety algorithm presented in Section 7.5.3 requires an order of m × n2
operations.context is in memory rather than in a register set and all the register sets are in
use?
Answer:
Below this Java code that implement the safety algorithm of the banker’s algorithm
for (int i = 0; i < n; i++) {

// first find a thread that can finish

for (int j = 0; j < n; j++) {

if (!finish[j]) {

boolean temp = true;

for (int k = 0; k < m; k++) {

if (need[j][k] > work[k]) temp = false;

if (temp) { // if this thread can

finish finish[j] = true;

for (int x = 0; x < m; x++)

work[x] += work[j][x];

As can be seen, the nested outer loops—both of which loop through n times—provide the
n2 performance. Within these outer loops are two sequential inner loops which loop m
times. The big-oh of this algorithm is therefore O(m × n2).
7.6 Consider a computer system that runs 5,000 jobs per month and has no deadlock-
prevention or deadlock-avoidance scheme. Deadlocks occur about twice per month, and the
operator must terminate and rerun about ten jobs per deadlock. Each job is worth about
two dollars (in CPU time), and the jobs terminated tend to be about half done when they
are aborted.
A systems programmer has estimated that a deadlock-avoidance algorithm (like the
banker’s algorithm) could be installed in the system with an increase of about 10 percent in
the average execution time per job. Since the machine currently has 30 percent idle time, all
5,000 jobs per month could still be run, although
a. What are the arguments for installing the deadlock-avoidance algorithm?
b. What are the arguments against installing the deadlock-avoidance algorithm?
Answer:
a. An argument for installing deadlock avoidance in the system is that we could ensure
deadlock would never occur. In addition, despite the increase in turnaround time, all
5,000 jobs could still run.
b. An argument against installing deadlock avoidance software is that deadlocks occur
infrequently and they cost little when they do occur.

7.8 Consider the following resource-allocation policy. Requests for and releases of resources
are allowed at any time. If a request for resources cannot be satisfied because the resources
are not available, then we check any processes that are blocked waiting for resources. If a
blocked process has the desired resources, then these resources are taken away from it and
are given to the requesting process. The vector of resources for which the blocked process is
waiting is increased to include the resources that were taken away.
For example, a system has three resource types, and the vector Available is initialized to
(4,2,2). If process P0 asks for (2,2,1), it gets them. If P1 asks for (1,0,1), it gets them. Then, if
P0 asks for (0,0,1), it is blocked (resource not available). If P2 now asks for (2,0,0), it gets the
available one (1,0,0), as well as one that was allocated to P0 (since P0 is blocked). P0’s
Allocation vector goes down to (1,2,1), and its Need vector goes up to (1,0,1).
a. Can deadlock occur? If you answer “yes,” give an example. If you answer “no,” specify
which necessary condition cannot occur.
b. Can indefinite blocking occur? Explain your answer.
Answer:
a. Deadlock cannot occur because preemption exists.
b. Yes. A process may never acquire all the resources it needs if they are continuously
preempted by a series of requests such as those of process C
7.22 Consider the following snapshot of a system:
Allocation Max
ABCD ABCD
P0 3014 5117
P1 2210 3211
P2 3121 3321
P3 0510 4612
P4 4212 6325
Using the banker’s algorithm, determine whether or not each of the following states is
unsafe. If the state is safe, illustrate the order in which the processes may complete.
Otherwise, illustrate why the state is unsafe.
a. Available = (0, 3, 0, 1)
b. Available = (1, 0, 0, 2)
Answer:
a. Unsafe.
Analysis guidelines: Given the Allocation and Max columns, we can first derive the
Request column by Allocation - Max:

Allocation Max Request


ABCD ABCD ABCD
P0 3014 5117 2103
P1 2210 3211 1001
P2 3121 3321 0200
P3 0510 4512 4002
P4 4212 6325 2113

The Request column specifies how many more resources each process needs to
complete.
Following the banker's algorithm, I compare Request with Available to decide
whether the available resources are sufficient for a process to execute.
That is, if Request is no greater than Available, the corresponding process can be
executed. Upon completion, the resources allocated to the process should be
reclaimed and added to Available. With the incremented Available, the banker's
algorithm iterates the comparison of Request and Available for subsequent
processes. If Request is less than Available, the corresponding process cannot be
executed.

b. Safe.
More than one safe sequence may be feasible, being omitted here. Deriving a safe
sequence still follows the preceding analysis guidelines.

7.23 Consider the following snapshot of a system:


Allocation Max Available
ABCD ABCD ABCD
P0 2001 4212 3321
P1 3121 5252
P2 2103 2316
P3 1312 1424
P4 1432 3665
Answer the following questions using the banker’s algorithm:
a. Illustrate that the system is in a safe state by demonstrating an order in which the
processes may complete.
b. If a request from process P1 arrives for (1, 1, 0, 0), can the request be granted
immediately?
c. If a request from process P4 arrives for (0, 0, 2, 0), can the request be granted
immediately?
Answer:
a.

Need Matrix Max – Allocation


Max Allocation Need Available
P0 4212 2001 2211 3321
P1 5252 3121 2131
P2 2316 2103 0213
P3 1424 1312 0112
P4 3665 1432 2233

P0 can be complete with the available

Complete Available
P0 5322
P3 6655
P1 9755
P2 11 8 5 8
P4 12 12 8 10
Safe state with the order PO – P3 – P1 – P2 – P4
P0 – P3 P1 – P4 – P2 P0 – P3 – P4 – P1 – P2
P2 – P1 – P4 P0 – P3 – P4 – P2 – P1
P2 – P4 – P1
The given snapshot of the system is in safe state. With the available resources the
processes will complete their execution with the following sequences: P0-P3- (any
combination of P1, P2 and P4).

b. Request from P1 process (1 1 0 0)


Available = 2 2 2 1

Allocation Max Need


Process
ABCD ABCD ABCD
P0 2001 4212 2211
P1 4221 5252 1031
P2 2103 2316 0213
P3 1312 1424 0112
P4 1432 3665 2233

Allocation to P0 and complete = 4 2 2 2


Complete P3 = 1 3 1 2 + 4 2 2 2 = 5 5 3 4
Complete P1 = 5 5 3 4 + 4 2 2 1 = 9 7 5 5
Complete P2 = 9 7 5 5 + 2 1 0 3 = 11 8 5 8
Complete P4 = 11 8 5 8 + 1 4 3 2 = 12 12 8 10

Yes, the request (1 1 0 0) may be granted to P1, and the system will remain in safe
state. With the available resources the processes will complete their execution with
the following sequences: P0-P3-(any combination of P1, P2 and P4)
c. No, the above said request (0 0 2 0) cannot be granted to P4. In case, if you grant the
resources the system will go to un-safe state and all the processes will be in
deadlock.

Proces
Allocation Max Need Available
s
P0 2001 4212 2211 3301
P1 3121 5252 2131
P2 2103 2316 0213
P3 1312 1424 0112
P4 1432 3665 2233

You might also like