1、MODERNOPERATINGSYSTEMSTHIRD EDITIONPROBLEM SOLUTIONSANDREW S. TANENBAUMVrije UniversiteitAmsterdam, The NetherlandsPRENTICE HALLUPPER SADDLE RIVER, NJ 07458Copyright Pearson Education, Inc. 2008SOLUTIONS TO CHAPTER 1 PROBLEMS1. Multiprogramming is the rapid switching of the CPU between multiple proc
2、-esses in memory. It is commonly used to keep the CPU busy while one ormore processes are doing I/O.2. 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
3、 spooling consists of firstcopying printable files to disk before printing them, rather than printing di-rectly as the output is generated. Input spooling on a personal computer is notvery likely, but output spooling is.3. The prime reason for multiprogramming is to give the CPU something to dowhile
4、 waiting for I/O to complete. If there is no DMA, the CPU is fully occu-pied doing I/O, so there is nothing to be gained (at least in terms of CPU utili-zation) by multiprogramming. No matter how much I/O a program does, theCPU will be 100% busy. This of course assumes the major delay is the waitwhi
5、le data are copied. A CPU could do other work if the I/O were slow forother reasons (arriving on a serial line, for instance).4. 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 mac
6、hines are architecturally compatible. They differ only inprice and performance, which is the essence of the family idea.5. A25 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
7、 and $11,520, respectively. For currentprices, check on how much RAM currently costs, probably less than $1/MB.6. Consider fairness and real time. Fairness requires that each process be allo-cated its resources in a fair way, with no process getting more than its fairshare. On the other hand, real t
8、ime requires that resources be allocated basedon the times when different processes must complete their execution. A real-time process may get a disproportionate share of the resources.7. Choices (a), (c), and (d) should be restricted to kernel mode.8. It may take 20, 25 or 30 msec to complete the e
9、xecution of these programsdepending on how the operating system schedules them. If P0 and P1 arescheduled on the same CPU and P2 is scheduled on the other CPU, it willtake 20 mses. If P0 and P2 are scheduled on the same CPU and P1 isscheduled on the other CPU, it will take 25 msec. If P1 and P2 are
10、scheduledon the same CPU and P0 is scheduled on the other CPU, it will take 30 msec.If all three are on the same CPU, it will take 35 msec.2 PROBLEM SOLUTIONS FOR CHAPTER 19. Every nanosecond one instruction emerges from the pipeline. This means themachine is executing 1 billion instructions per sec
11、ond. It does not matter atall how many stages the pipeline has. A 10-stage pipeline with 1 nsec perstage would also execute 1 billion instructions per second. All that matters ishow often a finished instruction pops out the end of the pipeline.10. Average access time =0.95 2 nsec (word is cache)+ 0.
12、05 0.99 10 nsec (word is in RAM, but not in cache)+ 0.05 0.01 10,000,000 nsec (word on disk only)= 5002.395 nsec= 5.002395 sec11. 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-M
13、B 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 scan-ning from the disk would require about 27 seconds, and from tape 2 minutes 7seconds. Of course, these
14、times are just to read the data. Processing andrewriting the data would increase the time.12. 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 befo
15、re 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.13. A trap instruction switches the execution mode of a CPU from the user modeto the kernel
16、 mode. This instruction allows a user program to invoke func-tions in the operating system kernel.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 ca
17、used by an external event and itstiming is not reproducible.15. 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 sys-tem because the single process is never suspended.16. Mounting a file system makes
18、 any files already in the mount point directoryinaccessible, so mount points are normally empty. However, a system admin-istrator might want to copy some of the most important files normally locatedin the mounted directory to the mount point so they could be found in theirnormal path in an emergency
19、 when the mounted device was being repaired.PROBLEM SOLUTIONS FOR CHAPTER 1 317. A system call allows a user process to access and execute operating systemfunctions inside the kernel. User programs use system calls to invoke operat-ing system services.18. Fork can fail if there are no free slots lef
20、t 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
21、 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. Time to retrieve the file =1 * 50 ms (Time
22、 to move the arm over track # 50)+ 5 ms (Time for the first sector to rotate under the head)+ 10/100 * 1000 ms (Read 10 MB)= 155 ms22. 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
23、 reading or writing. This is not possible with character special files.23. 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
24、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.24. Yes it can, especially if the kernel is a message-passing system.25. As far as program logic is concerned it does not matter whether
25、 a call to a li-brary 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 syste
26、m the operating sys-tem may schedule another process to run when a system call completes,further slowing the progress in real time of a calling process.26. Several UNIX calls have no counterpart in the Win32 API:Link: a Win32 program cannot refer to a file by an alternative name or see itin more tha
27、n one directory. Also, attempting to create a link is a convenientway to test for and create a lock on a file.4 PROBLEM SOLUTIONS FOR CHAPTER 1Mount and umount: a Windows program cannot make assumptions aboutstandard path names because on systems with multiple disk drives the drivename part of the p
28、ath may be different.Chmod: Windows uses access control listsKill: Windows programmers cannot kill a misbehaving program that is notcooperating.27. Every system architecture has its own set of instructions that it can execute.Thus a Pentium cannot execute SPARC programs and a SPARC cannot exe-cute P
29、entium programs. Also, different architectures differ in bus architectureused (such as VME, ISA, PCI, MCA, SBus, .) as well as the word size of theCPU (usually 32 or 64 bit). Because of these differences in hardware, it is notfeasible to build an operating system that is completely portable. A highl
30、yportable operating system will consist of two high-level layers-a machine-dependent layer and a machine independent layer. The machine-dependentlayer addresses the specifics of the hardware, and must be implemented sepa-rately for every architecture. This layer provides a uniform interface on which
31、the machine-independent layer is built. The machine-independent layer has tobe implemented only once. To be highly portable, the size of the machine-dependent layer must be kept as small as possible.28. Separation of policy and mechanism allows OS designers to implement asmall number of basic primit
32、ives in the kernel. These primitives are sim-plified, because they are not dependent of any specific policy. They can thenbe used to implement more complex mechanisms and policies at the userlevel.29. The conversions are straightforward:(a) A micro year is 106 365 24 3600 = 31.536 sec.(b) 1000 meter
33、s or 1 km.(c) There are 240bytes, which is 1,099,511,627,776 bytes.(d) It is 6 1024kg.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, the proc-ess could go directly fr
34、om blocked to running. The other missing transition,from ready to blocked, is impossible. A ready process cannot do I/O or any-thing else that might block it. Only a running process can block.PROBLEM SOLUTIONS FOR CHAPTER 2 52. You could have a register containing a pointer to the current process ta
35、bleentry. When I/O completed, the CPU would store the current machine state inthe current process table entry. Then it would go to the interrupt vector for theinterrupting device and fetch a pointer to another process table entry (the ser-vice procedure). This process would then be started up.3. Gen
36、erally, high-level languages do not allow the kind of access to CPU hard-ware that is required. For instance, an interrupt handler may be required toenable and disable the interrupt servicing a particular device, or to manipulatedata within a process stack area. Also, interrupt service routines must
37、 exe-cute 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 crash be-cause a poorly written user program does not allow for enough stack space.Second, if the kernel leaves stack dat
38、a 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. If each job has 50% I/O wait, then it will take 20 minutes to complete in theabsence of competition. If run sequentially, the second one wi
39、ll finish 40minutes after the first one starts. With two jobs, the approximate CPU utiliza-tion is 1 0.52. Thus each one gets 0.375 CPU minute per minute of realtime. To accumulate 10 minutes of CPU time, a job must run for 10/0.375minutes, or about 26.67 minutes. Thus running sequentially the jobs
40、finishafter 40 minutes, but running in parallel they finish after 26.67 minutes.6. It would be difficult, if not impossible, to keep the file system consistent. Sup-pose that a client process sends a request to server process 1 to update a file.This process updates the cache entry in its memory. Sho
41、rtly thereafter, anoth-er client process sends a request to server 2 to read that file. Unfortunately, ifthe file is also cached there, server 2, in its innocence, will return obsoletedata. If the first process writes the file through to the disk after caching it,and server 2 checks the disk on ever
42、y read to see if its cached copy is up-to-date, the system can be made to work, but it is precisely all these disk ac-cesses that the caching system is trying to avoid.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 W
43、eb 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. Yes. If the server is entirely CPU bound, the
44、re is no need to have multiplethreads. It just adds unnecessary complexity. As an example, consider a tele-phone directory assistance number (like 555-1212) for an area with 1 million6 PROBLEM SOLUTIONS FOR CHAPTER 2people. If each (name, telephone number) record is, say, 64 characters, theentire da
45、tabase takes 64 megabytes, and can easily be kept in the serversmemory to provide fast lookup.10. 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. Multipro-gramming threads is no different than multiprogramm
46、ing processes, so eachthread needs its own register save area.11. Threads in a process cooperate. They are not hostile to one another. If yield-ing is 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.12.
47、 User-level threads cannot be preempted by the clock unless the whole proc-ess quantum has been used up. Kernel-level threads can be preempted indivi-dually. 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
48、pick a dif-ferent thread from the same process to run next if it so desires.13. 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 mean re-quest takes 40 msec and the server can do 25 per second. For a multithreade
49、dserver, all the waiting for the disk is overlapped, so every request takes 15msec, and the server can handle 66 2/3 requests per second.14. The biggest advantage is the efficiency. No traps to the kernel are needed toswitch threads. The biggest disadvantage is that if one thread blocks, the en-tire process blocks.15. Yes, it can be done. After each call to pthreadrucreate, the main programcould do a pthreadrujoin to wait until the thread just created has exited beforecreating the next thread.16. The pointers are really necessary because the size of the g