• 沒有找到結果。

condition required for the first process to make progress. In a

CPU Scheduling

Exercises 43 condition required for the first process to make progress. In a

multipro-cessor system, other processes execute on other promultipro-cessors and thereby modify the program state in order to release the first process from the spinlock.

6.24 In log-based systems that provide support for transactions, updates to data items cannot be performed before the corresponding entries are logged. Why is this restriction necessary?

Answer: If the transaction needs to be aborted, then the values of the updated data values need to be rolled back to the old values. This requires the old values of the data entries to be logged before the updates are performed.

6.25 Show that the two-phase locking protocol ensures conflict serializability.

Answer: A schedule refers to the execution sequence of the operations for one or more transactions. A serial schedule is the situation where each transaction of a schedule is performed atomically. If a schedule consists of two different transactions where consecutive operations from the different transactions access the same data and at least one of the operations is a write, then we have what is known as a conflict. If a schedule can be transformed into a serial schedule by a series of swaps on nonconflicting operations, we say that such a schedule is conflict serializable.

The two-phase locking protocol ensures conflict serializabilty because exclusive locks (which are used for write operations) must be acquired serially, without releasing any locks during the acquire (growing) phase.

Other transactions that wish to acquire the same locks must wait for the first transaction to begin releasing locks. By requiring that all locks must first be acquired before releasing any locks, we are ensuring that potential conflicts are avoided.

6.26 What are the implications of assigning a new timestamp to a transaction that is rolled back? How does the system process transactions that were issued after the rolled-back transaction but that have timestamps smaller than the new timestamp of the rolled-back transaction?

Answer: If the transactions that were issued after the rolled-back action had accessed variables that were updated by the rolled-back trans-action, then these transactions would have to rolled-back as well. If they have not performed such operations (that is, there is no overlap with the rolled-back transaction in terms of the variables accessed), then these operations are free to commit when appropriate.

6.27 Assume that a finite number of resources of a single resource type must be managed. Processes may ask for a number of these resources and

—once finished—will return them. As an example, many commercial software packages provide a given number of licenses, indicating the number of applications that may run concurrently. When the application is started, the license count is decremented. When the application is terminated, the license count is incremented. If all licenses are in use, requests to start the application are denied. Such requests will only be granted when an existing license holder terminates the application and a license is returned.

The following program segment is used to manage a finite number of instances of an available resource. The maximum number of resources and the number of available resources are declared as follows:

#define MAX RESOURCES 5

int available resources = MAX RESOURCES;

When a process wishes to obtain a number of resources, it invokes the decrease count()function:

/* decrease available resources by count resources */

/* return 0 if sufficient resources available, */

/* otherwise return -1 */

int decrease count(int count) { if (available resources < count)

return -1;

else {

available resources -= count;

return 0;

} }

When a process wants to return a number of resources, it calls the de-crease count()function:

/* increase available resources by count */

int increase count(int count) { available resources += count;

return 0;

}

The preceding program segment produces a race condition. Do the fol-lowing:

a. Identify the data involved in the race condition.

b. Identify the location (or locations) in the code where the race condition occurs.

c. Using a semaphore, fix the race condition.

Answer:

• Identify the data involved in the race condition: The variable available resources.

• Identify the location (or locations) in the code where the race condition occurs: The code that decrements available resources and the code that increments available resources are the statements that could be involved in race conditions.

Exercises 45

• Using a semaphore, fix the race condition: Use a semaphore to represent the available resources variable and replace increment and decrement operations by semaphore increment and semaphore decrement operations.

6.28 Thedecrease count()function in the previous exercise currently re-turns 0 if sufficient resources are available and -1 otherwise. This leads to awkward programming for a process that wishes obtain a number of resources:

while (decrease count(count) == -1)

;

Rewrite the resource-manager code segment using a monitor and con-dition variables so that thedecrease count() function suspends the process until sufficient resources are available. This will allow a process to invokedecrease count()by simply calling

decrease count(count);

The process will only return from this function call when sufficient resources are available.

Answer:

monitor resources {

int available_resources;

condition resources_avail;

int decrease_count(int count) {

while (available_resources < count) resources_avail.wait();

available_resources -= count;

}

int increase_count(int count) {

available_resources += count;

resources_avail.signal();

}

7

C H A P T E R

Deadlocks

Deadlock is a problem that can only arise in a system with multiple active asynchronous processes. It is important that the students learn the three basic approaches to deadlock: prevention, avoidance, and detection (although the terms prevention and avoidance are easy to confuse).

It can be useful to pose a deadlock problem in human terms and ask why human systems never deadlock. Can the students transfer this understanding of human systems to computer systems?

Projects can involve simulation: create a list of jobs consisting of requests and releases of resources (single type or multiple types). Ask the students to al-locate the resources to prevent deadlock. This basically involves programming the Banker’s Algorithm.

The survey paper by Coffman, Elphick, and Shoshani [1971] is good sup-plemental reading, but you might also consider having the students go back to the papers by Havender [1968], Habermann [1969], and Holt [1971a]. The last two were published in CACM and so should be readily available.

Exercises

7.1 Consider the traffic deadlock depicted in Figure 7.1.

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 exclu-sion; (2) hold-and-wait; (3) no preemption; and (4) circular wait.

The mutual exclusion condition holds as only one car can occupy a space in the roadway. Hold-and-wait occurs where a car holds onto their place in the roadway while they wait to advance in 47

Figure 7.1 Traffic deadlock for Exercise 7.1.

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 they will not be able to immediately clear the intersection.

7.2 Consider the deadlock situation that could occur in the dining-philosophers problem when the philosophers obtain the chopsticks one at a time. Dis-cuss how the four necessary conditions for deadlock indeed hold in this setting. Discuss how deadlocks could be avoided by eliminating any one of the four conditions.

Answer: Deadlock is possible because the four necessary conditions hold in the following manner: 1) mutual exclusion is required for chop-sticks, 2) the philosophers tend to hold onto the chopstick in hand while they wait for the other chopstick, 3) there is no preemption of chopsticks in the sense that a chopstick allocated to a philosopher cannot be forcibly taken away, and 4) there is a possibility of circular wait. Deadlocks could be avoided by overcoming the conditions in the following manner: 1) allow simultaneous sharing of chopsticks, 2) have the philosophers relin-quish the first chopstick if they are unable to obtain the other chopstick, 3) allow for chopsticks to be forcibly taken away if a philosopher has had a chopstick for a long period of time, and 4) enforce a numbering of the chopsticks and always obtain the lower numbered chopstick before obtaining the higher numbered one.

Exercises 49