现代操作系统 习题答案.docx
MODERNOPERATINGSYSTEMSSECOND EDITIONPROBLEM SOLUTIONSANDREW S. TANENBAUMVrije UniversiteitAmsterdam, The NetherlandsPRENTICE HALLUPPER SADDLE RIVER, NJ 07458SOLUTIONS TO CHAPTER 1 PROBLEMS1. An operating system must provide the users with an extended (i.e., virtual)machine, and it must manage the I/O devices and other system resources.2. Multiprogramming is the rapid switching of the CPU between multipleprocesses in memory. It is commonly used to keep the CPU busy while oneor more processes are doing I/O.3. Input spooling is the technique of reading in jobs, for example, from cards,onto the disk, so that when the currently executing processes are finished,there will be work waiting for the CPU. Output spooling consists of firstcopying printable files to disk before printing them, rather than printingdirectly as the output is generated. Input spooling on a personal computer isnot very likely, but output spooling is.4. The prime reason for multiprogramming is to give the CPU something to dowhile waiting for I/O to complete. If there is no DMA, the CPU is fully occupieddoing I/O, so there is nothing to be gained (at least in terms of CPU utilization)by multiprogramming. No matter how much I/O a program does, theCPU will be 100 percent busy. This of course assumes the major delay is thewait while data are copied. A CPU could do other work if the I/O were slowfor other reasons (arriving on a serial line, for instance).5. Second generation computers did not have the necessary hardware to protectthe operating system from malicious user programs.6. It is still alive. For example, Intel makes Pentium I, II, and III, and 4 CPUswith a variety of different properties including speed and power consumption.All of these machines are architecturally compatible. They differ only inprice and performance, which is the essence of the family idea.7. A 25 × 80 character monochrome text screen requires a 2000-byte buffer. The1024 × 768 pixel 24-bit color bitmap requires 2,359,296 bytes. In 1980 thesetwo options would have cost $10 and $11,520, respectively. For currentprices, check on how much RAM currently costs, probably less than $1/MB.8. Choices (a), (c), and (d) should be restricted to kernel mode.9. Personal computer systems are always interactive, often with only a singleuser. Mainframe systems nearly always emphasize batch or timesharing withmany users. Protection is much more of an issue on mainframe systems, as isefficient use of all resources.10. Every nanosecond one instruction emerges from the pipeline. This means themachine is executing 1 billion instructions per second. It does not matter atall how many stages the pipeline has. A 10-stage pipeline with 1 nsec per2 PROBLEM SOLUTIONS FOR CHAPTER 1stage would also execute 1 billion instructions per second. All that matters ishow often a finished instructions pops out the end of the pipeline.11. The manuscript contains 80 × 50 × 700 = 2.8 million characters. This is, ofcourse, impossible to fit into the registers of any currently available CPU andis too big for a 1-MB cache, but if such hardware were available, themanuscript could be scanned in 2.8 msec from the registers or 5.8 msec fromthe cache. There are approximately 2700 1024-byte blocks of data, so scanningfrom the disk would require about 27 seconds, and from tape 2 minutes 7seconds. Of course, these times are just to read the data. Processing andrewriting the data would increase the time.12. Logically, it does not matter if the limit register uses a virtual address or aphysical address. However, the performance of the former is better. If virtualaddresses are used, the addition of the virtual address and the base registercan start simultaneously with the comparison and then can run in parallel. Ifphysical addresses are used, the comparison cannot start until the addition iscomplete, increasing the access time.13. Maybe. If the caller gets control back and immediately overwrites the data,when the write finally occurs, the wrong data will be written. However, if thedriver first copies the data to a private buffer before returning, then the callercan be allowed to continue immediately. Another possibility is to allow thecaller to continue and give it a signal when the buffer may be reused, but thisis tricky and error prone.14. A trap is caused by the program and is synchronous with it. If the program isrun again and again, the trap will always occur at exactly the same position inthe instruction stream. An interrupt is caused by an external event and itstiming is not reproducible.15. Base = 40,000 and limit = 10,000. An answer of limit = 50,000 is incorrectfor the way the system was described in this book. It could have been implementedthat way, but doing so would have required waiting until the address+ base calculation was completed before starting the limit check, thus slowingdown the computer.16. The process table is needed to store the state of a process that is currentlysuspended, either ready or blocked. It is not needed in a single process systembecause the single process is never suspended.17. Mounting a file system makes any files already in the mount point directoryinaccessible, so mount points are normally empty. However, a systemadministrator might want to copy some of the most important files normallylocated in the mounted directory to the mount point so they could be found intheir normal path in an emergency when the mounted device was beingchecked or repaired.PROBLEM SOLUTIONS FOR CHAPTER 1 318. Fork can fail if there are no free slots left in the process table (and possibly ifthere is no memory or swap space left). Exec can fail if the file name givendoes not exist or is not a valid executable file. Unlink can fail if the file to beunlinked does not exist or the calling process does not have the authority tounlink it.19. If the call fails, for example because fd is incorrect, it can return 1. It canalso fail because the disk is full and it is not possible to write the number ofbytes requested. On a correct termination, it always returns nbytes.20. It contains the bytes: 1, 5, 9, 2.21. Block special files consist of numbered blocks, each of which can be read orwritten independently of all the other ones. It is possible to seek to any blockand start reading or writing. This is not possible with character special files.22. System calls do not really have names, other than in a documentation sense.When the library procedure read traps to the kernel, it puts the number of thesystem call in a register or on the stack. This number is used to index into atable. There is really no name used anywhere. On the other hand, the nameof the library procedure is very important, since that is what appears in theprogram.23. Yes it can, especially if the kernel is a message-passing system.24. As far as program logic is concerned it does not matter whether a call to alibrary procedure results in a system call. But if performance is an issue, if atask can be accomplished without a system call the program will run faster.Every system call involves overhead time in switching from the user contextto the kernel context. Furthermore, on a multiuser system the operating systemmay schedule another process to run when a system call completes,further slowing the progress in real time of a calling process.25. Several UNIX calls have no counterpart in the Win32 API:Link: a Win32 program cannot refer to a file by an alternate name or see it inmore than one directory. Also, attempting to create a link is a convenient wayto test for and create a lock on a file.Mount and umount: a Windows program cannot make assumptions aboutstandard path names because on systems with multiple disk drives the drivename part of the path may be different.Chmod: Windows programmers have to assume that every user can accessevery file.Kill: Windows programmers cannot kill a misbehaving program that is notcooperating.4 PROBLEM SOLUTIONS FOR CHAPTER 126. The conversions are straightforward:(a) A micro year is 106 × 365 × 24 × 3600 = 31.536 sec.(b) 1000 meters or 1 km.(c) There are 240 bytes, which is 1,099,511,627,776 bytes.(d) It is 6 × 1024 kg.SOLUTIONS TO CHAPTER 2 PROBLEMS1. The transition from blocked to running is conceivable. Suppose that a processis blocked on I/O and the I/O finishes. If the CPU is otherwise idle, theprocess could go directly from blocked to running. The other missing transition,from ready to blocked, is impossible. A ready process cannot do I/O oranything else that might block it. Only a running process can block.2. You could have a register containing a pointer to the current process tableentry. When I/O completed, the CPU would store the current machine statein the current process table entry. Then it would go to the interrupt vector forthe interrupting device and fetch a pointer to another process table entry (theservice procedure). This process would then be started up.3. Generally, high-level languages do not allow one the kind of access to CPUhardware that is required. For instance, an interrupt handler may be requiredto enable and disable the interrupt servicing a particular device, or to manipulatedata within a process stack area. Also, interrupt service routines mustexecute as rapidly as possible.4. There are several reasons for using a separate stack for the kernel. Two ofthem are as follows. First, you do not want the operating system to crashbecause a poorly written user program does not allow for enough stack space.Second, if the kernel leaves stack data in a user programs memory spaceupon return from a system call, a malicious user might be able to use this datato find out information about other processes.5. It would be difficult, if not impossible, to keep the file system consistent.Suppose that a client process sends a request to server process 1 to update afile. This process updates the cache entry in its memory. Shortly thereafter,another client process sends a request to server 2 to read that file. Unfortunately,if the file is also cached there, server 2, in its innocence, will returnobsolete data. If the first process writes the file through to the disk after cachingit, and server 2 checks the disk on every read to see if its cached copy isup-to-date, the system can be made to work, but it is precisely all these diskaccesses that the caching system is trying to avoid.PROBLEM SOLUTIONS FOR CHAPTER 2 56. When a thread is stopped, it has values in the registers. They must be saved,just as when the process is stopped the registers must be saved. Timesharingthreads is no different than timesharing processes, so each thread needs itsown register save area.7. No. If a single-threaded process is blocked on the keyboard, it cannot fork.8. A worker thread will block when it has to read a Web page from the disk. Ifuser-level threads are being used, this action will block the entire process,destroying the value of multithreading. Thus it is essential that kernel threadsare used to permit some threads to block without affecting the others.9. Threads in a process cooperate. They are not hostile to one another. If yieldingis needed for the good of the application, then a thread will yield. Afterall, it is usually the same programmer who writes the code for all of them.10. User-level threads cannot be preempted by the clock uless the whole processquantum has been used up. Kernel-level threads can be preempted individually.In the latter case, if a thread runs too long, the clock will interrupt thecurrent process and thus the current thread. The kernel is free to pick a differentthread from the same process to run next if it so desires.11. In the single-threaded case, the cache hits take 15 msec and cache misses take90 msec. The weighted average is 2/3 × 15 + 1/3 × 90. Thus the meanrequest takes 40 msec and the server can do 25 per second. For a multithreadedserver, all the waiting for the disk is overlapped, so every requesttakes 15 msec, and the server can handle 66 2/3 requests per second.12. Yes. If the server is entirely CPU bound, there is no need to have multiplethreads. It just adds unnecessary complexity. As an example, consider a telephonedirectory assistance number (like 555-1212) for an area with 1 millionpeople. If each (name, telephone number) record is, say, 64 characters, theentire database takes 64 megabytes, and can easily be kept in the serversmemory to provide fast lookup.13. The pointers are really necessary because the size of the global variable isunknown. It could be anything from a character to an array of floating-pointnumbers. If the value were stored, one would have to give the size tocreate3global, which is all right, but what type should the second parameterof set3global be, and what type should the value of read3global be?14. It could happen that the runtime system is precisely at the point of blocking orunblocking a thread, and is busy manipulating the scheduling queues. Thiswould be a very inopportune moment for the clock interrupt handler to begininspecting those queues to see if it was time to do thread switching, since theymight be in an inconsistent state. One solution is to set a flag when the runtimesystem is entered. The clock handler would see this and set its own flag,6 PROBLEM SOLUTIONS FOR CHAPTER 2then return. When the runtime system finished, it would check the clock flag,see that a clock interrupt occurred, and now run the clock handler.15. Yes it is possible, but inefficient. A thread wanting to do a system call firstsets an alarm timer, then does the call. If the call blocks, the timer returnscontrol to the threads package. Of course, most of the time the call will notblock, and the timer has to be cleared. Thus each system call that mightblock has to be executed as three system calls. If timers go off prematurely,all kinds of problems can develop. This is not an attractive way to build athreads package.16. The priority inversion problem occurs when a low-priority process is in itscritical region and suddenly a high-priority process becomes ready and isscheduled. If it uses busy waiting, it will run forever. With user-levelthreads, it cannot happen that a low-priority thread is suddenly preempted toallow a high-priority thread run. There is no preemption. With kernel-levelthreads this problem can arise.17. Each thread calls procedures on its own, so it must have its own stack for thelocal variables, return addresses, and so on. This is equally true for user-levelthreads as for kernel-level threads.18. A race condition is a situation in which