+ All Categories
Home > Documents > Examples&Sollutions

Examples&Sollutions

Date post: 10-Apr-2015
Category:
Upload: api-3842044
View: 6,647 times
Download: 1 times
Share this document with a friend
134
1 CHAPTER Introduction Practice Exercises 1.1 What are the three main purposes of an operating system? 1.2 What are the main differences between operating systems for mainframe computers and personal computers? 1.3 List the four steps that are necessary to run a program on a completely dedicated machine. 1.4 We have stressed the need for an operating system to make efficient use of the computing hardware. When is it appropriate for the operating system to forsake this principle and to wasteresources? Why is such a system not really wasteful? 1.5 What is the main difficulty that a programmer must overcome in writing an operating system for a real-time environment? 1.6 Consider the various definitions of operating system. Consider whether the operating system should include applications such as Web browsers and mail programs. Argue both that it should and that it should not, and support your answer. 1.7 How does the distinction between kernel mode and user mode function as a rudimentary form of protection (security) system? 1.8 Which of the following instructions should be privileged? a. Set value of timer. b. Read the clock. c. Clear memory. d. Issue a trap instruction. e. Turn off interrupts. f. Modify entries in device-status table. 1
Transcript
Page 1: Examples&Sollutions

1C H A P T E R

Introduction

Practice Exercises

1.1 What are the three main purposes of an operating system?

1.2 What are the main differences between operating systems for mainframecomputers and personal computers?

1.3 List the four steps that are necessary to run a program on a completelydedicated machine.

1.4 We have stressed the need for an operating system to make efficient useof the computing hardware. When is it appropriate for the operatingsystem to forsake this principle and to “waste” resources? Why is sucha system not really wasteful?

1.5 What is the main difficulty that a programmer must overcome in writingan operating system for a real-time environment?

1.6 Consider the various definitions of operating system. Consider whetherthe operating system should include applications such as Web browsersand mail programs. Argue both that it should and that it should not, andsupport your answer.

1.7 How does the distinction between kernel mode and user mode functionas a rudimentary form of protection (security) system?

1.8 Which of the following instructions should be privileged?

a. Set value of timer.

b. Read the clock.

c. Clear memory.

d. Issue a trap instruction.

e. Turn off interrupts.

f. Modify entries in device-status table.

1

Page 2: Examples&Sollutions

2 Chapter 1 Introduction

g. Switch from user to kernel mode.

h. Access I/O device.

1.9 Some early computers protected the operating system by placing it ina memory partition that could not be modified by either the user jobor the operating system itself. Describe two difficulties that you thinkcould arise with such a scheme.

1.10 Some CPUs provide for more than two modes of operation. What aretwo possible uses of these multiple modes?

1.11 Timers could be used to compute the current time. Provide a short de-scription of how this could be accomplished.

1.12 Is the Internet a LAN or a WAN?

Page 3: Examples&Sollutions

1C H A P T E R

Introduction

Practice Exercises

1.1 What are the three main purposes of an operating system?Answer:

• To provide an environment for a computer user to execute programson computer hardware in a convenient and efficient manner.

• To allocate the separate resources of the computer as needed tosolve the problem given. The allocation process should be as fairand efficient as possible.

• As a control program it serves two major functions: (1) supervisionof the execution of user programs to prevent errors and improperuse of the computer, and (2) management of the operation andcontrol of I/O devices.

1.2 What are the main differences between operating systems for mainframecomputers and personal computers?Answer: Generally, operating systems for batch systems have simplerrequirements than for personal computers. Batch systems do not haveto be concerned with interacting with a user as much as a personalcomputer. As a result, an operating system for a PC must be concernedwith response time for an interactive user. Batch systems do not havesuch requirements. A pure batch system also may have not to handletime sharing, whereas an operating system must switch rapidly betweendifferent jobs.

1.3 List the four steps that are necessary to run a program on a completelydedicated machine.Answer:

1

Page 4: Examples&Sollutions

2 Chapter 1 Introduction

a. Reserve machine time.

b. Manually load program into memory.

c. Load starting address and begin execution.

d. Monitor and control execution of program from console.

1.4 We have stressed the need for an operating system to make efficient useof the computing hardware. When is it appropriate for the operatingsystem to forsake this principle and to “waste” resources? Why is sucha system not really wasteful?Answer: Single-user systems should maximize use of the system forthe user. A GUI might “waste” CPU cycles, but it optimizes the user’sinteraction with the system.

1.5 What is the main difficulty that a programmer must overcome in writingan operating system for a real-time environment?Answer: The main difficulty is keeping the operating system withinthe fixed time constraints of a real-time system. If the system does notcomplete a task in a certain time frame, it may cause a breakdownof the entire system it is running. Therefore when writing an operatingsystem for a real-time system, the writer must be sure that his schedulingschemes don’t allow response time to exceed the time constraint.

1.6 Consider the various definitions of operating system. Consider whetherthe operating system should include applications such as Web browsersand mail programs. Argue both that it should and that it should not, andsupport your answer.Answer: Point. Applications such as web browsers and email tools areperforming an increasingly important role in modern desktop computersystems. To fulfill this role, they should be incorporated as part of theoperating system. By doing so, they can provide better performanceand better integration with the rest of the system. In addition, theseimportant applications can have the same look-and-feel as the operatingsystem software.Counterpoint. The fundamental role of the operating system is to man-age system resources such as the CPU, memory, I/O devices, etc. In ad-dition, it’s role is to run software applications such as web browsers andemail applications. By incorporating such applications into the operatingsystem, we burden the operating system with additional functionality.Such a burden may result in the operating system performing a less-than-satisfactory job at managing system resources. In addition, we increasethe size of the operating system thereby increasing the likelihood ofsystem crashes and security violations.

1.7 How does the distinction between kernel mode and user mode functionas a rudimentary form of protection (security) system?Answer: The distinction between kernel mode and user mode pro-vides a rudimentary form of protection in the following manner. Certaininstructions could be executed only when the CPU is in kernel mode.Similarly, hardware devices could be accessed only when the programis executing in kernel mode. Control over when interrupts could be en-

Page 5: Examples&Sollutions

Practice Exercises 3

abled or disabled is also possible only when the CPU is in kernel mode.Consequently, the CPU has very limited capability when executing inuser mode, thereby enforcing protection of critical resources.

1.8 Which of the following instructions should be privileged?

a. Set value of timer.

b. Read the clock.

c. Clear memory.

d. Issue a trap instruction.

e. Turn off interrupts.

f. Modify entries in device-status table.

g. Switch from user to kernel mode.

h. Access I/O device.

Answer: The following operations need to be privileged: Set value oftimer, clear memory, turn off interrupts, modify entries in device-statustable, access I/O device. The rest can be performed in user mode.

1.9 Some early computers protected the operating system by placing it ina memory partition that could not be modified by either the user jobor the operating system itself. Describe two difficulties that you thinkcould arise with such a scheme.Answer: The data required by the operating system (passwords, accesscontrols, accounting information, and so on) would have to be storedin or passed through unprotected memory and thus be accessible tounauthorized users.

1.10 Some CPUs provide for more than two modes of operation. What aretwo possible uses of these multiple modes?Answer: Although most systems only distinguish between user andkernel modes, some CPUs have supported multiple modes. Multiplemodes could be used to provide a finer-grained security policy. Forexample, rather than distinguishing between just user and kernel mode,you could distinguish between different types of user mode. Perhapsusers belonging to the same group could execute each other’s code. Themachine would go into a specified mode when one of these users wasrunning code. When the machine was in this mode, a member of thegroup could run code belonging to anyone else in the group.Another possibility would be to provide different distinctions withinkernel code. For example, a specific mode could allow USB device driversto run. This would mean that USB devices could be serviced withouthaving to switch to kernel mode, thereby essentially allowing USB devicedrivers to run in a quasi-user/kernel mode.

1.11 Timers could be used to compute the current time. Provide a short de-scription of how this could be accomplished.Answer: A program could use the following approach to compute thecurrent time using timer interrupts. The program could set a timer for

Page 6: Examples&Sollutions

4 Chapter 1 Introduction

some time in the future and go to sleep. When it is awakened by theinterrupt, it could update its local state, which it is using to keep trackof the number of interrupts it has received thus far. It could then repeatthis process of continually setting timer interrupts and updating its localstate when the interrupts are actually raised.

1.12 Is the Internet a LAN or a WAN?Answer: The Internet is a WAN as the various computers are locatedat geographically different places and are connected by long-distancenetwork links.

Page 7: Examples&Sollutions

2C H A P T E ROperating-SystemStructures

Practice Exercises

2.1 What is the purpose of system calls?

2.2 What are the five major activities of an operating system in regard toprocess management?

2.3 What are the three major activities of an operating system in regard tomemory management?

2.4 What are the three major activities of an operating system in regard tosecondary-storage management?

2.5 What is the purpose of the command interpreter? Why is it usuallyseparate from the kernel?

2.6 What system calls have to be executed by a command interpreter or shellin order to start a new process?

2.7 What is the purpose of system programs?

2.8 What is the main advantage of the layered approach to system design?What are the disadvantages of using the layered approach?

2.9 List five services provided by an operating system. Explain how eachprovides convenience to the users. Explain also in which cases it wouldbe impossible for user-level programs to provide these services.

2.10 What is the purpose of system calls?

2.11 What are the main advantages of the microkernel approach to systemdesign?

2.12 Why do some systems store the operating system in firmware, and otherson disk?

2.13 How could a system be designed to allow a choice of operating systemsto boot from? What would the bootstrap program need to do?

3

Page 8: Examples&Sollutions
Page 9: Examples&Sollutions

2C H A P T E ROperating-SystemStructures

Practice Exercises

2.1 What is the purpose of system calls?Answer: System calls allow user-level processes to request services ofthe operating system.

2.2 What are the five major activities of an operating system in regard toprocess management?Answer:

a. The creation and deletion of both user and system processes

b. The suspension and resumption of processes

c. The provision of mechanisms for process synchronization

d. The provision of mechanisms for process communication

e. The provision of mechanisms for deadlock handling

2.3 What are the three major activities of an operating system in regard tomemory management?Answer:

a. Keep track of which parts of memory are currently being usedand by whom.

b. Decide which processes are to be loaded into memory when mem-ory space becomes available.

c. Allocate and deallocate memory space as needed.

2.4 What are the three major activities of an operating system in regard tosecondary-storage management?Answer:

5

Page 10: Examples&Sollutions

6 Chapter 2 Operating-System Structures

• Free-space management.

• Storage allocation.

• Disk scheduling.

2.5 What is the purpose of the command interpreter? Why is it usuallyseparate from the kernel?Answer: It reads commands from the user or from a file of commandsand executes them, usually by turning them into one or more systemcalls. It is usually not part of the kernel since the command interpreteris subject to changes.

2.6 What system calls have to be executed by a command interpreter or shellin order to start a new process?Answer: In Unix systems, a fork system call followed by an exec systemcall need to be performed to start a new process. The fork call clones thecurrently executing process, while the exec call overlays a new processbased on a different executable over the calling process.

2.7 What is the purpose of system programs?Answer: System programs can be thought of as bundles of usefulsystem calls. They provide basic functionality to users so that users donot need to write their own programs to solve common problems.

2.8 What is the main advantage of the layered approach to system design?What are the disadvantages of using the layered approach?Answer: As in all cases of modular design, designing an operatingsystem in a modular way has several advantages. The system is easierto debug and modify because changes affect only limited sections ofthe system rather than touching all sections of the operating system.Information is kept only where it is needed and is accessible only withina defined and restricted area, so any bugs affecting that data must belimited to a specific module or layer.

2.9 List five services provided by an operating system. Explain how eachprovides convenience to the users. Explain also in which cases it wouldbe impossible for user-level programs to provide these services.Answer:

a. Program execution. The operating system loads the contents (orsections) of a file into memory and begins its execution. A user-level program could not be trusted to properly allocate CPU time.

b. I/O operations. Disks, tapes, serial lines, and other devices mustbe communicated with at a very low level. The user need onlyspecify the device and the operation to perform on it, while thesystem converts that request into device- or controller-specificcommands. User-level programs cannot be trusted to access onlydevices they should have access to and to access them only whenthey are otherwise unused.

c. File-system manipulation. There are many details in file creation,deletion, allocation, and naming that users should not have to per-form. Blocks of disk space are used by files and must be tracked.

Page 11: Examples&Sollutions

Practice Exercises 7

Deleting a file requires removing the name file information andfreeing the allocated blocks. Protections must also be checked toassure proper file access. User programs could neither ensure ad-herence to protection methods nor be trusted to allocate only freeblocks and deallocate blocks on file deletion.

d. Communications. Message passing between systems requiresmessages to be turned into packets of information, sent to the net-work controller, transmitted across a communications medium,and reassembled by the destination system. Packet ordering anddata correction must take place. Again, user programs might notcoordinate access to the network device, or they might receivepackets destined for other processes.

e. Error detection. Error detection occurs at both the hardware andsoftware levels. At the hardware level, all data transfers must beinspected to ensure that data have not been corrupted in transit.All data on media must be checked to be sure they have notchanged since they were written to the media. At the softwarelevel, media must be checked for data consistency; for instance,whether the number of allocated and unallocated blocks of storagematch the total number on the device. There, errors are frequentlyprocess-independent (for instance, the corruption of data on adisk), so there must be a global program (the operating system)that handles all types of errors. Also, by having errors processedby the operating system, processes need not contain code to catchand correct all the errors possible on a system.

2.10 What is the purpose of system calls?Answer: System calls allow user-level processes to request services ofthe operating system.

2.11 What are the main advantages of the microkernel approach to systemdesign?Answer: Benefits typically include the following (a) adding a newservice does not require modifying the kernel, (b) it is more secure asmore operations are done in user mode than in kernel mode, and (c)a simpler kernel design and functionality typically results in a morereliable operating system.

2.12 Why do some systems store the operating system in firmware, and otherson disk?Answer: For certain devices, such as handheld PDAs and cellular tele-phones, a disk with a file system may be not be available for the device.In this situation, the operating system must be stored in firmware.

2.13 How could a system be designed to allow a choice of operating systemsto boot from? What would the bootstrap program need to do?Answer: Consider a system that would like to run both WindowsXP and three different distributions of Linux (e.g., RedHat, Debian, andMandrake). Each operating system will be stored on disk. During systemboot-up, a special program (which we will call the boot manager) willdetermine which operating system to boot into. This means that rather

Page 12: Examples&Sollutions

8 Chapter 2 Operating-System Structures

initially booting to an operating system, the boot manager will first runduring system startup. It is this boot manager that is responsible fordetermining which system to boot into. Typically boot managers mustbe stored at certain locations of the hard disk to be recognized duringsystem startup. Boot managers often provide the user with a selection ofsystems to boot into; boot managers are also typically designed to bootinto a default operating system if no choice is selected by the user.

Page 13: Examples&Sollutions

3C H A P T E R

Processes

Practice Exercises

3.1 Palm OS provides no means of concurrent processing. Discuss threemajor complications that concurrent processing adds to an operatingsystem.

3.2 The Sun UltraSPARC processor has multiple register sets. Describe theactions of a context switch if the new context is already loaded intoone of the register sets. What else must happen if the new context is inmemory rather than in a register set and all the register sets are in use?

3.3 When a process creates a new process using the fork() operation, whichof the following state is shared between the parent process and the childprocess?

a. Stack

b. Heap

c. Shared memory segments

3.4 Again considering the RPC mechanism, consider the “exactly once” se-mantic. Does the algorithm for implementing this semantic execute cor-rectly even if the “ACK” message back to the client is lost due to a networkproblem? Describe the sequence of messages and whether "exactly once"is still preserved.

3.5 Assume that a distributed system is susceptible to server failure. Whatmechanisms would be required to guarantee the “exactly once” seman-tics for execution of RPCs?

5

Page 14: Examples&Sollutions
Page 15: Examples&Sollutions

3C H A P T E R

Processes

Practice Exercises

3.1 Palm OS provides no means of concurrent processing. Discuss threemajor complications that concurrent processing adds to an operatingsystem.Answer:

a. A method of time sharing must be implemented to allow eachof several processes to have access to the system. This methodinvolves the preemption of processes that do not voluntarily giveup the CPU (by using a system call, for instance) and the kernelbeing reentrant (so more than one process may be executing kernelcode concurrently).

b. Processes and system resources must have protections and mustbe protected from each other. Any given process must be limitedin the amount of memory it can use and the operations it canperform on devices like disks.

c. Care must be taken in the kernel to prevent deadlocks betweenprocesses, so processes aren’t waiting for each other’s allocatedresources.

3.2 The Sun UltraSPARC processor has multiple register sets. Describe theactions of a context switch if the new context is already loaded intoone of the register sets. What else must happen if the new context is inmemory rather than in a register set and all the register sets are in use?Answer: The CPU current-register-set pointer is changed to point to theset containing the new context, which takes very little time. If the contextis in memory, one of the contexts in a register set must be chosen and bemoved to memory, and the new context must be loaded from memory

9

Page 16: Examples&Sollutions

10 Chapter 3 Processes

into the set. This process takes a little more time than on systems withone set of registers, depending on how a replacement victim is selected.

3.3 When a process creates a new process using the fork() operation, whichof the following state is shared between the parent process and the childprocess?

a. Stack

b. Heap

c. Shared memory segments

Answer: Only the shared memory segments are shared between theparent process and the newly forked child process. Copies of the stackand the heap are made for the newly created process.

