Uploaded by 林琨霖

Operating System Deadlock homework

advertisement
Homework #7
7.1 Consider the traffic deadlock depicted in Figure 7.10.
a. Show that the four necessary conditions for deadlock indeed hold in this example.
b. State a simple rule for avoiding deadlocks in this system.
Answer:
a. The four necessary conditions for a deadlock are (1) mutual exclusion; (2) holdand-wait; (3) no preemption; and (4) circular wait. The mutual exclusion condition
holds since only one car can occupy a space in the roadway. Hold-and-wait occurs
where a car holds onto its place in the roadway while it waits to advance in the
roadway. A car cannot be removed (i.e. preempted) from its position in the roadway.
Lastly, there is indeed a circular wait as each car is waiting for a subsequent car to
advance. The circular wait condition is also easily observed from the graphic.
b. A simple rule that would avoid this traffic deadlock is that a car may not advance
into an intersection if it is clear it will not be able immediately to clear the
intersection.
7.4 In Section 7.4.4, we describe a situation in which we prevent deadlock by
ensuring that all locks are acquired in a certain order. However, we also point out
that deadlock is possible in this situation if two threads simultaneously invoke the
transaction() function. Fix the transaction() function to prevent deadlocks.
Answer:
Add a new lock to this function. This third lock must be acquired before the two locks
associated with the
accounts are acquired. The transaction() function now appears as follows:
void transaction(Account from, Account to, double amount)
{
Semaphore lock1, lock2, lock3;
wait(lock3);
lock1 = getLock(from);
lock2 = getLock(to);
wait(lock1);
wait(lock2);
withdraw(from, amount);
deposit(to, amount);
signal(lock3);
signal(lock2);
signal(lock1);
}
7.6 In a real computer system, neither the resources available nor the demands of
processes for resources are consistent over long periods (months). Resources break
or are replaced, new processes come and go, new resources are bought and added
to the system. If deadlock is controlled by the banker’s algorithm, which of the
following changes can be made safely (without introducing the possibility of
deadlock), and under what circumstances?
a. Increase Available (new resources added)
b. Decrease Available (resource permanently removed from
system)
c. Increase Max for one process (the process needs or wants
more resources than allowed).
d. Decrease Max for one process (the process decides it does
not need that many resources)
e. Increase the number of processes
f. Decrease the number of processes
Answer:
a. Increase Available (new resources added)—This could safely
be changed without any problems.
b. Decrease Available (resource permanently removed from
system)—This could have an effect on the
system and introduce the possibility of deadlock as the safety
of the system assumed there were a
certain number of available resources.
c. Increase Max for one process (the process needs more
resources than allowed, it may want more)—
This could have an effect on the system and introduce the
possibility of deadlock.
d. Decrease Max for one process (the process decides it does
not need that many resources)—This
could safely be changed without any problems.
e. Increase the number of processes—This could be allowed
assuming that resources were allocated to the new process(es)
such that the system does not enter an unsafe state.
f. Decrease the number of processes—This could safely be
changed without any problems.
Download