• 沒有找到結果。

Process Synchronization

在文檔中 OPERATING SYSTEM CONCEPTS (頁 31-35)

PROCESS

SYNCHRONIZATION

Chapter 7 is concerned with the topic of process synchronization among concurrently executing processes. Concurrency is generally very hard for students to deal with correctly, and so we have tried to introduce it and its problems with the classic process coordination problems: mutual exclusion, bounded-buffer, readers/writers, and so on. An understanding of these problems and their solutions is part of current operating-system theory and development.

We first use semaphores and monitors to introduce synchronization techniques. Next, Java synchronization is introduced to further demonstrate a language-based synchronization tech-nique.

Answers to Exercises

7.1 What is the meaning of the term busy waiting? What other kinds of waiting are there in an operating system? Can busy waiting be avoided altogether? Explain your answer.

Answer: No answer.

7.2 Explain why spinlocks are not appropriate for uniprocessor systems yet may be suitable for multiprocessor systems.

Answer: No answer.

7.3 Prove that, in the bakery algorithm (Section 7.2), the following property holds: If Piis in its critical section and Pk(k6=i) has already chosen itsnumber[k] 6= 0, then (number[i], i) < (number[k], k).

Answer: No answer.

7.4 The first known correct software solution to the critical-section problem for two threads was developed by Dekker; it is shown in Figure 7.27. The two threads, T0 and T1, coordinate activity sharing an object of class Dekker.

Show that the algorithm satisfies all three requirements for the critical-section problem.

Answer: No answer.

25

26 Chapter 7 Process Synchronization

7.5 The first known correct software solution to the critical-section problem for n processes with a lower bound on waiting of n ; 1 turns was presented by Eisenberg and McGuire. The processes share the following variables:

enum pstate fidle, want in, in csg; pstate flag[n];

int turn;

All the elements of flag are initially idle; the initial value of turn is immaterial (between 0 and n-1). The structure of process Pi is shown in Fig-ure 7.28.

Prove that the algorithm satisfies all three requirements for the critical-section problem.

Answer: No answer.

7.6 In Section 7.3, we mentioned that disabling interrupts frequently can af-fect the system's clock. Explain why it can, and how such efaf-fects can be minimized.

Answer: No answer.

7.7 Show that, if the wait and signal operations are not executed atomically, then mutual exclusion may be violated.

Answer: No answer.

7.8 The Sleeping-Barber Problem. A barbershop consists of a waiting room with n chairs and the barber room containing the barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop.

If the barber is busy but chairs are available, then the customer sits in one of the free chairs. If the barber is asleep, the customer wakes up the barber. Write a program to coordinate the barber and the customers.

Answer: Please refer to the supporting Web site for source code solution.

7.9 The Cigarette-Smokers Problem. Consider a system with three smoker processes and one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and smoke a cigarette, the smoker needs three in-gredients: tobacco, paper, and matches. One of the smoker processes has paper, another has tobacco, and the third has matches. The agent has an infinite supply of all three materials. The agent places two of the in-gredients on the table. The smoker who has the remaining ingredient then makes and smokes a cigarette, signaling the agent on completion. The agent then puts out another two of the three ingredients, and the cycle repeats.

Write a program to synchronize the agent and the smokers.

Answer: Please refer to the supporting Web site for source code solution.

7.10 Demonstrate that monitors, conditional critical regions, and semaphores are all equivalent, insofar as the same types of synchronization problems can be implemented with them.

Answer: No answer.

7.11 Write a bounded-buffer monitor in which the buffers (portions) are embed-ded within the monitor itself.

Answer: No answer.

Answers to Exercises 27

7.12 The strict mutual exclusion within a monitor makes the bounded-buffer mon-itor of Exercise 7.11 mainly suitable for small portions.

a. Explain why this assertion is true.

b. Design a new scheme that is suitable for larger portions.

Answer: No answer.

7.13 Suppose that the signal statement can appear as only the last statement in a monitor procedure. Suggest how the implementation described in Section 7.7 can be simplified.

Answer: No answer.

7.14 Consider a system consisting of processes P1, P2, ..., Pn, each of which has a unique priority number. Write a monitor that allocates three identical line printers to these processes, using the priority numbers for deciding the order of allocation.

Answer: No answer.

7.15 A file is to be shared among different processes, each of which has a unique number. The file can be accessed simultaneously by several processes, sub-ject to the following constraint: The sum of all unique numbers associ-ated with all the processes currently accessing the file must be less than n. Write a monitor to coordinate access to the file.

Answer: No answer.

7.16 Suppose that we replace the wait and signal operations of monitors with a single construct await(B), where B is a general Boolean expression that causes the process executing it to wait until B becomes true.

a. Write a monitor using this scheme to implement the readers--writers prob-lem.

b. Explain why, in general, this construct cannot be implemented efficiently.

c. What restrictions need to be put on the await statement so that it can be implemented efficiently? (Hint: Restrict the generality of B; see kessels [1977].)

Answer: No answer.

7.17 Write a monitor that implements an alarm clock that enables a calling program to delay itself for a specified number of time units (ticks). You may as-sume the existence of a real hardware clock that invokes a procedure tick in your monitor at regular intervals.

Answer: No answer.

7.18 Why does Solaris 2 implement multiple locking mechanisms? Under what cir-cumstances does it use spinlocks, semaphores, adaptive mutexes, conditional variables, and readers--writers locks? Why does it use each mechanism? What is the purpose of turnstiles?

Answer: Solaris 2 provides different locking mechanisms depending on the application developer's needs. Spinlocks are useful for multiprocessor sys-tems where a thread can run in a busy-loop (for a short period of time) rather

28 Chapter 7 Process Synchronization

than incurring the overhead of being put in a sleep queue. Mutexes are use-ful for locking resources. Solaris 2 uses adaptive mutexes, meaning that

the mutex is implemented with a spin lock on multiprocessor machines. Semaphores and condition variables are more appropriate tools for synchronization when a resource must be held for a long period of time for spinning is ineffi-cient for a long duration. Readers/writers locks are useful when readers and writers both need access to a resource, but the readers are more ac-tive and performance can be gained not using exclusive access locks. So-laris 2 uses turnstiles to order the list of threads waiting to acquire ei-ther an adaptive mutex or a reader--writer lock.

7.19 Why do Solaris 2 and Windows 2000 use spinlocks as a synchronization mech-anism on only multiprocessor systems and not on uniprocessor systems?

Answer: No answer.

7.20 Explain the differences, in terms of cost, among the three storage types:

volatile, nonvolatile, and stable.

Answer: No answer.

7.21 Explain the purpose of the checkpoint mechanism. How often should check-points be performed? How does the frequency of checkcheck-points affect:

 System performance when no failure occurs?

 The time it takes to recover from a system crash?

 The time it takes to recover from a disk crash?

Answer: No answer.

7.22 Explain the concept of transaction atomicity.

Answer: No answer.

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

Answer: No answer.

7.24 Show that some schedules are possible under the two-phase locking proto-col but not possible under the timestamp protoproto-col, and vice versa.

Answer: No answer.

在文檔中 OPERATING SYSTEM CONCEPTS (頁 31-35)