3.4 Again considering the RPC mechanism, consider the “exactly once” se-mantic. Does the algorithm for implementing this semantic execute cor-rectly even if the “ACK” message back to the client is lost due to a networkproblem? Describe the sequence of messages and whether "exactly once"is still preserved.Answer: The “exactly once” semantics ensure that a remore procedurewill be executed exactly once and only once. The general algorithm forensuring this combines an acknowledgment (ACK) scheme combinedwith timestamps (or some other incremental counter that allows theserver to distinguish between duplicate messages).The general strategy is for the client to send the RPC to the server alongwith a timestamp. The client will also start a timeout clock. The clientwill then wait for one of two occurrences: (1) it will receive an ACK fromthe server indicating that the remote procedure was performed, or (2) itwill time out. If the client times out, it assumes the server was unableto perform the remote procedure so the client invokes the RPC a secondtime, sending a later timestamp. The client may not receive the ACK forone of two reasons: (1) the original RPC was never received by the server,or (2) the RPC was correctly received—and performed—by the serverbut the ACK was lost. In situation (1), the use of ACKs allows the serverultimately to receive and perform the RPC. In situation (2), the server willreceive a duplicate RPC and it will use the timestamp to identify it as aduplicate so as not to perform the RPC a second time. It is important tonote that the server must send a second ACK back to the client to informthe client the RPC has been performed.

3.5 Assume that a distributed system is susceptible to server failure. Whatmechanisms would be required to guarantee the “exactly once” seman-tics for execution of RPCs?Answer: The server should keep track in stable storage (such as adisk log) information regarding what RPC operations were received,whether they were successfully performed, and the results associatedwith the operations. When a server crash takes place and a RPC messageis received, the server can check whether the RPC had been previouslyperformed and therefore guarantee “exactly once” semanctics for theexecution of RPCs.

Page 17: Examples&Sollutions

4C H A P T E R

Threads

Practice Exercises

4.1 Provide two programming examples in which multithreading providesbetter performance than a single-threaded solution.

4.2 What are two differences between user-level threads and kernel-levelthreads? Under what circumstances is one type better than the other?

4.3 Describe the actions taken by a kernel to context switch between kernel-level threads.

4.4 What resources are used when a thread is created? How do they differfrom those used when a process is created?

4.5 Assume an operating system maps user-level threads to the kernel us-ing the many-to-many model and the mapping is done through LWPs.Furthermore, the system allows developers to create real-time threads.Is it necessary to bind a real-time thread to an LWP? Explain.

4.6 A Pthread program that performs the summation function was providedin Section 4.3.1. Rewrite this program in Java.

7

Page 18: Examples&Sollutions
Page 19: Examples&Sollutions

4C H A P T E R

Threads

Practice Exercises

4.1 Provide two programming examples in which multithreading providesbetter performance than a single-threaded solution.Answer: (1) A Web server that services each request in a separate thread.2) (A parallelized application such as matrix multiplication where (dif-ferent parts of the matrix may be worked on in parallel. (3) An (interac-tive GUI program such as a debugger where a thread is used (to monitoruser input, another thread represents the running (application, and athird thread monitors performance.

4.2 What are two differences between user-level threads and kernel-levelthreads? Under what circumstances is one type better than the other?Answer: (1) User-level threads are unknown by the kernel, whereas thekernel is aware of kernel threads. (2) On systems using either M:1 or M:Nmapping, user threads are scheduled by the thread library and the kernelschedules kernel threads. (3) Kernel threads need not be associated witha process whereas every user thread belongs to a process. Kernel threadsare generally more expensive to maintain than user threads as they mustbe represented with a kernel data structure.

4.3 Describe the actions taken by a kernel to context switch between kernel-level threads.Answer: Context switching between kernel threads typically requiressaving the value of the CPU registers from the thread being switched outand restoring the CPU registers of the new thread being scheduled.

4.4 What resources are used when a thread is created? How do they differfrom those used when a process is created?Answer: Because a thread is smaller than a process, thread creationtypically uses fewer resources than process creation. Creating a process

11

Page 20: Examples&Sollutions

12 Chapter 4 Threads

requires allocating a process control block (PCB), a rather large data struc-ture. The PCB includes a memory map, list of open files, and environ-ment variables. Allocating and managing the memory map is typicallythe most time-consuming activity. Creating either a user or kernel threadinvolves allocating a small data structure to hold a register set, stack, andpriority.

4.5 Assume an operating system maps user-level threads to the kernel us-ing the many-to-many model and the mapping is done through LWPs.Furthermore, the system allows developers to create real-time threads.Is it necessary to bind a real-time thread to an LWP? Explain.Answer: Yes. Timing is crucial to real-time applications. If a thread ismarked as real-time but is not bound to an LWP, the thread may haveto wait to be attached to an LWP before running. Consider if a real-timethread is running (is attached to an LWP) and then proceeds to block (i.e.must perform I/O, has been preempted by a higher-priority real-timethread, is waiting for a mutual exclusion lock, etc.) While the real-timethread is blocked, the LWP it was attached to has been assigned to anotherthread. When the real-time thread has been scheduled to run again, itmust first wait to be attached to an LWP. By binding an LWP to a real-time thread you are ensuring the thread will be able to run with minimaldelay once it is scheduled.

4.6 A Pthread program that performs the summation function was providedin Section 4.3.1. Rewrite this program in Java.Answer: Please refer to the supporting Web site for source code solution.

Page 21: Examples&Sollutions

5C H A P T E R

CPU Scheduling

Practice Exercises

5.1 A CPU scheduling algorithm determines an order for the execution of itsscheduled processes. Given n processes to be scheduled on one proces-sor, how many possible different schedules are there? Give a formula interms of n.

5.2 Define the difference between preemptive and nonpreemptive schedul-ing.

5.3 Suppose that the following processes arrive for execution at the timesindicated. Each process will run the listed amount of time. In answeringthe questions, use nonpreemptive scheduling and base all decisions onthe information you have at the time the decision must be made.

Process Arrival Time Burst TimeP1 0.0 8P2 0.4 4P3 1.0 1

a. What is the average turnaround time for these processes with theFCFS scheduling algorithm?

b. What is the average turnaround time for these processes with theSJF scheduling algorithm?

c. The SJF algorithm is supposed to improve performance, but noticethat we chose to run process P1 at time 0 because we did not knowthat two shorter processes would arrive soon. Compute what theaverage turnaround time will be if the CPU is left idle for the first 1unit and then SJF scheduling is used. Remember that processes P1and P2 are waiting during this idle time, so their waiting time mayincrease. This algorithm could be known as future-knowledgescheduling.

9

Page 22: Examples&Sollutions

10 Chapter 5 CPU Scheduling

5.4 What advantage is there in having different time-quantum sizes on dif-ferent levels of a multilevel queueing system?

5.5 Many CPU-scheduling algorithms are parameterized. For example, theRR algorithm requires a parameter to indicate the time slice. Multilevelfeedback queues require parameters to define the number of queues,the scheduling algorithms for each queue, the criteria used to moveprocesses between queues, and so on.These algorithms are thus really sets of algorithms (for example, theset of RR algorithms for all time slices, and so on). One set of algorithmsmay include another (for example, the FCFS algorithm is the RR algorithmwith an infinite time quantum). What (if any) relation holds between thefollowing pairs of sets of algorithms?

a. Priority and SJF

b. Multilevel feedback queues and FCFS

c. Priority and FCFS

d. RR and SJF

5.6 Suppose that a scheduling algorithm (at the level of short-term CPUscheduling) favors those processes that have used the least processortime in the recent past. Why will this algorithm favor I/O-bound pro-grams and yet not permanently starve CPU-bound programs?

5.7 Distinguish between PCS and SCS scheduling.

5.8 Assume an operating system maps user-level threads to the kernel usingthe many-to-many model where the mapping is done through the useof LWPs. Furthermore, the system allows program developers to createreal-time threads. Is it necessary to bind a real-time thread to an LWP?

Page 23: Examples&Sollutions

5C H A P T E R

CPU Scheduling

Practice Exercises

5.1 A CPU scheduling algorithm determines an order for the execution of itsscheduled processes. Given n processes to be scheduled on one proces-sor, how many possible different schedules are there? Give a formula interms of n.Answer: n! (n factorial = n × n – 1 × n – 2 × ... × 2 × 1).

5.2 Define the difference between preemptive and nonpreemptive schedul-ing.Answer: Preemptive scheduling allows a process to be interruptedin the midst of its execution, taking the CPU away and allocating itto another process. Nonpreemptive scheduling ensures that a processrelinquishes control of the CPU only when it finishes with its currentCPU burst.

5.3 Suppose that the following processes arrive for execution at the timesindicated. Each process will run the listed amount of time. In answeringthe questions, use nonpreemptive scheduling and base all decisions onthe information you have at the time the decision must be made.

Process Arrival Time Burst TimeP1 0.0 8P2 0.4 4P3 1.0 1

a. What is the average turnaround time for these processes with theFCFS scheduling algorithm?

13

Page 24: Examples&Sollutions

14 Chapter 5 CPU Scheduling

b. What is the average turnaround time for these processes with theSJF scheduling algorithm?

c. The SJF algorithm is supposed to improve performance, but noticethat we chose to run process P1 at time 0 because we did not knowthat two shorter processes would arrive soon. Compute what theaverage turnaround time will be if the CPU is left idle for the first 1unit and then SJF scheduling is used. Remember that processes P1and P2 are waiting during this idle time, so their waiting time mayincrease. This algorithm could be known as future-knowledgescheduling.

Answer:

a. 10.53

b. 9.53

c. 6.86

Remember that turnaround time is finishing time minus arrival time, soyou have to subtract the arrival times to compute the turnaround times.FCFS is 11 if you forget to subtract arrival time.

5.4 What advantage is there in having different time-quantum sizes on dif-ferent levels of a multilevel queueing system?Answer: Processes that need more frequent servicing, for instance,interactive processes such as editors, can be in a queue with a small timequantum. Processes with no need for frequent servicing can be in a queuewith a larger quantum, requiring fewer context switches to complete theprocessing, and thus making more efficient use of the computer.

5.5 Many CPU-scheduling algorithms are parameterized. For example, theRR algorithm requires a parameter to indicate the time slice. Multilevelfeedback queues require parameters to define the number of queues,the scheduling algorithms for each queue, the criteria used to moveprocesses between queues, and so on.These algorithms are thus really sets of algorithms (for example, theset of RR algorithms for all time slices, and so on). One set of algorithmsmay include another (for example, the FCFS algorithm is the RR algorithmwith an infinite time quantum). What (if any) relation holds between thefollowing pairs of sets of algorithms?

a. Priority and SJF

b. Multilevel feedback queues and FCFS

c. Priority and FCFS

d. RR and SJF

Answer:

a. The shortest job has the highest priority.

b. The lowest level of MLFQ is FCFS.

Page 25: Examples&Sollutions

Practice Exercises 15

c. FCFS gives the highest priority to the job having been in existencethe longest.

d. None.

5.6 Suppose that a scheduling algorithm (at the level of short-term CPUscheduling) favors those processes that have used the least processortime in the recent past. Why will this algorithm favor I/O-bound pro-grams and yet not permanently starve CPU-bound programs?Answer: It will favor the I/O-bound programs because of the relativelyshort CPU burst request by them; however, the CPU-bound programswill not starve because the I/O-bound programs will relinquish the CPUrelatively often to do their I/O.

5.7 Distinguish between PCS and SCS scheduling.Answer: PCS scheduling is done local to the process. It is how thethread library schedules threads onto available LWPs. SCS scheduling isthe situation where the operating system schedules kernel threads. Onsystems using either many-to-one or many-to-many, the two schedulingmodels are fundamentally different. On systems using one-to-one, PCSand SCS are the same.

5.8 Assume an operating system maps user-level threads to the kernel usingthe many-to-many model where the mapping is done through the useof LWPs. Furthermore, the system allows program developers to createreal-time threads. Is it necessary to bind a real-time thread to an LWP?Answer: Yes, otherwise a user thread may have to compete for anavailable LWP prior to being actually scheduled. By binding the userthread to an LWP, there is no latency while waiting for an available LWP;the real-time user thread can be scheduled immediately.

Page 26: Examples&Sollutions
Page 27: Examples&Sollutions

6C H A P T E R

ProcessSynchronization

Practice Exercises

6.1 In Section 6.4 we mentioned that disabling interrupts frequently couldaffect the system’s clock. Explain why it could and how such effectscould be minimized.

6.2 The Cigarette-Smokers Problem. Consider a system with three smoker pro-cesses and one agent process. Each smoker continuously rolls a cigaretteand then smokes it. But to roll and smoke a cigarette, the smoker needsthree ingredients: tobacco, paper, and matches. One of the smoker pro-cesses has paper, another has tobacco, and the third has matches. Theagent has an infinite supply of all three materials. The agent placestwo of the ingredients on the table. The smoker who has the remainingingredient then makes and smokes a cigarette, signaling the agent oncompletion. The agent then puts out another two of the three ingredi-ents, and the cycle repeats. Write a program to synchronize the agentand the smokers using Java synchronization.

6.3 Give the reasons why Solaris, Windows XP, and Linux implement mul-tiple locking mechanisms. Describe the circumstances under which theyuse spinlocks, mutexes, semaphores, adaptive mutexes, and conditionvariables. In each case, explain why the mechanism is needed.

6.4 Explain the differences, in terms of cost, among the three storage typesvolatile, nonvolatile, and stable.

6.5 Explain the purpose of the checkpoint mechanism. How often shouldcheckpoints be performed? Describe how the frequency of checkpointsaffects:

• 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

6.6 Explain the concept of transaction atomicity.

11

Page 28: Examples&Sollutions

12 Chapter 6 Process Synchronization

6.7 Show that some schedules are possible under the two-phase lockingprotocol but not possible under the timestamp protocol, and vice versa.

6.8 The wait() statement in all Java program examples was part of a whileloop. Explain why you would always need to use a while statementwhen using wait() and why you would never use an if statement.

Page 29: Examples&Sollutions

6C H A P T E R

ProcessSynchronization

Practice Exercises

6.1 In Section 6.4 we mentioned that disabling interrupts frequently couldaffect the system’s clock. Explain why it could and how such effectscould be minimized.Answer: The system clock is updated at every clock interrupt. If in-terrupts were disabled—particularly for a long period of time—it ispossible the system clock could easily lose the correct time. The sys-tem clock is also used for scheduling purposes. For example, the timequantum for a process is expressed as a number of clock ticks. At everyclock interrupt, the scheduler determines if the time quantum for thecurrently running process has expired. If clock interrupts were disabled,the scheduler could not accurately assign time quantums. This effect canbe minimized by disabling clock interrupts for only very short periods.

6.2 The Cigarette-Smokers Problem. Consider a system with three smoker pro-cesses and one agent process. Each smoker continuously rolls a cigaretteand then smokes it. But to roll and smoke a cigarette, the smoker needsthree ingredients: tobacco, paper, and matches. One of the smoker pro-cesses has paper, another has tobacco, and the third has matches. Theagent has an infinite supply of all three materials. The agent placestwo of the ingredients on the table. The smoker who has the remainingingredient then makes and smokes a cigarette, signaling the agent oncompletion. The agent then puts out another two of the three ingredi-ents, and the cycle repeats. Write a program to synchronize the agentand the smokers using Java synchronization.Answer: Please refer to the supporting Web site for source code solution.

6.3 Give the reasons why Solaris, Windows XP, and Linux implement mul-tiple locking mechanisms. Describe the circumstances under which they

17

Page 30: Examples&Sollutions

18 Chapter 6 Process Synchronization

use spinlocks, mutexes, semaphores, adaptive mutexes, and conditionvariables. In each case, explain why the mechanism is needed.Answer: These operating systems provide different locking mecha-nisms depending on the application developers’ needs. Spinlocks areuseful for multiprocessor systems where a thread can run in a busy-loop(for a short period of time) rather than incurring the overhead of beingput in a sleep queue. Mutexes are useful for locking resources. Solaris 2uses adaptive mutexes, meaning that the mutex is implemented with aspin lock on multiprocessor machines. Semaphores and condition vari-ables are more appropriate tools for synchronization when a resourcemust be held for a long period of time, since spinning is inefficient for along duration.

6.4 Explain the differences, in terms of cost, among the three storage typesvolatile, nonvolatile, and stable.Answer: Volatile storage refers to main and cache memory and is veryfast. However, volatile storage cannot survive system crashes or power-ing down the system. Nonvolatile storage survives system crashes andpowered-down systems. Disks and tapes are examples of nonvolatilestorage. Recently, USB devices using erasable program read-only mem-ory (EPROM) have appeared providing nonvolatile storage. Stable stor-age refers to storage that technically can never be lost as there are redun-dant backup copies of the data (usually on disk).

6.5 Explain the purpose of the checkpoint mechanism. How often shouldcheckpoints be performed? Describe how the frequency of checkpointsaffects:

• 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: A checkpoint log record indicates that a log record and itsmodified data has been written to stable storage and that the transactionneed not to be redone in case of a system crash. Obviously, the more oftencheckpoints are performed, the less likely it is that redundant updateswill have to be performed during the recovery process.

• System performance when no failure occurs—If no failures occur,the system must incur the cost of performing checkpoints that areessentially unnecessary. In this situation, performing checkpointsless often will lead to better system performance.

• The time it takes to recover from a system crash—The existence of acheckpoint record means that an operation will not have to beredone during system recovery. In this situation, the more oftencheckpoints were performed, the faster the recovery time is from asystem crash.

• The time it takes to recover from a disk crash—The existence of acheckpoint record means that an operation will not have to beredone during system recovery. In this situation, the more often

Page 31: Examples&Sollutions

Practice Exercises 19

checkpoints were performed, the faster the recovery time is from adisk crash.

6.6 Explain the concept of transaction atomicity.Answer: A transaction is a series of read and write operations uponsome data followed by a commit operation. If the series of operations ina transaction cannot be completed, the transaction must be aborted andthe operations that did take place must be rolled back. It is importantthat the series of operations in a transaction appear as one indivisibleoperation to ensure the integrity of the data being updated. Otherwise,data could be compromised if operations from two (or more) differenttransactions were intermixed.

6.7 Show that some schedules are possible under the two-phase lockingprotocol but not possible under the timestamp protocol, and vice versa.Answer: A schedule that is allowed in the two-phase locking protocolbut not in the timestamp protocol is:

step T0 T1 Precedence1 lock-S(A)2 read(A)3 lock-X(B)4 write(B)5 unlock(B)6 lock-S(B)7 read(B) T1 → T08 unlock(A)9 unlock(B)

This schedule is not allowed in the timestamp protocol because at step7, the W-timestamp of B is 1.A schedule that is allowed in the timestamp protocol but not in thetwo-phase locking protocol is:

step T0 T1 T21 write(A)2 write(A)3 write(A)4 write(B)5 write(B)

This schedule cannot have lock instructions added to make it legal undertwo-phase locking protocol because T1 must unlock (A) between steps 2and 3, and must lock (B) between steps 4 and 5.

6.8 The wait() statement in all Java program examples was part of a whileloop. Explain why you would always need to use a while statementwhen using wait() and why you would never use an if statement.Answer: This is an important issue to emphasize! Java only providesanonymous notification—you cannot notify a certain thread that a cer-

Page 32: Examples&Sollutions

20 Chapter 6 Process Synchronization

tain condition is true. When a thread is notified, it is its responsibilityto re-check the condition that it is waiting for. If a thread did not re-check the condition, it might have received the notification without thecondition having been met.

Page 33: Examples&Sollutions

7C H A P T E R

Deadlocks

Practice Exercises

7.1 List three examples of deadlocks that are not related to a computer-system environment.

7.2 Suppose that a system is in an unsafe state. Show that it is possible forthe processes to complete their execution without entering a deadlockstate.

7.3 Prove that the safety algorithm presented in Section 7.5.3 requires anorder of m × n2 operations.

7.4 Consider a computer system that runs 5,000 jobs per month with nodeadlock-prevention or deadlock-avoidance scheme. Deadlocks occurabout twice per month, and the operator must terminate and rerun about10 jobs per deadlock. Each job is worth about $2 (in CPU time), and thejobs terminated tend to be about half-done when they are aborted.

A systems programmer has estimated that a deadlock-avoidancealgorithm (like the banker’s algorithm) could be installed in the systemwith an increase in the average execution time per job of about 10 percent.Since the machine currently has 30-percent idle time, all 5,000 jobs permonth could still be run, although turnaround time would increase byabout 20 percent on average.

a. What are the arguments for installing the deadlock-avoidancealgorithm?

b. What are the arguments against installing the deadlock-avoidancealgorithm?

7.5 Can a system detect that some of its processes are starving? If you answer“yes,” explain how it can. If you answer “no,” explain how the systemcan deal with the starvation problem.

7.6 Consider the following resource-allocation policy. Requests and releasesfor resources are allowed at any time. If a request for resources cannot

13

Page 34: Examples&Sollutions

14 Chapter 7 Deadlocks

be satisfied because the resources are not available, then we check anyprocesses that are blocked, waiting for resources. If they have the desiredresources, then these resources are taken away from them and are givento the requesting process. The vector of resources for which the processis waiting is increased to include the resources that were taken away.

For example, consider a system with three resource types and thevector Available initialized to (4,2,2). If process P0 asks for (2,2,1), it getsthem. If P1 asks for (1,0,1), it gets them. Then, if P0 asks for (0,0,1), itis blocked (resource not available). If P2 now asks for (2,0,0), it gets theavailable one (1,0,0) and one that was allocated to P0 (since P0 is blocked).P0’s Allocation vector goes down to (1,2,1) and its Need vector goes up to(1,0,1).

a. Can deadlock occur? If you answer “yes”, give an example. If youanswer “no,” specify which necessary condition cannot occur.

b. Can indefinite blocking occur? Explain your answer.

7.7 Suppose that you have coded the deadlock-avoidance safety algorithmand now have been asked to implement the deadlock-detection algo-rithm. Can you do so by simply using the safety algorithm code andredefining Maxi = Waitingi + Allocationi , where Waitingi is a vectorspecifying the resources process i is waiting for, and Allocationi is asdefined in Section 7.5? Explain your answer.

7.8 Is it possible to have a deadlock involving only one single process?Explain your answer.

Page 35: Examples&Sollutions

7C H A P T E R

Deadlocks

Practice Exercises

7.1 List three examples of deadlocks that are not related to a computer-system environment.Answer:

• Two cars crossing a single-lane bridge from opposite directions.

• A person going down a ladder while another person is climbing upthe ladder.

• Two trains traveling toward each other on the same track.

• Two carpenters who must pound nails. There is a single hammerand a single bucket of nails. Deadlock occurs if one carpenter hasthe hammer and the other carpenter has the nails.

7.2 Suppose that a system is in an unsafe state. Show that it is possible forthe processes to complete their execution without entering a deadlockstate.Answer: An unsafe state may not necessarily lead to deadlock, it justmeans that we cannot guarantee that deadlock will not occur. Thus, itis possible that a system in an unsafe state may still allow all processesto complete without deadlock occurring. Consider the situation wherea system has 12 resources allocated among processes P0, P1, and P2. Theresources are allocated according to the following policy:

Max Current NeedP0 10 5 5P1 4 2 2P2 9 3 6

21

Page 36: Examples&Sollutions

22 Chapter 7 Deadlocks

for (int i = 0; i < n; i++) {// first find a thread that can finish

for (int j = 0; j < n; j++) {if (!finish[j]) {

boolean temp = true;

for (int k = 0; k < m; k++) {if (need[j][k] > work[k])

temp = false;

}

if (temp) { // if this thread can finish

finish[j] = true;

for (int x = 0; x < m; x++)

work[x] += work[j][x];

}}

}}

Figure 7.1 Banker’s algorithm safety algorithm.

Currently there are two resources available. This system is in an un-safe state as process P1 could complete, thereby freeing a total of fourresources. But we cannot guarantee that processes P0 and P2 can com-plete. However, it is possible that a process may release resources beforerequesting any further. For example, process P2 could release a resource,thereby increasing the total number of resources to five. This allows pro-cess P0 to complete, which would free a total of nine resources, therebyallowing process P2 to complete as well.

7.3 Prove that the safety algorithm presented in Section 7.5.3 requires anorder of m × n2 operations.Answer:Figure 7.1 provides Java code that implement the safety algorithm ofthe banker’s algorithm (the complete implementation of the banker’salgorithm is available with the source code download).As can be seen, the nested outer loops—both of which loop through ntimes—provide the n2 performance. Within these outer loops are twosequential inner loops which loop m times. The big-oh of this algorithmis therefore O(m × n2).

7.4 Consider a computer system that runs 5,000 jobs per month with nodeadlock-prevention or deadlock-avoidance scheme. Deadlocks occurabout twice per month, and the operator must terminate and rerun about10 jobs per deadlock. Each job is worth about $2 (in CPU time), and thejobs terminated tend to be about half-done when they are aborted.

A systems programmer has estimated that a deadlock-avoidancealgorithm (like the banker’s algorithm) could be installed in the systemwith an increase in the average execution time per job of about 10 percent.Since the machine currently has 30-percent idle time, all 5,000 jobs permonth could still be run, although turnaround time would increase byabout 20 percent on average.

Page 37: Examples&Sollutions

Practice Exercises 23

a. What are the arguments for installing the deadlock-avoidancealgorithm?

b. What are the arguments against installing the deadlock-avoidancealgorithm?

Answer: An argument for installing deadlock avoidance in the systemis that we could ensure deadlock would never occur. In addition, despitethe increase in turnaround time, all 5,000 jobs could still run.An argument against installing deadlock avoidance software is thatdeadlocks occur infrequently and they cost little when they do occur.

7.5 Can a system detect that some of its processes are starving? If you answer“yes,” explain how it can. If you answer “no,” explain how the systemcan deal with the starvation problem.Answer: Starvation is a difficult topic to define as it may mean differentthings for different systems. For the purposes of this question, we willdefine starvation as the situation whereby a process must wait beyonda reasonable period of time—perhaps indefinitely—before receiving arequested resource. One way of detecting starvation would be to firstidentify a period of time—T —that is considered unreasonable. When aprocess requests a resource, a timer is started. If the elapsed time exceedsT , then the process is considered to be starved.One strategy for dealing with starvation would be to adopt a policywhere resources are assigned only to the process that has been waitingthe longest. For example, if process Pa has been waiting longer for re-source X than process Pb , the request from process Pb would be deferreduntil process Pa ’s request has been satisfied.Another strategy would be less strict than what was just mentioned. Inthis scenario, a resource might be granted to a process that has waited lessthan another process, providing that the other process is not starving.However, if another process is considered to be starving, its requestwould be satisfied first.

7.6 Consider the following resource-allocation policy. Requests and releasesfor resources are allowed at any time. If a request for resources cannotbe satisfied because the resources are not available, then we check anyprocesses that are blocked, waiting for resources. If they have the desiredresources, then these resources are taken away from them and are givento the requesting process. The vector of resources for which the processis waiting is increased to include the resources that were taken away.

For example, consider a system with three resource types and thevector Available initialized to (4,2,2). If process P0 asks for (2,2,1), it getsthem. If P1 asks for (1,0,1), it gets them. Then, if P0 asks for (0,0,1), itis blocked (resource not available). If P2 now asks for (2,0,0), it gets theavailable one (1,0,0) and one that was allocated to P0 (since P0 is blocked).P0’s Allocation vector goes down to (1,2,1) and its Need vector goes up to(1,0,1).

a. Can deadlock occur? If you answer “yes”, give an example. If youanswer “no,” specify which necessary condition cannot occur.

b. Can indefinite blocking occur? Explain your answer.

Page 38: Examples&Sollutions

24 Chapter 7 Deadlocks

Answer:

a. Deadlock cannot occur because preemption exists.

b. Yes. A process may never acquire all the resources it needs if theyare continuously preempted by a series of requests such as thoseof process C.

7.7 Suppose that you have coded the deadlock-avoidance safety algorithmand now have been asked to implement the deadlock-detection algo-rithm. Can you do so by simply using the safety algorithm code andredefining Maxi = Waitingi + Allocationi , where Waitingi is a vectorspecifying the resources process i is waiting for, and Allocationi is asdefined in Section 7.5? Explain your answer.Answer:Yes. The Max vector represents the maximum request a process maymake. When calculating the safety algorithm we use the Need matrix,which represents Max — Allocation. Another way to think of this is Max= Need + Allocation. According to the question, the Waiting matrix fulfillsa role similar to the Need matrix, therefore Max = Waiting + Allocation.

7.8 Is it possible to have a deadlock involving only one single process?Explain your answer.Answer: No. This follows directly from the hold-and-wait condition.

Page 39: Examples&Sollutions

8C H A P T E R

MemoryManagement

Practice Exercises

8.1 Name two differences between logical and physical addresses.

8.2 Consider a system in which a program can be separated into two parts:code and data. The CPU knows whether it wants an instruction (in-struction fetch) or data (data fetch or store). Therefore, two base–limitregister pairs are provided: one for instructions and one for data. Theinstruction base–limit register pair is automatically read-only, so pro-grams can be shared among different users. Discuss the advantages anddisadvantages of this scheme.

8.3 Why are page sizes always powers of 2?

8.4 Consider a logical address space of eight pages of 1024 words each,mapped onto a physical memory of 32 frames.

a. How many bits are there in the logical address?

b. How many bits are there in the physical address?

8.5 What is the effect of allowing two entries in a page table to point to thesame page frame in memory? Explain how this effect could be used todecrease the amount of time needed to copy a large amount of memoryfrom one place to another. What effect would updating some byte on theone page have on the other page?

8.6 Describe a mechanism by which one segment could belong to the addressspace of two different processes.

8.7 Sharing segments among processes without requiring the same segmentnumber is possible in a dynamically linked segmentation system.

a. Define a system that allows static linking and sharing of segmentswithout requiring that the segment numbers be the same.

b. Describe a paging scheme that allows pages to be shared withoutrequiring that the page numbers be the same.

15

Page 40: Examples&Sollutions

16 Chapter 8 Memory Management

8.8 In the IBM/370, memory protection is provided through the use of keys.A key is a 4-bit quantity. Each 2K block of memory has a key (the storagekey) associated with it. The CPU also has a key (the protection key)associated with it. A store operation is allowed only if both keys areequal, or if either is zero. Which of the following memory-managementschemes could be used successfully with this hardware?

a. Bare machine

b. Single-user system

c. Multiprogramming with a fixed number of processes

d. Multiprogramming with a variable number of processes

e. Paging

f. Segmentation

Page 41: Examples&Sollutions

8C H A P T E R

MemoryManagement

Practice Exercises

8.1 Name two differences between logical and physical addresses.Answer: A logical address does not refer to an actual existing address;rather, it refers to an abstract address in an abstract address space. Con-trast this with a physical address that refers to an actual physical addressin memory. A logical address is generated by the CPU and is translatedinto a physical address by the memory management unit(MMU). There-fore, physical addresses are generated by the MMU.

8.2 Consider a system in which a program can be separated into two parts:code and data. The CPU knows whether it wants an instruction (in-struction fetch) or data (data fetch or store). Therefore, two base–limitregister pairs are provided: one for instructions and one for data. Theinstruction base–limit register pair is automatically read-only, so pro-grams can be shared among different users. Discuss the advantages anddisadvantages of this scheme.Answer: The major advantage of this scheme is that it is an effectivemechanism for code and data sharing. For example, only one copy of aneditor or a compiler needs to be kept in memory, and this code can beshared by all processes needing access to the editor or compiler code.Another advantage is protection of code against erroneous modification.The only disadvantage is that the code and data must be separated,which is usually adhered to in a compiler-generated code.

8.3 Why are page sizes always powers of 2?Answer: Recall that paging is implemented by breaking up an addressinto a page and offset number. It is most efficient to break the addressinto X page bits and Y offset bits, rather than perform arithmetic onthe address to calculate the page number and offset. Because each bit

25

Page 42: Examples&Sollutions

26 Chapter 8 Memory Management

position represents a power of 2, splitting an address between bits resultsin a page size that is a power of 2.

8.4 Consider a logical address space of eight pages of 1024 words each,mapped onto a physical memory of 32 frames.

a. How many bits are there in the logical address?

b. How many bits are there in the physical address?

Answer:

a. Logical address: 13 bits

b. Physical address: 15 bits

8.5 What is the effect of allowing two entries in a page table to point to thesame page frame in memory? Explain how this effect could be used todecrease the amount of time needed to copy a large amount of memoryfrom one place to another. What effect would updating some byte on theone page have on the other page?Answer: By allowing two entries in a page table to point to the samepage frame in memory, users can share code and data. If the code is reen-trant, much memory space can be saved through the shared use of largeprograms such as text editors, compilers, and database systems. “Copy-ing” large amounts of memory could be effected by having differentpage tables point to the same memory location.However, sharing of nonreentrant code or data means that any userhaving access to the code can modify it and these modifications wouldbe reflected in the other user’s “copy.”

8.6 Describe a mechanism by which one segment could belong to the addressspace of two different processes.Answer: Since segment tables are a collection of base–limit registers,segments can be shared when entries in the segment table of two differentjobs point to the same physical location. The two segment tables musthave identical base pointers, and the shared segment number must bethe same in the two processes.

8.7 Sharing segments among processes without requiring the same segmentnumber is possible in a dynamically linked segmentation system.

a. Define a system that allows static linking and sharing of segmentswithout requiring that the segment numbers be the same.

b. Describe a paging scheme that allows pages to be shared withoutrequiring that the page numbers be the same.

Answer: Both of these problems reduce to a program being able toreference both its own code and its data without knowing the segment orpage number associated with the address. MULTICS solved this problemby associating four registers with each process. One register had theaddress of the current program segment, another had a base address forthe stack, another had a base address for the global data, and so on. Theidea is that all references have to be indirect through a register that mapsto the current segment or page number. By changing these registers, the

Page 43: Examples&Sollutions

Practice Exercises 27

same code can execute for different processes without the same page orsegment numbers.

8.8 In the IBM/370, memory protection is provided through the use of keys.A key is a 4-bit quantity. Each 2K block of memory has a key (the storagekey) associated with it. The CPU also has a key (the protection key)associated with it. A store operation is allowed only if both keys areequal, or if either is zero. Which of the following memory-managementschemes could be used successfully with this hardware?

a. Bare machine

b. Single-user system

c. Multiprogramming with a fixed number of processes

d. Multiprogramming with a variable number of processes

e. Paging

f. Segmentation

Answer:

a. Protection not necessary, set system key to 0.

b. Set system key to 0 when in supervisor mode.

c. Region sizes must be fixed in increments of 2k bytes, allocate keywith memory blocks.

d. Same as above.

e. Frame sizes must be in increments of 2k bytes, allocate key withpages.

f. Segment sizes must be in increments of 2k bytes, allocate key withsegments.

Page 44: Examples&Sollutions
Page 45: Examples&Sollutions

9C H A P T E R

VirtualMemory

Practice Exercises

9.1 Under what circumstances do page faults occur? Describe the actionstaken by the operating system when a page fault occurs.

9.2 Assume that you have a page-reference string for a process with mframes (initially all empty). The page-reference string has length p; ndistinct page numbers occur in it. Answer these questions for any page-replacement algorithms:

a. What is a lower bound on the number of page faults?

b. What is an upper bound on the number of page faults?

9.3 Which of the following programming techniques and structures are“good” for a demand-paged environment ? Which are “not good”? Ex-plain your answers.

a. Stack

b. Hashed symbol table

c. Sequential search

d. Binary search

e. Pure code

f. Vector operations

g. Indirection

9.4 Consider the following page-replacement algorithms. Rank these al-gorithms on a five-point scale from “bad” to “perfect” according totheir page-fault rate. Separate those algorithms that suffer from Belady’sanomaly from those that do not.

a. LRU replacement

17

Page 46: Examples&Sollutions

18 Chapter 9 Virtual Memory

b. FIFO replacement

c. Optimal replacement

d. Second-chance replacement

9.5 When virtual memory is implemented in a computing system, there arecertain costs associated with the technique and certain benefits. List thecosts and the benefits. Is it possible for the costs to exceed the benefits?If it is, what measures can be taken to ensure that this does not happen?

9.6 An operating system supports a paged virtual memory, using a centralprocessor with a cycle time of 1 microsecond. It costs an additional 1microsecond to access a page other than the current one. Pages have 1000words, and the paging device is a drum that rotates at 3000 revolutionsper minute and transfers 1 million words per second. The followingstatistical measurements were obtained from the system:

• 1 percent of all instructions executed accessed a page other than thecurrent page.

• Of the instructions that accessed another page, 80 percent accessed apage already in memory.

• When a new page was required, the replaced page was modified 50percent of the time.

Calculate the effective instruction time on this system, assuming that thesystem is running one process only and that the processor is idle duringdrum transfers.

9.7 Consider the two-dimensional array A:

int A[][] = new int[100][100];

where A[0][0] is at location 200, in a paged memory system with pagesof size 200. A small process is in page 0 (locations 0 to 199) for ma-nipulating the matrix; thus, every instruction fetch will be from page0.

For three page frames, how many page faults are generated by the fol-lowing array-initialization loops, using LRU replacement, and assumingpage frame 1 has the process in it, and the other two are initially empty?

a. for (int j = 0; j < 100; j++)for (int i = 0; i < 100; i++)A[i][j] = 0;

b. for (int i = 0; i < 100; i++)for (int j = 0; j < 100; j++)A[i][j] = 0;

9.8 Consider the following page reference string:

1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6.

How many page faults would occur for the following replacement al-gorithms, assuming one, two, three, four, five, six, or seven frames?

Page 47: Examples&Sollutions

Exercises 19

Remember all frames are initially empty, so your first unique pages willall cost one fault each.

• LRU replacement

• FIFO replacement

• Optimal replacement

9.9 Suppose that you want to use a paging algorithm that requires a refer-ence bit (such as second-chance replacement or working-set model), butthe hardware does not provide one. Sketch how you could simulate areference bit even if one were not provided by the hardware, or explainwhy it is not possible to do so. If it is possible, calculate what the costwould be.

9.10 You have devised a new page-replacement algorithm that you think maybe optimal. In some contorted test cases, Belady’s anomaly occurs. Is thenew algorithm optimal? Explain your answer.

9.11 Segmentation is similar to paging but uses variable-sized “pages.” De-fine two segment-replacement algorithms based on FIFO and LRU page-replacement schemes. Remember that since segments are not the samesize, the segment that is chosen to be replaced may not be big enoughto leave enough consecutive locations for the needed segment. Considerstrategies for systems where segments cannot be relocated, and thosefor systems where they can.

9.12 Consider a demand-paged computer system where the degree of mul-tiprogramming is currently fixed at four. The system was recently mea-sured to determine utilization of CPU and the paging disk. The resultsare one of the following alternatives. For each case, what is happening?Can the degree of multiprogramming be increased to increase the CPUutilization? Is the paging helping?

a. CPU utilization 13 percent; disk utilization 97 percent

b. CPU utilization 87 percent; disk utilization 3 percent

c. CPU utilization 13 percent; disk utilization 3 percent

9.13 We have an operating system for a machine that uses base and limitregisters, but we have modified the machine to provide a page table.Can the page tables be set up to simulate base and limit registers? Howcan they be, or why can they not be?

Page 48: Examples&Sollutions
Page 49: Examples&Sollutions

9C H A P T E R

VirtualMemory

Practice Exercises

9.1 Under what circumstances do page faults occur? Describe the actionstaken by the operating system when a page fault occurs.Answer: A page fault occurs when an access to a page that has not beenbrought into main memory takes place. The operating system verifiesthe memory access, aborting the program if it is invalid. If it is valid, afree frame is located and I/O is requested to read the needed page intothe free frame. Upon completion of I/O, the process table and page tableare updated and the instruction is restarted.

9.2 Assume that you have a page-reference string for a process with mframes (initially all empty). The page-reference string has length p; ndistinct page numbers occur in it. Answer these questions for any page-replacement algorithms:

a. What is a lower bound on the number of page faults?

b. What is an upper bound on the number of page faults?

Answer:

a. n

b. p

9.3 Which of the following programming techniques and structures are“good” for a demand-paged environment ? Which are “not good”? Ex-plain your answers.

a. Stack

b. Hashed symbol table

29

Page 50: Examples&Sollutions

30 Chapter 9 Virtual Memory

c. Sequential search

d. Binary search

e. Pure code

f. Vector operations

g. Indirection

Answer:

a. Stack—good.

b. Hashed symbol table—not good.

c. Sequential search—good.

d. Binary search—not good.

e. Pure code—good.

f. Vector operations—good.

g. Indirection—not good.

9.4 Consider the following page-replacement algorithms. Rank these al-gorithms on a five-point scale from “bad” to “perfect” according totheir page-fault rate. Separate those algorithms that suffer from Belady’sanomaly from those that do not.

a. LRU replacement

b. FIFO replacement

c. Optimal replacement

d. Second-chance replacement

Answer:

Rank Algorithm Suffer from Belady’s anomaly1 Optimal no2 LRU no3 Second-chance yes4 FIFO yes

9.5 When virtual memory is implemented in a computing system, there arecertain costs associated with the technique and certain benefits. List thecosts and the benefits. Is it possible for the costs to exceed the benefits?If it is, what measures can be taken to ensure that this does not happen?Answer: The costs are additional hardware and slower access time. Thebenefits are good utilization of memory and larger logical address spacethan physical address space.

9.6 An operating system supports a paged virtual memory, using a centralprocessor with a cycle time of 1 microsecond. It costs an additional 1microsecond to access a page other than the current one. Pages have 1000

Page 51: Examples&Sollutions

Practice Exercises 31

words, and the paging device is a drum that rotates at 3000 revolutionsper minute and transfers 1 million words per second. The followingstatistical measurements were obtained from the system:

• 1 percent of all instructions executed accessed a page other than thecurrent page.

• Of the instructions that accessed another page, 80 percent accessed apage already in memory.

• When a new page was required, the replaced page was modified 50percent of the time.

Calculate the effective instruction time on this system, assuming that thesystem is running one process only and that the processor is idle duringdrum transfers.Answer:

effective access time = 0.99 × (1 �sec + 0.008 × (2 �sec)+ 0.002 × (10,000 �sec + 1,000 �sec)+ 0.001 × (10,000 �sec + 1,000 �sec)

= (0.99 + 0.016 + 22.0 + 11.0) �sec= 34.0 �sec

9.7 Consider the two-dimensional array A:

int A[][] = new int[100][100];

where A[0][0] is at location 200, in a paged memory system with pagesof size 200. A small process is in page 0 (locations 0 to 199) for ma-nipulating the matrix; thus, every instruction fetch will be from page0.

For three page frames, how many page faults are generated by the fol-lowing array-initialization loops, using LRU replacement, and assumingpage frame 1 has the process in it, and the other two are initially empty?

a. for (int j = 0; j < 100; j++)for (int i = 0; i < 100; i++)A[i][j] = 0;

b. for (int i = 0; i < 100; i++)for (int j = 0; j < 100; j++)A[i][j] = 0;

Answer:

a. 50

b. 5,000

9.8 Consider the following page reference string:

1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6.

Page 52: Examples&Sollutions

32 Chapter 9 Virtual Memory

How many page faults would occur for the following replacement al-gorithms, assuming one, two, three, four, five, six, or seven frames?Remember all frames are initially empty, so your first unique pages willall cost one fault each.

• LRU replacement

• FIFO replacement

• Optimal replacement

Answer:

Number of frames LRU FIFO Optimal1 20 20 202 18 18 153 15 16 114 10 14 85 8 10 76 7 10 77 7 7 7

9.9 Suppose that you want to use a paging algorithm that requires a refer-ence bit (such as second-chance replacement or working-set model), butthe hardware does not provide one. Sketch how you could simulate areference bit even if one were not provided by the hardware, or explainwhy it is not possible to do so. If it is possible, calculate what the costwould be.Answer: You can use the valid/invalid bit supported in hardware tosimulate the reference bit. Initially set the bit to invalid. On first referencea trap to the operating system is generated. The operating system willset a software bit to 1 and reset the valid/invalid bit to valid.

9.10 You have devised a new page-replacement algorithm that you think maybe optimal. In some contorted test cases, Belady’s anomaly occurs. Is thenew algorithm optimal? Explain your answer.Answer: No. An optimal algorithm will not suffer from Belady’sanomaly because—by definition—an optimal algorithm replaces thepage that will not be used for the longest time. Belady’s anomaly occurswhen a page-replacement algorithm evicts a page that will be neededin the immediate future. An optimal algorithm would not have selectedsuch a page.

9.11 Segmentation is similar to paging but uses variable-sized “pages.” De-fine two segment-replacement algorithms based on FIFO and LRU page-replacement schemes. Remember that since segments are not the samesize, the segment that is chosen to be replaced may not be big enoughto leave enough consecutive locations for the needed segment. Considerstrategies for systems where segments cannot be relocated, and thosefor systems where they can.Answer:

Page 53: Examples&Sollutions

Practice Exercises 33

a. FIFO. Find the first segment large enough to accommodate the in-coming segment. If relocation is not possible and no one segmentis large enough, select a combination of segments whose mem-ories are contiguous, which are “closest to the first of the list”and which can accommodate the new segment. If relocation ispossible, rearrange the memory so that the first N segments largeenough for the incoming segment are contiguous in memory. Addany leftover space to the free-space list in both cases.

b. LRU. Select the segment that has not been used for the longestperiod of time and that is large enough, adding any leftover spaceto the free space list. If no one segment is large enough, selecta combination of the “oldest” segments that are contiguous inmemory (if relocation is not available) and that are large enough.If relocation is available, rearrange the oldest N segments to becontiguous in memory and replace those with the new segment.

9.12 Consider a demand-paged computer system where the degree of mul-tiprogramming is currently fixed at four. The system was recently mea-sured to determine utilization of CPU and the paging disk. The resultsare one of the following alternatives. For each case, what is happening?Can the degree of multiprogramming be increased to increase the CPUutilization? Is the paging helping?

a. CPU utilization 13 percent; disk utilization 97 percent

b. CPU utilization 87 percent; disk utilization 3 percent

c. CPU utilization 13 percent; disk utilization 3 percent

Answer:

a. Thrashing is occurring.

b. CPU utilization is sufficiently high to leave things alone, and in-crease degree of multiprogramming.

c. Increase the degree of multiprogramming.

9.13 We have an operating system for a machine that uses base and limitregisters, but we have modified the machine to provide a page table.Can the page tables be set up to simulate base and limit registers? Howcan they be, or why can they not be?Answer: The page table can be set up to simulate base and limit registersprovided that the memory is allocated in fixed-size segments. In thisway, the base of a segment can be entered into the page table and thevalid/invalid bit used to indicate that portion of the segment as residentin the memory. There will be some problem with internal fragmentation.

Page 54: Examples&Sollutions
Page 55: Examples&Sollutions

10C H A P T E R

File-SystemInterface

Practice Exercises

10.1 Some systems automatically delete all user files when a user logs off ora job terminates, unless the user explicitly requests that they be kept;other systems keep all files unless the user explicitly deletes them.Discuss the relative merits of each approach.

10.2 Why do some systems keep track of the type of a file, while others leaveit to the user or simply do not implement multiple file types? Whichsystem is “better?”

10.3 Similarly, some systems support many types of structures for a file’sdata, while others simply support a stream of bytes. What are theadvantages and disadvantages?

10.4 Could you simulate a multilevel directory structure with a single-leveldirectory structure in which arbitrarily long names can be used? If youranswer is yes, explain how you can do so, and contrast this scheme withthe multilevel directory scheme. If your answer is no, explain whatprevents your simulation’s success. How would your answer changeif file names were limited to seven characters?

10.5 Explain the purpose of the open() and close() operations.

10.6 Give an example of an application in which data in a file should beaccessed in the following order:

a. Sequentially

b. Randomly

10.7 In some systems, a subdirectory can be read and written by an autho-rized user, just as ordinary files can be.

a. Describe the protection problems that could arise.

b. Suggest a scheme for dealing with each of the protection prob-lems you named in part a.

21

Page 56: Examples&Sollutions

22 Chapter 10 File-System Interface

10.8 Consider a system that supports 5000 users. Suppose that you want toallow 4990 of these users to be able to access one file.

a. How would you specify this protection scheme in UNIX?

b. Could you suggest another protection scheme that can be usedmore effectively for this purpose than the scheme provided byUNIX?

10.9 Researchers have suggested that, instead of having an access list associ-ated with each file (specifying which users can access the file, and how),we should have a user control list associated with each user (specifyingwhich files a user can access, and how). Discuss the relative merits ofthese two schemes.

Page 57: Examples&Sollutions

10C H A P T E R

File-SystemInterface

Practice Exercises

10.1 Some systems automatically delete all user files when a user logs off ora job terminates, unless the user explicitly requests that they be kept;other systems keep all files unless the user explicitly deletes them.Discuss the relative merits of each approach.Answer: Deleting all files not specifically saved by the user has theadvantage of minimizing the file space needed for each user by notsaving unwanted or unnecessary files. Saving all files unless specificallydeleted is more secure for the user in that it is not possible to lose filesinadvertently by forgetting to save them.

10.2 Why do some systems keep track of the type of a file, while others leaveit to the user or simply do not implement multiple file types? Whichsystem is “better?”Answer: Some systems allow different file operations based on thetype of the file (for instance, an ascii file can be read as a stream while adatabase file can be read via an index to a block). Other systems leavesuch interpretation of a file’s data to the process and provide no help inaccessing the data. The method that is “better” depends on the needsof the processes on the system, and the demands the users place on theoperating system. If a system runs mostly database applications, it maybe more efficient for the operating system to implement a database-type file and provide operations, rather than making each programimplement the same thing (possibly in different ways). For general-purpose systems it may be better to only implement basic file types tokeep the operating system size smaller and allow maximum freedomto the processes on the system.

35

Page 58: Examples&Sollutions

36 Chapter 10 File-System Interface

10.3 Similarly, some systems support many types of structures for a file’sdata, while others simply support a stream of bytes. What are theadvantages and disadvantages?Answer: An advantage of having the system support different filestructures is that the support comes from the system; individual ap-plications are not required to provide the support. In addition, if thesystem provides the support for different file structures, it can imple-ment the support presumably more efficiently than an application.The disadvantage of having the system provide support for defined filetypes is that it increases the size of the system. In addition, applicationsthat may require different file types other than what is provided by thesystem may not be able to run on such systems.An alternative strategy is for the operating system to define no supportfor file structures and instead treat all files as a series of bytes. This isthe approach taken by UNIX systems. The advantage of this approachis that it simplifies the operating system support for file systems, as thesystem no longer has to provide the structure for different file types.Furthermore, it allows applications to define file structures, thereby al-leviating the situation where a system may not provide a file definitionrequired for a specific application.

10.4 Could you simulate a multilevel directory structure with a single-leveldirectory structure in which arbitrarily long names can be used? If youranswer is yes, explain how you can do so, and contrast this scheme withthe multilevel directory scheme. If your answer is no, explain whatprevents your simulation’s success. How would your answer changeif file names were limited to seven characters?Answer: If arbitrarily long names can be used then it is possible tosimulate a multilevel directory structure. This can be done, for example,by using the character “.” to indicate the end of a subdirectory. Thus, forexample, the name jim.java.F1 specifies that F1 is a file in subdirectoryjava which in turn is in the root directory jim.If file names were limited to seven characters, then the above schemecould not be utilized and thus, in general, the answer is no. The next bestapproach in this situation would be to use a specific file as a symboltable (directory) to map arbitrarily long names (such as jim.java.F1)into shorter arbitrary names (such as XX00743), which are then usedfor actual file access.

10.5 Explain the purpose of the open() and close() operations.Answer:

• The open() operation informs the system that the named file isabout to become active.

• The close() operation informs the system that the named file isno longer in active use by the user who issued the close operation.

10.6 Give an example of an application in which data in a file should beaccessed in the following order:

a. Sequentially

Page 59: Examples&Sollutions

Practice Exercises 37

b. Randomly

Answer:

a. Print the content of the file.

b. Print the content of record i. This record can be found usinghashing or index techniques.

10.7 In some systems, a subdirectory can be read and written by an autho-rized user, just as ordinary files can be.

a. Describe the protection problems that could arise.

b. Suggest a scheme for dealing with each of the protection prob-lems you named in part a.

Answer:

a. One piece of information kept in a directory entry is file location.If a user could modify this location, then he could access otherfiles defeating the access-protection scheme.

b. Do not allow the user to directly write onto the subdirectory.Rather, provide system operations to do so.

10.8 Consider a system that supports 5000 users. Suppose that you want toallow 4990 of these users to be able to access one file.

a. How would you specify this protection scheme in UNIX?

b. Could you suggest another protection scheme that can be usedmore effectively for this purpose than the scheme provided byUNIX?

Answer:

a. There are two methods for achieving this:

1. Create an access control list with the names of all 4990users.

2. Put these 4990 users in one group and set the group accessaccordingly. This scheme cannot always be implementedsince user groups are restricted by the system.

b. The universal access to files applies to all users unless their nameappears in the access-control list with different access permis-sion. With this scheme you simply put the names of the re-maining ten users in the access control list but with no accessprivileges allowed.

10.9 Researchers have suggested that, instead of having an access list associ-ated with each file (specifying which users can access the file, and how),we should have a user control list associated with each user (specifyingwhich files a user can access, and how). Discuss the relative merits ofthese two schemes.Answer:

Page 60: Examples&Sollutions

38 Chapter 10 File-System Interface

• File control list. Since the access control information is concentratedin one single place, it is easier to change access control informationand this requires less space.

• User control list. This requires less overhead when opening a file.

Page 61: Examples&Sollutions

11C H A P T E R

File-SystemImplementation

Practice Exercises

11.1 Consider a file currently consisting of 100 blocks. Assume that thefile control block (and the index block, in the case of indexed alloca-tion) is already in memory. Calculate how many disk I/O operationsare required for contiguous, linked, and indexed (single-level) alloca-tion strategies, if, for one block, the following conditions hold. In thecontiguous-allocation case, assume that there is no room to grow in thebeginning, but there is room to grow in the end. Assume that the blockinformation to be added is stored in memory.

a. The block is added at the beginning.

b. The block is added in the middle.

c. The block is added at the end.

d. The block is removed from the beginning.

e. The block is removed from the middle.

f. The block is removed from the end.

11.2 What problems could occur if a system allowed a file system to bemounted simultaneously at more than one location?

11.3 Why must the bit map for file allocation be kept on mass storage, ratherthan in main memory?

11.4 Consider a system that supports the strategies of contiguous, linked,and indexed allocation. What criteria should be used in deciding whichstrategy is best utilized for a particular file?

11.5 One problem with contiguous allocation is that the user must preallo-cate enough space for each file. If the file grows to be larger than thespace allocated for it, special actions must be taken. One solution tothis problem is to define a file structure consisting of an initial con-tiguous area (of a specified size). If this area is filled, the operating

23

Page 62: Examples&Sollutions

24 Chapter 11 File-System Implementation

system automatically defines an overflow area that is linked to the ini-tial contiguous area. If the overflow area is filled, another overflow areais allocated. Compare this implementation of a file with the standardcontiguous and linked implementations.

11.6 How do caches help improve performance? Why do systems not usemore or larger caches if they are so useful?

11.7 Why is it advantageous for the user for an operating system to dynami-cally allocate its internal tables? What are the penalties to the operatingsystem for doing so?

11.8 Explain how the VFS layer allows an operating system easily to supportmultiple types of file systems.

Page 63: Examples&Sollutions

11C H A P T E R

File-SystemImplementation

Practice Exercises

11.1 Consider a file currently consisting of 100 blocks. Assume that thefile control block (and the index block, in the case of indexed alloca-tion) is already in memory. Calculate how many disk I/O operationsare required for contiguous, linked, and indexed (single-level) alloca-tion strategies, if, for one block, the following conditions hold. In thecontiguous-allocation case, assume that there is no room to grow in thebeginning, but there is room to grow in the end. Assume that the blockinformation to be added is stored in memory.

a. The block is added at the beginning.

b. The block is added in the middle.

c. The block is added at the end.

d. The block is removed from the beginning.

e. The block is removed from the middle.

f. The block is removed from the end.

Answer:

Contiguous Linked Indexeda. 201 1 1b. 101 52 1c. 1 3 1d. 198 1 0e. 98 52 0f. 0 100 0

39

Page 64: Examples&Sollutions

40 Chapter 11 File-System Implementation

11.2 What problems could occur if a system allowed a file system to bemounted simultaneously at more than one location?Answer: There would be multiple paths to the same file, which couldconfuse users or encourage mistakes (deleting a file with one pathdeletes the file in all the other paths).

11.3 Why must the bit map for file allocation be kept on mass storage, ratherthan in main memory?Answer: In case of system crash (memory failure) the free-space listwould not be lost as it would be if the bit map had been stored in mainmemory.

11.4 Consider a system that supports the strategies of contiguous, linked,and indexed allocation. What criteria should be used in deciding whichstrategy is best utilized for a particular file?Answer:

• Contiguous—if file is usually accessed sequentially, if file isrelatively small.

• Linked—if file is large and usually accessed sequentially.

• Indexed—if file is large and usually accessed randomly.

11.5 One problem with contiguous allocation is that the user must preallo-cate enough space for each file. If the file grows to be larger than thespace allocated for it, special actions must be taken. One solution tothis problem is to define a file structure consisting of an initial con-tiguous area (of a specified size). If this area is filled, the operatingsystem automatically defines an overflow area that is linked to the ini-tial contiguous area. If the overflow area is filled, another overflow areais allocated. Compare this implementation of a file with the standardcontiguous and linked implementations.Answer: This method requires more overhead then the standard con-tiguous allocation. It requires less overhead than the standard linkedallocation.

11.6 How do caches help improve performance? Why do systems not usemore or larger caches if they are so useful?Answer: Caches allow components of differing speeds to communicatemore efficiently by storing data from the slower device, temporarily,in a faster device (the cache). Caches are, almost by definition, moreexpensive than the device they are caching for, so increasing the numberor size of caches would increase system cost.

11.7 Why is it advantageous for the user for an operating system to dynami-cally allocate its internal tables? What are the penalties to the operatingsystem for doing so?Answer: Dynamic tables allow more flexibility in system use growth —tables are never exceeded, avoiding artificial use limits. Unfortunately,kernel structures and code are more complicated, so there is morepotential for bugs. The use of one resource can take away more systemresources (by growing to accommodate the requests) than with statictables.

Page 65: Examples&Sollutions

Practice Exercises 41

11.8 Explain how the VFS layer allows an operating system easily to supportmultiple types of file systems.Answer: VFS introduces a layer of indirection in the file system imple-mentation. In many ways, it is similar to object-oriented programmingtechniques. System calls can be made generically (independent of filesystem type). Each file system type provides its function calls and datastructures to the VFS layer. A system call is translated into the properspecific functions for the target file system at the VFS layer. The callingprogram has no file-system-specific code, and the upper levels of thesystem call structures likewise are file system-independent. The trans-lation at the VFS layer turns these generic calls into file-system-specificoperations.

Page 66: Examples&Sollutions
Page 67: Examples&Sollutions

12C H A P T E RMassStorageStructure

Practice Exercises

12.1 The accelerating seek described in Exercise 12.3 is typical of hard-diskdrives. By contrast, floppy disks (and many hard disks manufacturedbefore the mid-1980s) typically seek at a fixed rate. Suppose that thedisk in Exercise 12.3 has a constant-rate seek rather than a constant-acceleration seek, so the seek time is of the form t = x + yL, where tis the time in milliseconds and L is the seek distance. Suppose that thetime to seek to an adjacent cylinder is 1 millisecond, as before, and is0.5 milliseconds for each additional cylinder.

a. Write an equation for this seek time as a function of the seekdistance.

b. Using the seek-time function from part a, calculate the total seektime for each of the schedules in Exercise 12.2. Is your answerthe same as it was for Exercise 12.3(c)?

c. What is the percentage speedup of the fastest schedule over FCFSin this case?

12.2 Is disk scheduling, other than FCFS scheduling, useful in a single-userenvironment? Explain your answer.

12.3 Explain why SSTF scheduling tends to favor middle cylinders over theinnermost and outermost cylinders.

12.4 Why is rotational latency usually not considered in disk scheduling?How would you modify SSTF, SCAN, and C-SCAN to include latencyoptimization?

12.5 How would use of a RAM disk affect your selection of a disk-schedulingalgorithm? What factors would you need to consider? Do the sameconsiderations apply to hard-disk scheduling, given that the file systemstores recently used blocks in a buffer cache in main memory?

25

Page 68: Examples&Sollutions

26 Chapter 12 Mass-Storage Structure

12.6 Why is it important to balance file system I/O among the disks andcontrollers on a system in a multitasking environment?

12.7 What are the tradeoffs involved in rereading code pages from the filesystem versus using swap space to store them?

12.8 Is there any way to implement truly stable storage? Explain your an-swer.

12.9 The term “fast wide SCSI-II” denotes a SCSI bus that operates at a datarate of 20 megabytes per second when it moves a packet of bytes be-tween the host and a device. Suppose that a fast wide SCSI-II disk drivespins at 7200 RPM, has a sector size of 512 bytes, and holds 160 sectorsper track.

a. Estimate the sustained transfer rate of this drive in megabytesper second.

b. Suppose that the drive has 7000 cylinders, 20 tracks per cylinder,a head switch time (from one platter to another) of 0.5 millisec-ond, and an adjacent cylinder seek time of 2 milliseconds. Usethis additional information to give an accurate estimate of thesustained transfer rate for a huge transfer.

c. Suppose that the average seek time for the drive is 8 milliseconds.Estimate the I/Os per second and the effective transfer rate fora random-access workload that reads individual sectors that arescattered across the disk.

d. Calculate the random-access I/Os per second and transfer ratefor I/O sizes of 4 kilobytes, 8 kilobytes, and 64 kilobytes.

e. If multiple requests are in the queue, a scheduling algorithm suchas SCAN should be able to reduce the average seek distance. Sup-pose that a random-access workload is reading 8-kilobyte pages,the average queue length is 10, and the scheduling algorithmreduces the average seek time to 3 milliseconds. Now calculatethe I/Os per second and the effective transfer rate of the drive.

12.10 More than one disk drive can be attached to a SCSI bus. In particular,a fast wide SCSI-II bus (see Exercise 12.9) can be connected to at most15 disk drives. Recall that this bus has a bandwidth of 20 megabytesper second. At any time, only one packet can be transferred on the busbetween some disk’s internal cache and the host. However, a disk canbe moving its disk arm while some other disk is transferring a packeton the bus. Also, a disk can be transferring data between its magneticplatters and its internal cache while some other disk is transferring apacket on the bus. Considering the transfer rates that you calculatedfor the various workloads in Exercise 12.9, discuss how many disks canbe used effectively by one fast wide SCSI-II bus.

12.11 Remapping of bad blocks by sector sparing or sector slipping couldinfluence performance. Suppose that the drive in Exercise 12.9 has atotal of 100 bad sectors at random locations and that each bad sector ismapped to a spare that is located on a different track, but within the

Page 69: Examples&Sollutions

Exercises 27

same cylinder. Estimate the number of I/Os per second and the effec-tive transfer rate for a random-access workload consisting of 8-kilobytereads, with a queue length of 1 (that is, the choice of scheduling algo-rithm is not a factor). What is the effect of a bad sector on performance?

12.12 In a disk jukebox, what would be the effect of having more open filesthan the number of drives in the jukebox?

12.13 If magnetic hard disks eventually have the same cost per gigabyte as dotapes, will tapes become obsolete, or will they still be needed? Explainyour answer.

12.14 It is sometimes said that tape is a sequential-access medium, whereasmagnetic disk is a random-access medium. In fact, the suitability ofa storage device for random access depends on the transfer size. Theterm streaming transfer rate denotes the data rate for a transfer thatis underway, excluding the effect of access latency. By contrast, theeffective transfer rate is the ratio of total bytes per total seconds, includingoverhead time such as the access latency.

Suppose that, in a computer, the level-2 cache has an access latencyof 8 nanoseconds and a streaming transfer rate of 800 megabytes persecond, the main memory has an access latency of 60 nanoseconds anda streaming transfer rate of 80 megabytes per second, the magnetic diskhas an access latency of 15 millisecond and a streaming transfer rate of5 megabytes per second, and a tape drive has an access latency of 60seconds and a streaming transfer rate of 2 megabytes per seconds.

a. Random access causes the effective transfer rate of a device todecrease, because no data are transferred during the access time.For the disk described, what is the effective transfer rate if anaverage access is followed by a streaming transfer of 512 bytes,8 kilobytes, 1 megabyte, and 16 megabytes?

b. The utilization of a device is the the ratio of effective transferrate to streaming transfer rate. Calculate the utilization of thedisk drive for random access that performs transfers in each ofthe four sizes given in part a.

c. Suppose that a utilization of 25 percent (or higher) is consideredacceptable. Using the performance figures given, compute thesmallest transfer size for disk that gives acceptable utilization.

d. Complete the following sentence: A disk is a random-access de-vice for transfers larger than bytes, and is a sequential-access device for smaller transfers.

e. Compute the minimum transfer sizes that give acceptable uti-lization for cache, memory, and tape.

f. When is a tape a random-access device, and when is it a sequential-access device?

12.15 Suppose that we agree that 1 kilobyte is 1,024 bytes, 1 megabyte is1,0242 bytes, and 1 gigabyte is 1,0243 bytes. This progression continuesthrough terabytes, petabytes, and exabytes (1,0246). There are currently

Page 70: Examples&Sollutions

28 Chapter 12 Mass-Storage Structure

several new proposed scientific projects that plan to record and store afew exabytes of data during the next decade. To answer the followingquestions, you will need to make a few reasonable assumptions; statethe assumptions that you make.

a. How many disk drives would be required to hold 4 exabytes ofdata?

b. How many magnetic tapes would be required to hold 4 exabytesof data?

c. How many optical tapes would be required to hold 4 exabytesof data (see Exercise 12.21)?

d. How many holographic storage cartridges would be required tohold 4 exabytes of data (see Exercise 12.20)?

e. How many cubic feet of storage space would each option re-quire?

Page 71: Examples&Sollutions

12C H A P T E RMassStorageStructure

Practice Exercises

12.1 The accelerating seek described in Exercise 12.3 is typical of hard-diskdrives. By contrast, floppy disks (and many hard disks manufacturedbefore the mid-1980s) typically seek at a fixed rate. Suppose that thedisk in Exercise 12.3 has a constant-rate seek rather than a constant-acceleration seek, so the seek time is of the form t = x + yL, where tis the time in milliseconds and L is the seek distance. Suppose that thetime to seek to an adjacent cylinder is 1 millisecond, as before, and is0.5 milliseconds for each additional cylinder.

a. Write an equation for this seek time as a function of the seekdistance.

b. Using the seek-time function from part a, calculate the total seektime for each of the schedules in Exercise 12.2. Is your answerthe same as it was for Exercise 12.3(c)?

c. What is the percentage speedup of the fastest schedule over FCFSin this case?

Answer:

a. t = 0.95 + 0.05L

b. FCFS 362.60; SSTF 95.80; SCAN 497.95; LOOK 174.50; C-SCAN 500.15;(and C-LOOK 176.70). SSTF is still the winner, and LOOK is therunner-up.

c. (362.60 − 95.80)/362.60 = 0.74 The percentage speedup of SSTFover FCFS is 74%, with respect to the seek time. If we include theoverhead of rotational latency and data transfer, the percentagespeedup will be less.

43

Page 72: Examples&Sollutions

44 Chapter 12 Mass-Storage Structure

12.2 Is disk scheduling, other than FCFS scheduling, useful in a single-userenvironment? Explain your answer.Answer: In a single-user environment, the I/O queue usually is empty.Requests generally arrive from a single process for one block or for asequence of consecutive blocks. In these cases, FCFS is an economicalmethod of disk scheduling. But LOOK is nearly as easy to programand will give much better performance when multiple processes areperforming concurrent I/O, such as when a Web browser retrieves datain the background while the operating system is paging and anotherapplication is active in the foreground.

12.3 Explain why SSTF scheduling tends to favor middle cylinders over theinnermost and outermost cylinders.Answer: The center of the disk is the location having the smallestaverage distance to all other tracks. Thus the disk head tends to moveaway from the edges of the disk. Here is another way to think of it. Thecurrent location of the head divides the cylinders into two groups. Ifthe head is not in the center of the disk and a new request arrives, thenew request is more likely to be in the group that includes the centerof the disk; thus, the head is more likely to move in that direction.

12.4 Why is rotational latency usually not considered in disk scheduling?How would you modify SSTF, SCAN, and C-SCAN to include latencyoptimization?Answer: Most disks do not export their rotational position informationto the host. Even if they did, the time for this information to reach thescheduler would be subject to imprecision and the time consumed bythe scheduler is variable, so the rotational position information wouldbecome incorrect. Further, the disk requests are usually given in termsof logical block numbers, and the mapping between logical blocks andphysical locations is very complex.

12.5 How would use of a RAM disk affect your selection of a disk-schedulingalgorithm? What factors would you need to consider? Do the sameconsiderations apply to hard-disk scheduling, given that the file systemstores recently used blocks in a buffer cache in main memory?Answer: Disk scheduling attempts to reduce the overhead time ofdisk head positioning. Since a RAM disk has uniform access times,scheduling is largely unnecessary. The comparison between RAM diskand the main memory disk-cache has no implications for hard-diskscheduling because we schedule only the buffer cache misses, not therequests that find their data in main memory.

12.6 Why is it important to balance file system I/O among the disks andcontrollers on a system in a multitasking environment?Answer: A system can perform only at the speed of its slowest bottle-neck. Disks or disk controllers are frequently the bottleneck in modernsystems as their individual performance cannot keep up with that ofthe CPU and system bus. By balancing I/O among disks and controllers,neither an individual disk nor a controller is overwhelmed, so thatbottleneck is avoided.

Page 73: Examples&Sollutions

Practice Exercises 45

12.7 What are the tradeoffs involved in rereading code pages from the filesystem versus using swap space to store them?Answer: If code pages are stored in swap space, they can be transferredmore quickly to main memory (because swap space allocation is tunedfor faster performance than general file system allocation). Using swapspace can require startup time if the pages are copied there at processinvocation rather than just being paged out to swap space on demand.Also, more swap space must be allocated if it is used for both code anddata pages.

12.8 Is there any way to implement truly stable storage? Explain your an-swer.Answer: Truly stable storage would never lose data. The fundamentaltechnique for stable storage is to maintain multiple copies of the data,so that if one copy is destroyed, some other copy is still available foruse. But for any scheme, we can imagine a large enough disaster thatall copies are destroyed.

12.9 The term “fast wide SCSI-II” denotes a SCSI bus that operates at a datarate of 20 megabytes per second when it moves a packet of bytes be-tween the host and a device. Suppose that a fast wide SCSI-II disk drivespins at 7200 RPM, has a sector size of 512 bytes, and holds 160 sectorsper track.

a. Estimate the sustained transfer rate of this drive in megabytesper second.

b. Suppose that the drive has 7000 cylinders, 20 tracks per cylinder,a head switch time (from one platter to another) of 0.5 millisec-ond, and an adjacent cylinder seek time of 2 milliseconds. Usethis additional information to give an accurate estimate of thesustained transfer rate for a huge transfer.

c. Suppose that the average seek time for the drive is 8 milliseconds.Estimate the I/Os per second and the effective transfer rate fora random-access workload that reads individual sectors that arescattered across the disk.

d. Calculate the random-access I/Os per second and transfer ratefor I/O sizes of 4 kilobytes, 8 kilobytes, and 64 kilobytes.

e. If multiple requests are in the queue, a scheduling algorithm suchas SCAN should be able to reduce the average seek distance. Sup-pose that a random-access workload is reading 8-kilobyte pages,the average queue length is 10, and the scheduling algorithmreduces the average seek time to 3 milliseconds. Now calculatethe I/Os per second and the effective transfer rate of the drive.

Answer:

a. The disk spins 120 times per second, and each spin transfers atrack of 80 KB. Thus, the sustained transfer rate can be approxi-mated as 9600 KB/s.

Page 74: Examples&Sollutions

46 Chapter 12 Mass-Storage Structure

b. Suppose that 100 cylinders is a huge transfer. The transfer rate istotal bytes divided by total time. Bytes: 100 cyl * 20 trk/cyl * 80KB/trk, i.e., 160,000 KB. Time: rotation time + track switch time +cylinder switch time. Rotation time is 2000 trks/120 trks per sec,i.e., 16.667 s. Track switch time is 19 switch per cyl * 100 cyl * 0.5ms, i.e., 950 ms. Cylinder switch time is 99 * 2 ms, i.e., 198 ms.Thus, the total time is 16.667 + 0.950 + 0.198, i.e., 17.815 s. (Weare ignoring any initial seek and rotational latency, which mightadd about 12 ms to the schedule, i.e. 0.1%.) Thus the transfer rateis 8981.2 KB/s. The overhead of track and cylinder switching isabout 6.5%.

c. The time per transfer is 8 ms to seek + 4.167 ms average rotationallatency + 0.052 ms (calculated from 1/(120 trk per second * 160sector per trk)) to rotate one sector past the disk head duringreading. We calculate the transfers per second as 1/(0.012219),i.e., 81.8. Since each transfer is 0.5 KB, the transfer rate is 40.9KB/s.

d. We ignore track and cylinder crossings for simplicity. For reads ofsize 4 KB, 8 KB, and 64 KB, the corresponding I/Os per second arecalculated from the seek, rotational latency, and rotational trans-fer time as in the previous item, giving (respectively) 1/(0.0126),1/(0.013), and 1/(0.019). Thus we get 79.4, 76.9, and 52.6 trans-fers per second, respectively. Transfer rates are obtained from 4,8, and 64 times these I/O rates, giving 318 KB/s, 615 KB/s, and3366 KB/s, respectively.

e. From 1/(3+4.167+0.83) we obtain 125 I/Os per second. From 8KB per I/O we obtain 1000 KB/s.

12.10 More than one disk drive can be attached to a SCSI bus. In particular,a fast wide SCSI-II bus (see Exercise 12.9) can be connected to at most15 disk drives. Recall that this bus has a bandwidth of 20 megabytesper second. At any time, only one packet can be transferred on the busbetween some disk’s internal cache and the host. However, a disk canbe moving its disk arm while some other disk is transferring a packeton the bus. Also, a disk can be transferring data between its magneticplatters and its internal cache while some other disk is transferring apacket on the bus. Considering the transfer rates that you calculatedfor the various workloads in Exercise 12.9, discuss how many disks canbe used effectively by one fast wide SCSI-II bus.Answer:For 8 KB random I/Os on a lightly loaded disk, where the random accesstime is calculated to be about 13 ms (see Exercise 12.9), the effectivetransfer rate is about 615 MB/s. In this case, 15 disks would have anaggregate transfer rate of less than 10 MB/s, which should not saturatethe bus. For 64 KB random reads to a lightly loaded disk, the transferrate is about 3.4 MB/s, so five or fewer disk drives would saturate thebus. For 8 KB reads with a large enough queue to reduce the averageseek to 3 ms, the transfer rate is about 1 MB/s, so the bus bandwidthmay be adequate to accommodate 15 disks.

Page 75: Examples&Sollutions

Practice Exercises 47

12.11 Remapping of bad blocks by sector sparing or sector slipping couldinfluence performance. Suppose that the drive in Exercise 12.9 has atotal of 100 bad sectors at random locations and that each bad sector ismapped to a spare that is located on a different track, but within thesame cylinder. Estimate the number of I/Os per second and the effec-tive transfer rate for a random-access workload consisting of 8-kilobytereads, with a queue length of 1 (that is, the choice of scheduling algo-rithm is not a factor). What is the effect of a bad sector on performance?Answer: Since the disk holds 22,400,000 sectors, the probability ofrequesting one of the 100 remapped sectors is very small. An exampleof a worst-case event is that we attempt to read, say, an 8 KB page, butone sector from the middle is defective and has been remapped to theworst possible location on another track in that cylinder. In this case,the time for the retrieval could be 8 ms to seek, plus two track switchesand two full rotational latencies. It is likely that a modern controllerwould read all the requested good sectors from the original track beforeswitching to the spare track to retrieve the remapped sector and thuswould incur only one track switch and rotational latency. So the timewould be 8 ms seek + 4.17 ms average rotational latency + 0.05 mstrack switch + 8.3 ms rotational latency + 0.83 ms read time (8 KBis 16sectors, 1/10 of a track rotation). Thus, the time to service this requestwould be 21.8 ms, giving an I/O rate of 45.9 requests per second andan effective bandwidth of 367 KB/s. For a severely time-constrainedapplication this might matter, but the overall impact in the weightedaverage of 100 remapped sectors and 22.4 million good sectors is nil.

12.12 In a disk jukebox, what would be the effect of having more open filesthan the number of drives in the jukebox?Answer: Two bad outcomes could result. One possibility is starvationof the applications that issue blocking I/Os to tapes that are not mountedin drives. Another possibility is thrashing, as the jukebox is commandedto switch tapes after every I/O operation.

12.13 If magnetic hard disks eventually have the same cost per gigabyte as dotapes, will tapes become obsolete, or will they still be needed? Explainyour answer.Answer: Tapes are easily removable, so they are useful for off-sitebackups and for bulk transfer of data (by sending cartridges). As arule, a magnetic hard disk is not a removable medium.

12.14 It is sometimes said that tape is a sequential-access medium, whereasmagnetic disk is a random-access medium. In fact, the suitability ofa storage device for random access depends on the transfer size. Theterm streaming transfer rate denotes the data rate for a transfer thatis underway, excluding the effect of access latency. By contrast, theeffective transfer rate is the ratio of total bytes per total seconds, includingoverhead time such as the access latency.

Suppose that, in a computer, the level-2 cache has an access latencyof 8 nanoseconds and a streaming transfer rate of 800 megabytes persecond, the main memory has an access latency of 60 nanoseconds anda streaming transfer rate of 80 megabytes per second, the magnetic disk

Page 76: Examples&Sollutions

48 Chapter 12 Mass-Storage Structure

has an access latency of 15 millisecond and a streaming transfer rate of5 megabytes per second, and a tape drive has an access latency of 60seconds and a streaming transfer rate of 2 megabytes per seconds.

a. Random access causes the effective transfer rate of a device todecrease, because no data are transferred during the access time.For the disk described, what is the effective transfer rate if anaverage access is followed by a streaming transfer of 512 bytes,8 kilobytes, 1 megabyte, and 16 megabytes?

b. The utilization of a device is the the ratio of effective transferrate to streaming transfer rate. Calculate the utilization of thedisk drive for random access that performs transfers in each ofthe four sizes given in part a.

c. Suppose that a utilization of 25 percent (or higher) is consideredacceptable. Using the performance figures given, compute thesmallest transfer size for disk that gives acceptable utilization.

d. Complete the following sentence: A disk is a random-access de-vice for transfers larger than bytes, and is a sequential-access device for smaller transfers.

e. Compute the minimum transfer sizes that give acceptable uti-lization for cache, memory, and tape.

f. When is a tape a random-access device, and when is it a sequential-access device?

Answer:

a. For 512 bytes, the effective transfer rate is calculated as follows.ETR = transfer size/transfer time.If X is transfer size, then transfer time is ((X/STR) + latency).Transfer time is 15ms + (512B/5MB per second) = 15.0097ms.Effective transfer rate is therefore 512B/15.0097ms = 33.12 KB/sec.ETR for 8KB = .47MB/sec.ETR for 1MB = 4.65MB/sec.ETR for 16MB = 4.98MB/sec.

b. Utilization of the device for 512B = 33.12 KB/sec / 5MB/sec =.0064 = .64For 8KB = 9.4%.For 1MB = 93%.For 16MB = 99.6%.

c. Calculate .25 = ETR/STR, solving for transfer size X.STR = 5MB, so 1.25MB/S = ETR.1.25MB/S * ((X/5) + .015) = X..25X + .01875 = X.X = .025MB.

d. A disk is a random-access device for transfers larger than K bytes(where K > disk block size), and is a sequential-access device forsmaller transfers.

Page 77: Examples&Sollutions

Practice Exercises 49

e. Calculate minimum transfer size for acceptable utilization ofcache memory:STR = 800MB, ETR = 200, latency = 8 * 10−9.200 (XMB/800 + 8 X 10−9) = XMB..25XMB + 1600 * 10−9 = XMB.X = 2.24 bytes.Calculate for memory:STR = 80MB, ETR = 20, L = 60 * 10−9.20 (XMB/80 + 60 * 10−9) = XMB..25XMB + 1200 * 10−9 = XMB.X = 1.68 bytes.Calculate for tape:STR = 2MB, ETR = .5, L = 60s..5 (XMB/2 + 60) = XMB..25XMB + 30 = XMB.X = 40MB.

f. It depends upon how it is being used. Assume we are usingthe tape to restore a backup. In this instance, the tape acts as asequential-access device where we are sequentially reading thecontents of the tape. As another example, assume we are usingthe tape to access a variety of records stored on the tape. In thisinstance, access to the tape is arbitrary and hence consideredrandom.

12.15 Suppose that we agree that 1 kilobyte is 1,024 bytes, 1 megabyte is1,0242 bytes, and 1 gigabyte is 1,0243 bytes. This progression continuesthrough terabytes, petabytes, and exabytes (1,0246). There are currentlyseveral new proposed scientific projects that plan to record and store afew exabytes of data during the next decade. To answer the followingquestions, you will need to make a few reasonable assumptions; statethe assumptions that you make.

a. How many disk drives would be required to hold 4 exabytes ofdata?

b. How many magnetic tapes would be required to hold 4 exabytesof data?

c. How many optical tapes would be required to hold 4 exabytesof data (see Exercise 12.21)?

d. How many holographic storage cartridges would be required tohold 4 exabytes of data (see Exercise 12.20)?

e. How many cubic feet of storage space would each option re-quire?

Answer:

a. Assume that a disk drive holds 10 GB. Then 100 disks hold 1TB, 100,000 disks hold 1 PB, and 100,000,000 disks hold 1 EB. Tostore 4 EBwould require about 400 million disks. If a magnetictape holds 40 GB, only 100 million tapes would be required. If

Page 78: Examples&Sollutions

50 Chapter 12 Mass-Storage Structure

an optical tape holds 50 times more data than a magnetic tape, 2million optical tapes would suffice. If a holographic cartridge canstore 180 GB, about 22.2 million cartridges would be required.

b. A 3.5" disk drive is about 1" high, 4" wide, and 6" deep. In feet,this is 1/12 by 1/3 by 1/2, or 1/72 cubic feet. Packed densely,the 400 million disks would occupy 5.6 million cubic feet. If weallow a factor of two for air space and space for power supplies,the required capacity is about 11 million cubic feet.

c. A 1/2" tape cartridge is about 1" high and 4.5" square. The vol-ume is about 1/85 cubic feet. For 100 million magnetic tapespacked densely, the volume is about 1.2 million cubic feet. For 2million optical tapes, the volume is 23,400 cubic feet.

d. A CD-ROM is 4.75" in diameter and about 1/16" thick. If weassume that a holostore disk is stored in a library slot that is 5"square and 1/8" wide, we calculate the volume of 22.2 milliondisks to be about 40,000 cubic feet.

Page 79: Examples&Sollutions

13C H A P T E R

I/O Systems

Practice Exercises

13.1 State three advantages of placing functionality in a device controller,rather than in the kernel. State three disadvantages.

13.2 The example of handshaking in Section 13.2 used 2 bits: a busy bit and acommand-ready bit. Is it possible to implement this handshaking withonly 1 bit? If it is, describe the protocol. If it is not, explain why 1 bit isinsufficient.

13.3 Why might a system use interrupt-driven I/O to manage a single serialport, but polling I/O to manage a front-end processor, such as a terminalconcentrator?

13.4 Polling for an I/O completion can waste a large number of CPU cyclesif the processor iterates a busy-waiting loop many times before the I/Ocompletes. But if the I/O device is ready for service, polling can be muchmore efficient than is catching and dispatching an interrupt. Describea hybrid strategy that combines polling, sleeping, and interrupts forI/O device service. For each of these three strategies (pure polling, pureinterrupts, hybrid), describe a computing environment in which thatstrategy is more efficient than is either of the others.

13.5 How does DMA increase system concurrency? How does it complicatehardware design?

13.6 Why is it important to scale up system bus and device speeds as theCPU speed increases?

13.7 Distinguish between a STREAMS driver and a STREAMS module.

29

Page 80: Examples&Sollutions
Page 81: Examples&Sollutions

13C H A P T E R

I/O Systems

Practice Exercises

13.1 State three advantages of placing functionality in a device controller,rather than in the kernel. State three disadvantages.Answer: Three advantages: Bugs are less likely to cause an operatingsystem crashPerformance can be improved by utilizing dedicated hardware andhard-coded algorithmsThe kernel is simplified by moving algorithms out of itThree disadvantages: Bugs are harder to fix—a new firmware versionor new hardware is neededImproving algorithms likewise require a hardware update rather thanjust a kernel or device-driver updateEmbedded algorithms could conflict with application’s use of the de-vice, causing decreased performance.

13.2 The example of handshaking in Section 13.2 used 2 bits: a busy bit and acommand-ready bit. Is it possible to implement this handshaking withonly 1 bit? If it is, describe the protocol. If it is not, explain why 1 bit isinsufficient.Answer: It is possible, using the following algorithm. Let’s assumewe simply use the busy-bit (or the command-ready bit; this answeris the same regardless). When the bit is off, the controller is idle. Thehost writes to data-out and sets the bit to signal that an operation isready (the equivalent of setting the command-ready bit). When thecontroller is finished, it clears the busy bit. The host then initiates thenext operation.This solution requires that both the host and the controller have readand write access to the same bit, which can complicate circuitry andincrease the cost of the controller.

51

Page 82: Examples&Sollutions

52 Chapter 13 I/O Systems

13.3 Why might a system use interrupt-driven I/O to manage a single serialport, but polling I/O to manage a front-end processor, such as a terminalconcentrator?Answer: Polling can be more efficient than interrupt-driven I/O. Thisis the case when the I/O is frequent and of short duration. Even thougha single serial port will perform I/O relatively infrequently and shouldthus use interrupts, a collection of serial ports such as those in a terminalconcentrator can produce a lot of short I/O operations, and interruptingfor each one could create a heavy load on the system. A well-timedpolling loop could alleviate that load without wasting many resourcesthrough looping with no I/O needed.

13.4 Polling for an I/O completion can waste a large number of CPU cyclesif the processor iterates a busy-waiting loop many times before the I/Ocompletes. But if the I/O device is ready for service, polling can be muchmore efficient than is catching and dispatching an interrupt. Describea hybrid strategy that combines polling, sleeping, and interrupts forI/O device service. For each of these three strategies (pure polling, pureinterrupts, hybrid), describe a computing environment in which thatstrategy is more efficient than is either of the others.Answer: A hybrid approach could switch between polling and inter-rupts depending on the length of the I/O operation wait. For example,we could poll and loop N times, and if the device is still busy at N+1,we could set an interrupt and sleep. This approach would avoid longbusy-waiting cycles. This method would be best for very long or veryshort busy times. It would be inefficient it the I/O completes at N+T(where T is a small number of cycles) due to the overhead of pollingplus setting up and catching interrupts.Pure polling is best with very short wait times. Interrupts are best withknown long wait times.

13.5 How does DMA increase system concurrency? How does it complicatehardware design?Answer: DMA increases system concurrency by allowing the CPUto perform tasks while the DMA system transfers data via the systemand memory buses. Hardware design is complicated because the DMAcontroller must be integrated into the system, and the system mustallow the DMA controller to be a bus master. Cycle stealing may alsobe necessary to allow the CPU and DMA controller to share use of thememory bus.

13.6 Why is it important to scale up system bus and device speeds as theCPU speed increases?Answer: Consider a system which performs 50% I/O and 50% com-putes. Doubling the CPU performance on this system would increasetotal system performance by only 50%. Doubling both system aspectswould increase performance by 100%. Generally, it is important to re-move the current system bottleneck, and to increase overall systemperformance, rather than blindly increasing the performance of indi-vidual system components.

13.7 Distinguish between a STREAMS driver and a STREAMS module.

Page 83: Examples&Sollutions

Practice Exercises 53

Answer: The STREAMS driver controls a physical device that could beinvolved in a STREAMS operation. The STREAMS module modifies theflow of data between the head (the user interface) and the driver.

Page 84: Examples&Sollutions
Page 85: Examples&Sollutions

14C H A P T E R

Protection

Practice Exercises

14.1 What are the main differences between capability lists and access lists?

14.2 A Burroughs B7000/B6000 MCP file can be tagged as sensitive data.When such a file is deleted, its storage area is overwritten by somerandom bits. For what purpose would such a scheme be useful?

14.3 In a ring-protection system, level 0 has the greatest access to objects,and level n (greater than zero) has fewer access rights. The access rightsof a program at a particular level in the ring structure are considered asa set of capabilities. What is the relationship between the capabilitiesof a domain at level j and a domain at level i to an object (for j > i)?

14.4 The RC 4000 system (and other systems) have defined a tree of processes(called a process tree) such that all the descendants of a process are givenresources (objects) and access rights by their ancestors only. Thus, adescendant can never have the ability to do anything that its ancestorscannot do. The root of the tree is the operating system, which has theability to do anything. Assume the set of access rights was representedby an access matrix, A. A(x,y) defines the access rights of process x toobject y. If x is a descendant of z, what is the relationship between A(x,y)and A(z,y) for an arbitrary object y?

14.5 What protection problems may arise if a shared stack is used for pa-rameter passing?

14.6 Consider a computing environment where a unique number is associ-ated with each process and each object in the system. Suppose that weallow a process with number n to access an object with number m onlyif n > m. What type of protection structure do we have?

14.7 Consider a computing environment where a process is given the priv-ilege of accessing an object only n times. Suggest a scheme for imple-menting this policy.

31

Page 86: Examples&Sollutions

32 Chapter 14 Protection

14.8 If all the access rights to an object are deleted, the object can no longerbe accessed. At this point, the object should also be deleted, and thespace it occupies should be returned to the system. Suggest an efficientimplementation of this scheme.

14.9 Why is it difficult to protect a system in which users are allowed to dotheir own I/O?

14.10 Capability lists are usually kept within the address space of the user.How does the system ensure that the user cannot modify the contentsof the list?

Page 87: Examples&Sollutions

14C H A P T E R

Protection

Practice Exercises

14.1 What are the main differences between capability lists and access lists?Answer: An access list is a list for each object consisting of the domainswith a nonempty set of access rights for that object. A capability list isa list of objects and the operations allowed on those objects for eachdomain.

14.2 A Burroughs B7000/B6000 MCP file can be tagged as sensitive data.When such a file is deleted, its storage area is overwritten by somerandom bits. For what purpose would such a scheme be useful?Answer: This would be useful as an extra security measure so that theold content of memory cannot be accessed, either intentionally or byaccident, by another program. This is especially useful for any highlyclassified information.

14.3 In a ring-protection system, level 0 has the greatest access to objects,and level n (greater than zero) has fewer access rights. The access rightsof a program at a particular level in the ring structure are considered asa set of capabilities. What is the relationship between the capabilitiesof a domain at level j and a domain at level i to an object (for j > i)?Answer: Dj is a subset of Di .

14.4 The RC 4000 system (and other systems) have defined a tree of processes(called a process tree) such that all the descendants of a process are givenresources (objects) and access rights by their ancestors only. Thus, adescendant can never have the ability to do anything that its ancestorscannot do. The root of the tree is the operating system, which has theability to do anything. Assume the set of access rights was representedby an access matrix, A. A(x,y) defines the access rights of process x to

55

Page 88: Examples&Sollutions

56 Chapter 14 Protection

object y. If x is a descendant of z, what is the relationship between A(x,y)and A(z,y) for an arbitrary object y?Answer: A(x,y) is a subset of A(z,y).

14.5 What protection problems may arise if a shared stack is used for pa-rameter passing?Answer: The contents of the stack could be compromised by otherprocess(es) sharing the stack.

14.6 Consider a computing environment where a unique number is associ-ated with each process and each object in the system. Suppose that weallow a process with number n to access an object with number m onlyif n > m. What type of protection structure do we have?Answer: Hierarchical structure.

14.7 Consider a computing environment where a process is given the priv-ilege of accessing an object only n times. Suggest a scheme for imple-menting this policy.Answer: Add an integer counter with the capability.

14.8 If all the access rights to an object are deleted, the object can no longerbe accessed. At this point, the object should also be deleted, and thespace it occupies should be returned to the system. Suggest an efficientimplementation of this scheme.Answer: Reference counts.

14.9 Why is it difficult to protect a system in which users are allowed to dotheir own I/O?Answer: In earlier chapters we identified a distinction between kerneland user mode where kernel mode is used for carrying out privilegedoperations such as I/O. One reason why I/O must be performed inkernel mode is that I/O requires accessing the hardware and properaccess to the hardware is necessary for system integrity. If we allowusers to perform their own I/O, we cannot guarantee system integrity.

14.10 Capability lists are usually kept within the address space of the user.How does the system ensure that the user cannot modify the contentsof the list?Answer: A capability list is considered a “protected object” and isaccessed only indirectly by the user. The operating system ensures theuser cannot access the capability list directly.

Page 89: Examples&Sollutions

15C H A P T E R

Security

No Exercises

33

Page 90: Examples&Sollutions
Page 91: Examples&Sollutions

15C H A P T E R

Security

No Exercises

57

Page 92: Examples&Sollutions
Page 93: Examples&Sollutions

16C H A P T E RDistributedSystemStructures

Practice Exercises

16.1 Most WANs employ only a partially connected topology. Why is thisso?

16.2 Under what circumstances is a token-passing network more effectivethan an Ethernet network?

16.3 Why would it be a bad idea for gateways to pass broadcast packetsbetween networks? What would be the advantages of doing so?

16.4 Discuss the advantages and disadvantages of caching name transla-tions for computers located in remote domains.

16.5 What are the advantages and disadvantages of using circuit switching?For what kinds of applications is circuit switching a viable strategy?

16.6 What are two formidable problems that designers must solve to imple-ment a network-transparent system?

16.7 Process migration within a heterogeneous network is usually impossi-ble, given the differences in architectures and operating systems. De-scribe a method for process migration across different architecturesrunning:

a. The same operating system

b. Different operating systems

16.8 To build a robust distributed system, you must know what kinds offailures can occur.

a. List three possible types of failure in a distributed system.

b. Specify which of the entries in your list also are applicable to acentralized system.

35

Page 94: Examples&Sollutions

36 Chapter 16 Distributed System Structures

16.9 Is it always crucial to know that the message you have sent has arrivedat its destination safely? If your answer is yes, explain why. If youranswer is no, give appropriate examples.

16.10 Present an algorithm for reconstructing a logical ring after a process inthe ring fails.

16.11 Consider a distributed system with two sites, A and B. Consider whethersite A can distinguish among the following:

a. B goes down.

b. The link between A and B goes down.

c. B is extremely overloaded and its response time is 100 timeslonger than normal.

What implications does your answer have for recovery in distributedsystems?

Page 95: Examples&Sollutions

16C H A P T E RDistributedSystemStructures

Practice Exercises

16.1 Most WANs employ only a partially connected topology. Why is thisso?Answer: Cost. A fully connected network requires a link betweenevery node in the network. For a WAN, this may be too costly as com-munication links between physically distant hosts may be expensive.

16.2 Under what circumstances is a token-passing network more effectivethan an Ethernet network?Answer: A token ring is very effective under high sustained load, asno collisions can occur and each slot may be used to carry a message,providing high throughput. A token ring is less effective when theload is light (token processing takes longer than bus access, so any onepacket can take longer to reach its destination) or sporadic.

16.3 Why would it be a bad idea for gateways to pass broadcast packetsbetween networks? What would be the advantages of doing so?Answer: All broadcasts would be propagated to all networks, causinga lot of network traffic. If broadcast traffic were limited to importantdata (and very little of it), then broadcast propagation would savegateways from having to run special software to watch for this data(such as network routing information) and rebroadcast it.

16.4 Discuss the advantages and disadvantages of caching name transla-tions for computers located in remote domains.Answer: There is a performance advantage to caching name transla-tions for computers located in remote domains: repeated resolution ofthe same name from different computers located in the local domaincould be performed locally without requiring a remote name lookupoperation. The disadvantage is that there could be inconsistencies in the

59

Page 96: Examples&Sollutions

60 Chapter 16 Distributed System Structures

name translations when updates are made in the mapping of names toIP addresses. These consistency problems could be solved by invalidat-ing translations, which would require state to be managed regardingwhich computers are caching a certain translation and also would re-quire a number of invalidation messages, or by using leases wherebythe caching entity invalidates a translation after a certain period of time.The latter approach requires less state and no invalidation messages butmight suffer from temporary inconsistencies.

16.5 What are the advantages and disadvantages of using circuit switching?For what kinds of applications is circuit switching a viable strategy?Answer: Circuit switching guarantees that the network resources re-quired for a transfer are reserved before the transmission takes place.This ensures that packets will not be dropped and their delivery wouldsatisfy quality of service requirements. The disadvantage of circuitswitching is that it requires a round-trip message to set-up the reser-vations and it also might overprovision resources, thereby resultingin suboptimal use of the resources. Circuit switching is a viable strat-egy for applications that have constant demands regarding networkresources and would require the resources for long periods of time,thereby amortizing the initial overheads.

16.6 What are two formidable problems that designers must solve to imple-ment a network-transparent system?Answer: One such issue is making all the processors and storagedevices seem transparent across the network. In other words, the dis-tributed system should appear as a centralized system to users. TheAndrew file system and NFS provide this feature: the distributed filesystem appears to the user as a single file system but in reality it maybe distributed across a network.Another issue concerns the mobility of users. We want to allow usersto connect to the “system” rather than to a specific machine (althoughin reality they may be logging in to a specific machine somewhere inthe distributed system).

16.7 Process migration within a heterogeneous network is usually impossi-ble, given the differences in architectures and operating systems. De-scribe a method for process migration across different architecturesrunning:

a. The same operating system

b. Different operating systems

Answer: For the same operating system, process migration is rela-tively straightforward, as the state of the process needs to migrate fromone processor to another. This involves moving the address space, stateof the CPU registers, and open files from the source system to the des-tination. However, it is important that identical copies of the operatingsystem are running on the different systems to ensure compatibility.If the operating system are the same, but perhaps different versionsare running on the separate systems, then migrating processes must be

Page 97: Examples&Sollutions

Practice Exercises 61

sure to follow programming guidelines that are consistent between thedifferent versions of the operating system.Java applets provide a nice example of process migration between dif-ferent operating systems. To hide differences in the underlying system,the migrated process (i.e., a Java applet) runs on a virtual machinerather than a specific operating system. All that is required is for thevirtual machine to be running on the system the process migrates to.

16.8 To build a robust distributed system, you must know what kinds offailures can occur.

a. List three possible types of failure in a distributed system.

b. Specify which of the entries in your list also are applicable to acentralized system.

Answer: Three common failures in a distributed system include:(1) network link failure, (2) host failure, (3) storage medium failure.Both (2) and (3) are failures that could also occur in a centralized sys-tem, whereas a network link failure can occur only in a networked-distributed system.

16.9 Is it always crucial to know that the message you have sent has arrivedat its destination safely? If your answer is yes, explain why. If youranswer is no, give appropriate examples.Answer: No. Many status-gathering programs work from the assump-tion that packets may not be received by the destination system. Theseprograms generally broadcast a packet and assume that at least someother systems on their network will receive the information. For in-stance, a daemon on each system might broadcast the system’s loadaverage and number of users. This information might be used for pro-cess migration target selection. Another example is a program thatdetermines if a remote site is both running and accessible over the net-work. If it sends a query and gets no reply it knows the system cannotcurrently be reached.

16.10 Present an algorithm for reconstructing a logical ring after a process inthe ring fails.Answer: Typically distributed systems employ a coordinator processthat performs functions needed by other processes in the system. Thiswould include enforcing mutual exclusion and—in this case of a ring—replacing a lost token.A scheme similar to the ring algorithm presented in Section 18.6.2 canbe used. The algorithm is as follows:The ring algorithm assumes that the links are unidirectional and thatprocesses send their messages to the neighbor on their right. The maindata structure used by the algorithm is the active list, a list that containsthe priority numbers of all active processes in the system when thealgorithm ends; each process maintains its own active list.

a. If process Pi detects a coordinator failure, it creates a new activelist that is initially empty. It then sends a message elect(i) to itsright neighbor, and adds the number i to its active list.

Page 98: Examples&Sollutions

62 Chapter 16 Distributed System Structures

b. If Pi receives a message elect(j) from the process on the left, itmust respond in one of three ways:

1. If this is the first elect message it has seen or sent, Pi createsa new active list with the numbers i and j. It then sends themessage elect(i), followed by the message elect(j).

2. If i �= j, that is, the message received does not contain Pi ’snumber, then Pi adds j to its active list and forwards themessage to its right neighbor.

3. If i = j, that is, Pi receives the message elect(i), then theactive list for Pi now contains the numbers of all the activeprocesses in the system. Process Pi can now determinethe largest number in the active list to identify the newcoordinator process.

16.11 Consider a distributed system with two sites, A and B. Consider whethersite A can distinguish among the following:

a. B goes down.

b. The link between A and B goes down.

c. B is extremely overloaded and its response time is 100 timeslonger than normal.

What implications does your answer have for recovery in distributedsystems?Answer: One technique would be for B to periodically send a I-am-upmessage to A indicating it is still alive. If A does not receive an I-am-up message, it can assume either B—or the network link—is down.Note that an I-am-up message does not allow A to distinguish betweeneach type of failure. One technique that allows A better to determineif the network is down is to send an Are-you-up message to B using analternate route. If it receives a reply, it can determine that indeed thenetwork link is down and that B is up.If we assume that A knows B is up and is reachable (via the I-am-up mechanism) and that A has some value N that indicates a normalresponse time, A could monitor the response time from B and comparevalues to N, allowing A to determine if B is overloaded or not.The implications of both of these techniques are that A could chooseanother host—say C—in the system if B is either down, unreachable,or overloaded.

Page 99: Examples&Sollutions

17C H A P T E R

Distributed FileSystems

No Exercises

37

Page 100: Examples&Sollutions
Page 101: Examples&Sollutions

17C H A P T E R

Distributed FileSystems

No Exercises

63

Page 102: Examples&Sollutions
Page 103: Examples&Sollutions

18C H A P T E R

DistributedCoordination

No Exercises

39

Page 104: Examples&Sollutions
Page 105: Examples&Sollutions

18C H A P T E R

DistributedCoordination

No Exercises

65

Page 106: Examples&Sollutions
Page 107: Examples&Sollutions

19C H A P T E R

Real-timeSystems

No Exercises

41

Page 108: Examples&Sollutions
Page 109: Examples&Sollutions

19C H A P T E R

Real-timeSystems

No Exercises

67

Page 110: Examples&Sollutions
Page 111: Examples&Sollutions

20C H A P T E R

MultimediaSystems

No Exercises

43

Page 112: Examples&Sollutions
Page 113: Examples&Sollutions

20C H A P T E R

MultimediaSystems

No Exercises

69

Page 114: Examples&Sollutions
Page 115: Examples&Sollutions

21C H A P T E R

The LinuxSystem

Practice Exercises

21.1 Dynamically loadable kernel modules give flexibility when drivers areadded to a system, but do they have disadvantages too? Under whatcircumstances would a kernel be compiled into a single binary file, andwhen would it be better to keep it split into modules? Explain youranswer.

21.2 Multithreading is a commonly used programming technique. Describethree different ways that threads could be implemented. Explain howthese ways compare to the Linux clone mechanism. When might eachalternative mechanism be better or worse than using clones?

21.3 The Linux kernel does not allow paging out of kernel memory. Whateffect does this restriction have on the kernel’s design? What are twoadvantages and two disadvantages of this design decision?

21.4 What are three advantages of dynamic (shared) linkage of librariescompared to static linkage? What are two cases where static linkage ispreferable?

21.5 Compare the use of networking sockets with the use of shared memoryas a mechanism for communicating data between processes on a singlecomputer. What are the advantages of each method? When might eachbe preferred?

21.6 UNIX systems used to use disk-layout optimizations based on the ro-tation position of disk data, but modern implementations, includingLinux, simply optimize for sequential data access. Why do they do so?Of what hardware characteristics does sequential access take advan-tage? Why is rotational optimization no longer so useful?

45

Page 116: Examples&Sollutions
Page 117: Examples&Sollutions

21C H A P T E R

The LinuxSystem

Practice Exercises

21.1 Dynamically loadable kernel modules give flexibility when drivers areadded to a system, but do they have disadvantages too? Under whatcircumstances would a kernel be compiled into a single binary file, andwhen would it be better to keep it split into modules? Explain youranswer.Answer: There are two principal drawbacks with the use of modules.The first is size: module management consumes unpageable kernelmemory, and a basic kernel with a number of modules loaded willconsume more memory than an equivalent kernel with the driverscompiled into the kernel image itself. This can be a very significantissue on machines with limited physical memory.

The second drawback is that modules can increase the complexityof the kernel bootstrap process. It is hard to load up a set of modulesfrom disk if the driver needed to access that disk itself a module thatneeds to be loaded. As a result, managing the kernel bootstrap withmodules can require extra work on the part of the administrator: themodules required to bootstrap need to be placed into a ramdisk imagethat is loaded alongside the initial kernel image when the system isinitialized.

In certain cases it is better to use a modular kernel, and in othercases it is better to use a kernel with its device drivers prelinked. Whereminimizing the size of the kernel is important, the choice will dependon how often the various device drivers are used. If they are in con-stant use, then modules are unsuitable. This is especially true wheredrivers are needed for the boot process itself. On the other hand, ifsome drivers are not always needed, then the module mechanism al-

71

Page 118: Examples&Sollutions

72 Chapter 21 The Linux System

lows those drivers to be loaded and unloaded on demand, potentiallyoffering a net saving in physical memory.

Where a kernel is to be built that must be usable on a large varietyof very different machines, then building it with modules is clearlypreferable to using a single kernel with dozens of unnecessary driversconsuming memory. This is particularly the case for commercially dis-tributed kernels, where supporting the widest variety of hardware inthe simplest manner possible is a priority.

However, if a kernel is being built for a single machine whoseconfiguration is known in advance, then compiling and using modulesmay simply be an unnecessary complexity. In cases like this, the use ofmodules may well be a matter of taste.

21.2 Multithreading is a commonly used programming technique. Describethree different ways that threads could be implemented. Explain howthese ways compare to the Linux clone mechanism. When might eachalternative mechanism be better or worse than using clones?Answer: Thread implementations can be broadly classified into twogroups: kernel-based threads and user-mode threads. User-mode threadpackages rely on some kernel support—they may require timer inter-rupt facilities, for example—but the scheduling between threads is notperformed by the kernel but by some library of user-mode code. Multi-ple threads in such an implementation appear to the operating systemas a single execution context. When the multithreaded process is run-ning, it decides for itself which of its threads to execute, using non-localjumps to switch between threads according to its own preemptive ornon-preemptive scheduling rules.

Alternatively, the operating system kernel may provide support forthreads itself. In this case, the threads may be implemented as separateprocesses that happen to share a complete or partial common addressspace, or they may be implemented as separate execution contextswithin a single process. Whichever way the threads are organized, theyappear as fully independent execution contexts to the application.

Hybrid implementations are also possible, where a large number ofthreads are made available to the application using a smaller numberof kernel threads. Runnable user threads are run by the first availablekernel thread.

In Linux, threads are implemented within the kernel by a clonemechanism that creates a new process within the same virtual addressspace as the parent process. Unlike some kernel-based thread packages,the Linux kernel does not make any distinction between threads andprocesses: a thread is simply a process that did not create a new virtualaddress space when it was initialized.

The main advantage of implementing threads in the kernel ratherthan in a user-mode library are that:

• kernel-threaded systems can take advantage of multipleprocessors if they are available; and

• if one thread blocks in a kernel service routine (for example, asystem call or page fault), other threads are still able to run.

Page 119: Examples&Sollutions

Practice Exercises 73

A lesser advantage is the ability to assign different security attributesto each thread.

User-mode implementations do not have these advantages. Becausesuch implementations run entirely within a single kernel execution con-text, only one thread can ever be running at once, even if multiple CPUsare available. For the same reason, if one thread enters a system call,no other threads can run until that system call completes. As a result,one thread doing a blocking disk read will hold up every thread in theapplication. However, user-mode implementations do have their ownadvantages. The most obvious is performance: invoking the kernel’sown scheduler to switch between threads involves entering a new pro-tection domain as the CPU switches to kernel mode, whereas switchingbetween threads in user mode can be achieved simply by saving andrestoring the main CPU registers. User-mode threads may also con-sume less system memory: most UNIX systems will reserve at least afull page for a kernel stack for each kernel thread, and this stack maynot be pageable.

The hybrid approach, implementing multiple user threads over asmaller number of kernel threads, allows a balance between these trade-offs to be achieved. The kernel threads will allow multiple threads tobe in blocking kernel calls at once and will permit running on mul-tiple CPUs, and user-mode thread switching can occur within eachkernel thread to perform lightweight threading without the overheadsof having too many kernel threads. The downside of this approachis complexity: giving control over the tradeoff complicates the threadlibrary’s user interface.

21.3 The Linux kernel does not allow paging out of kernel memory. Whateffect does this restriction have on the kernel’s design? What are twoadvantages and two disadvantages of this design decision?Answer: The primary impact of disallowing paging of kernel memoryin Linux is that the non-preemptability of the kernel is preserved. Anyprocess taking a page fault, whether in kernel or in user mode, risksbeing rescheduled while the required data is paged in from disk. Be-cause the kernel can rely on not being rescheduled during access to itsprimary data structures, locking requirements to protect the integrityof those data structures are very greatly simplified. Although designsimplicity is a benefit in itself, it also provides an important perfor-mance advantage on uniprocessor machines due to the fact that it isnot necessary to do additional locking on most internal data structures.

There are a number of disadvantages to the lack of pageable kernelmemory, however. First of all, it imposes constraints on the amount ofmemory that the kernel can use. It is unreasonable to keep very largedata structures in non-pageable memory, since that represents physicalmemory that absolutely cannot be used for anything else. This has twoimpacts: first of all, the kernel must prune back many of its internal datastructures manually, instead of being able to rely on a single virtual-memory mechanism to keep physical memory usage under control.Second, it makes it infeasible to implement certain features that requirelarge amounts of virtual memory in the kernel, such as the /tmp-

Page 120: Examples&Sollutions

74 Chapter 21 The Linux System

filesystem (a fast virtual-memory-based file system found on someUNIX systems).

Note that the complexity of managing page faults while runningkernel code is not an issue here. The Linux kernel code is already ableto deal with page faults: it needs to be able to deal with system callswhose arguments reference user memory that may be paged out todisk.

21.4 What are three advantages of dynamic (shared) linkage of librariescompared to static linkage? What are two cases where static linkage ispreferable?Answer: The primary advantages of shared libraries are that theyreduce the memory and disk space used by a system, and they enhancemaintainability.

When shared libraries are being used by all running programs,there is only one instance of each system library routine on disk, andat most one instance in physical memory. When the library in questionis one used by many applications and programs, then the disk andmemory savings can be quite substantial. In addition, the startup timefor running new programs can be reduced, since many of the commonfunctions needed by that program are likely to be already loaded intophysical memory.

Maintainability is also a major advantage of dynamic linkage overstatic. If all running programs use a shared library to access their sys-tem library routines, then upgrading those routines, either to add newfunctionality or to fix bugs, can be done simply by replacing that sharedlibrary. There is no need to recompile or relink any applications; anyprograms loaded after the upgrade is complete will automatically pickup the new versions of the libraries.

There are other advantages too. A program that uses shared librariescan often be adapted for specific purposes simply by replacing one ormore of its libraries, or even (if the system allows it, and most UNIXsincluding Linux do) adding a new one at run time. For example, adebugging library can be substituted for a normal one to trace a problemin an application. Shared libraries also allow program binaries to belinked against commercial, proprietary library code without actuallyincluding any of that code in the program’s final executable file. This isimportant because on most UNIX systems, many of the standard sharedlibraries are proprietary, and licensing issues may prevent includingthat code in executable files to be distributed to third parties.

In some places, however, static linkage is appropriate. One example isin rescue environments for system administrators. If a system adminis-trator makes a mistake while installing any new libraries, or if hardwaredevelops problems, it is quite possible for the existing shared librariesto become corrupt. As a result, often a basic set of rescue utilities arelinked statically, so that there is an opportunity to correct the faultwithout having to rely on the shared libraries functioning correctly.

There are also performance advantages that sometimes make staticlinkage preferable in special cases. For a start, dynamic linkage doesincrease the startup time for a program, as the linking must now be

Page 121: Examples&Sollutions

Practice Exercises 75

done at run time rather than at compile time. Dynamic linkage can alsosometimes increase the maximum working set size of a program (thetotal number of physical pages of memory required to run the program).In a shared library, the user has no control over where in the librarybinary file the various functions reside. Since most functions do notprecisely fill a full page or pages of the library, loading a function willusually result in loading in parts of the surrounding functions, too. Withstatic linkage, absolutely no functions that are not referenced (directlyor indirectly) by the application need to be loaded into memory.

Other issues surrounding static linkage include ease of distribution:it is easier to distribute an executable file with static linkage than withdynamic linkage if the distributor is not certain whether the recipientwill have the correct libraries installed in advance. There may also becommercial restrictions against redistributing some binaries as sharedlibraries. For example, the license for the UNIX “Motif” graphical en-vironment allows binaries using Motif to be distributed freely as longas they are statically linked, but the shared libraries may not be usedwithout a license.

21.5 Compare the use of networking sockets with the use of shared memoryas a mechanism for communicating data between processes on a singlecomputer. What are the advantages of each method? When might eachbe preferred?Answer: Using network sockets rather than shared memory for localcommunication has a number of advantages. The main advantage isthat the socket programming interface features a rich set of synchro-nization features. A process can easily determine when new data hasarrived on a socket connection, how much data is present, and whosent it. Processes can block until new data arrives on a socket, or theycan request that a signal be delivered when data arrives. A socket alsomanages separate connections. A process with a socket open for re-ceive can accept multiple connections to that socket and will be toldwhen new processes try to connect or when old processes drop theirconnections.

Shared memory offers none of these features. There is no wayfor a process to determine whether another process has delivered orchanged data in shared memory other than by going to look at thecontents of that memory. It is impossible for a process to block andrequest a wakeup when shared memory is delivered, and there is nostandard mechanism for other processes to establish a shared memorylink to an existing process.

However, shared memory has the advantage that it is very muchfaster than socket communications in many cases. When data is sentover a socket, it is typically copied from memory to memory multipletimes. Shared memory updates require no data copies: if one processupdates a data structure in shared memory, that update is immediatelyvisible to all other processes sharing that memory. Sending or receivingdata over a socket requires that a kernel system service call be madeto initiate the transfer, but shared memory communication can be per-formed entirely in user mode with no transfer of control required.

Page 122: Examples&Sollutions

76 Chapter 21 The Linux System

Socket communication is typically preferred when connection man-agement is important or when there is a requirement to synchronizethe sender and receiver. For example, server processes will usually es-tablish a listening socket to which clients can connect when they wantto use that service. Once the socket is established, individual requestsare also sent using the socket, so that the server can easily determinewhen a new request arrives and who it arrived from.

In some cases, however, shared memory is preferred. Shared memoryis often a better solution when either large amounts of data are tobe transferred or when two processes need random access to a largecommon data set. In this case, however, the communicating processesmay still need an extra mechanism in addition to shared memory toachieve synchronization between themselves. The X Window System, agraphical display environment for UNIX, is a good example of this: mostgraphic requests are sent over sockets, but shared memory is offeredas an additional transport in special cases where large bitmaps are tobe displayed on the screen. In this case, a request to display the bitmapwill still be sent over the socket, but the bulk data of the bitmap itselfwill be sent via shared memory.

21.6 UNIX systems used to use disk-layout optimizations based on the ro-tation position of disk data, but modern implementations, includingLinux, simply optimize for sequential data access. Why do they do so?Of what hardware characteristics does sequential access take advan-tage? Why is rotational optimization no longer so useful?Answer: The performance characteristics of disk hardware havechanged substantially in recent years. In particular, many enhance-ments have been introduced to increase the maximum bandwidth thatcan be achieved on a disk. In a modern system, there can be a longpipeline between the operating system and the disk’s read-write head.A disk I/O request has to pass through the computer’s local disk con-troller, over bus logic to the disk drive itself, and then internally to thedisk, where there is likely to be a complex controller that can cache dataaccesses and potentially optimize the order of I/O requests.

Because of this complexity, the time taken for one I/O request to beacknowledged and for the next request to be generated and receivedby the disk can far exceed the amount of time between one disk sectorpassing under the read-write head and the next sector header arriving.In order to be able efficiently to read multiple sectors at once, diskswill employ a readahead cache. While one sector is being passed backto the host computer, the disk will be busy reading the next sectors inanticipation of a request to read them. If read requests start arriving inan order that breaks this readahead pipeline, performance will drop.As a result, performance benefits substantially if the operating systemtries to keep I/O requests in strict sequential order.

A second feature of modern disks is that their geometry can be verycomplex. The number of sectors per cylinder can vary according to theposition of the cylinder: more data can be squeezed into the longertracks nearer the edge of the disk than at the center of the disk. For anoperating system to optimize the rotational position of data on such

Page 123: Examples&Sollutions

Practice Exercises 77

disks, it would have to have complete understanding of this geometry,as well as the timing characteristics of the disk and its controller. In gen-eral, only the disk’s internal logic can determine the optimal schedulingof I/Os, and the disk’s geometry is likely to defeat any attempt by theoperating system to perform rotational optimizations.

Page 124: Examples&Sollutions
Page 125: Examples&Sollutions

22C H A P T E R

Windows XP

Practice Exercises

22.1 What type of operating system is Windows XP? Describe two of itsmajor features.

22.2 List the design goals of Windows XP. Describe two in detail.

22.3 Describe the booting process for a Windows XP system.

22.4 Describe the three main architectural layers of Windows XP.

22.5 What is the job of the object manager?

22.6 What types of services does the process manager provide? What is alocal procedure call?

22.7 What are the responsibilities of the I/O manager?

22.8 Does Windows XP offer any user-mode processes that enable it to runprograms developed for other operating systems? Describe two of thesesubsystems.

22.9 What types of networking does Windows XP support? How does Win-dows XP implement transport protocols? Describe two networking pro-tocols.

22.10 How is the NTFS namespace organized? Describe.

22.11 How does NTFS handle data structures? How does NTFS recover froma system crash? What is guaranteed after a recovery takes place?

22.12 How does Windows XP allocate user memory?

22.13 Describe some of the ways an application can use memory via theWin32 API.

47

Page 126: Examples&Sollutions
Page 127: Examples&Sollutions

22C H A P T E R

Windows XP

Practice Exercises

22.1 What type of operating system is Windows XP? Describe two of itsmajor features.Answer: A 32/64 bit preemptive multitasking operating system sup-porting multiple users. (1) The ability automatically to repair applica-tion and operating system problems. (2) Better networking and deviceexperience (including digital photography and video).

22.2 List the design goals of Windows XP. Describe two in detail.Answer: Design goals include security, reliability, Windows and POSIXapplication compatibility, high performance, extensibility, portabilityand international support. (1) Reliability was perceived as a stringentrequirement and included extensive driver verification, facilities forcatching programming errors in user-level code, and a rigorous certi-fication process for third-party drivers, applications, and devices. (2)Achieving high performance required examination of past problem ar-eas such as I/O performance, server CPU bottlenecks, and the scalabilityof multithreaded and multiprocessor environments.

22.3 Describe the booting process for a Windows XP system.Answer: (1) As the hardware powers on, the BIOS begins executingfrom ROM and loads and executes the bootstrap loader from the disk. (2)The NTLDR program is loaded from the root directory of the identifiedsystem device and determines which boot device contains the operatingsystem. (3) NTLDR loads the HAL library, kernel, and system hive. Thesystem hive indicates the required boot drivers and loads them. (4)Kernel execution begins by initializing the system and creating twoprocesses: the system process containing all internal worker threads,and the first user-mode initialization process: SMSS. (5) SMSS further

79

Page 128: Examples&Sollutions

80 Chapter 22 Windows XP

initializes the system by establishing paging files and loading devicedrivers. (6) SMSS creates two processes: WINLOGON, which brings upthe rest of the system, and CSRSS (the Win32 subsystem process).

22.4 Describe the three main architectural layers of Windows XP.Answer: (1) The HAL (Hardware Abstraction Layer) creates operat-ing system portability by hiding hardware differences from the upperlayers of the operating system. Administrative details of low-level fa-cilities are provided by HAL interfaces. HAL presents a virtual-machineinterface that is used by the kernel dispatcher, the executive and devicedrivers. (2) The kernel layer provides a foundation for the executivefunctions and user-mode subsystems. The kernel remains in memoryand is never preempted. Its responsibilities are thread scheduling, in-terrupt and exception handling, low-level processor synchronization,and power failure recovery. (3) The executive layer provides a set of ser-vices used by all subsystems: object manager, virtual memory manager,process manager, local procedure call facility, I/O manager, securitymonitor, plug-and-play manager, registry, and booting.

22.5 What is the job of the object manager?Answer: Objects present a generic set of kernel mode interfaces to user-mode programs. Objects are manipulated by the executive-layer objectmanager. The job of the object manager is to supervise the allocationand use of all managed objects.

22.6 What types of services does the process manager provide? What is alocal procedure call?Answer: The process manager provides services for creating, delet-ing, and using processes, threads and jobs. The process manager alsoimplements queuing and delivery of asynchronous procedure calls tothreads. The local procedure call (LPC) is a message-passing system.The operating system uses the LPC to pass requests and results be-tween client and server processes within a single machine, in particularbetween Windows XP subsystems.

22.7 What are the responsibilities of the I/O manager?Answer: The I/O manager is responsible for file systems, devicedrivers, and network drivers. The I/O manager keeps track of whichdevice drivers, filter drivers, and file systems are loaded and managesbuffers for I/O requests. It furthermore assists in providing memory-mapped file I/O and controls the cache manager for the whole I/Osystem.

22.8 Does Windows XP offer any user-mode processes that enable it to runprograms developed for other operating systems? Describe two of thesesubsystems.Answer: Environmental subsystems are user-mode processes layeredover the native executable services to enable Windows XP to run pro-grams developed for other operating systems. (1) A Win32 applicationcalled the virtual DOS machine (VDM) is provided as a user-mode pro-cess to run MS-DOS applications. The VDM can execute or emulate Intel486 instructions and also provides routines to emulate MS-DOS BIOS

Page 129: Examples&Sollutions

Practice Exercises 81

services and provides virtual drivers for screen, keyboard, and com-munication ports. (2) Windows-on-windows (WOW32) provides kerneland stub routines for Windows 3.1 functions. The stub routines call theappropriate Win32 subroutines, converting the 16-bit addresses into32-bit addresses.

22.9 What types of networking does Windows XP support? How does Win-dows XP implement transport protocols? Describe two networking pro-tocols.Answer: Support is provided for both peer-to-peer and client-servernetworking. Transport protocols are implemented as drivers. (1) TheTCP/IP package includes SNMP, DHCP, WINS, and NetBIOS support. (2)Point-to-point tunneling protocol is provided to communicate betweenremote-access modules running on Windows XP servers and other clientsystems connected over the internet. Using this scheme, multi-protocolvirtual private networks (VPNs) are supported over the internet.

22.10 How is the NTFS namespace organized? Describe.Answer: The NTFS namespace is organized as a hierarchy of directorieswhere each directory uses a B+ tree data structure to store an index ofthe file names in that directory. The index root of a directory containsthe top level of the B+ tree. Each entry in the directory contains thename and file reference of the file as well as the update timestamp andfile size.

22.11 How does NTFS handle data structures? How does NTFS recover froma system crash? What is guaranteed after a recovery takes place?Answer: In NTFS, all file-system data structure updates are performedinside transactions. Before a data structure is altered, the transactionwrites a log record containing redo and undo information. A commitrecord is written to the log after a transaction has succeeded. After acrash the file system can be restored to a consistent state by processingthe log records, first redoing operations for committed transactions andundoing operations for transactions that did not successfully commit.This scheme does not guarantee that user file contents are correct aftera recovery, but rather that the file-system data structures (file metadata)are undamaged and reflect some consistent state that existed before thecrash.

22.12 How does Windows XP allocate user memory?Answer: User memory can be allocated according to several schemes:virtual memory, memory-mapped files, heaps, and thread-local stor-age.

22.13 Describe some of the ways an application can use memory via theWin32 API.Answer: (1) Virtual memory provides several functions that allowan application to reserve and release memory, specifying the virtualaddress at which the memory is allocated. (2) A file may be memory-mapped into address space, providing a means for two processes toshare memory. (3) When a Win32 process is initialized, it is createdwith a default heap. Private heaps can be created that provide regions of

Page 130: Examples&Sollutions

82 Chapter 22 Windows XP

reserved address space for applications. Thread management functionsare provided to allocate and control thread access to private heaps. (4)A thread-local storage mechanism provides a way for global and staticdata to work properly in a multithreaded environment. Thread-lockstorage allocates global storage on a per-thread basis.

Page 131: Examples&Sollutions

23C H A P T E RInfluentialOperatingSystems

No Exercises

49

Page 132: Examples&Sollutions
Page 133: Examples&Sollutions

23C H A P T E RInfluentialOperatingSystems

No Exercises

83

Page 134: Examples&Sollutions

Recommended