# CS372H Spring 2011 Homework 3 Solutions

## Problem 1

Suppose a program references pages in the following sequence:
ACBDBAEFBFAGEFA
Suppose the computer on which this program is running has 4 pages of physical memory.
1. Show how LRU-based demand paging would fault pages into the four frames of physical memory.
Solution:
 A C B D B A E F B F A G E F A 1 A + + + 2 C E G 3 B + + E 4 D F + +

2. Show how OPT (or MIN) based demand paging would fault pages into the four frames of physical memory.
Solution:
 A C B D B A E F B F A G E F A 1 A + + + 2 C E + 3 B + + G 4 D F + +

3. Show how clock-based demand paging would fault pages into the four frames of physical memory.
Solution:
 A C B D B A E F B F A G E F A 1 A1 1 1 1 1 +1 E1 1 1 1 1 0 +1 1 1 2 C1 1 1 1 1 0 F1 1 +1 1 0 0 +1 1 3 B1 1 +1 1 0 0 +1 1 0 G1 1 1 1 4 D1 1 1 0 0 0 0 A1 1 1 1 +1

"+" (plus sign) means cache hit
1/0 means the state of the "used" bit at the end of the specified step
bold italics is the position of the clock hand at the end of the specified step (this is what will be looked at first the next time the clock hand moves)

## Problem 2

Assume that you have a page reference string for a process. Let the page reference string have length p with n distinct page numbers occurring in it. Let m be the number of page frames that are allocated to the process (all the page frames are initially empty). Let n > m.
1. What is the lower bound on the number of page faults?
2. What is the upper bound on the number of page faults?
Hint: Your answer is independent of the page replacement scheme that you use.

Solution TBD.

## Problem 3

Belady's anomaly: Intuitively, it seems that the more frames the memory has, the fewer page faults a program will get. Surprisingly enough, this is not always true. Belady (1969) discovered an example in which FIFO page replacement causes more faults with four page frames than with three. This strange situation has become known as Belady's anomaly. To illustrate, a program with five virtual pages numbered from 0 to 4 references its pages in the order:
0 1 2 3 0 1 4 0 1 2 3 4
1. Using FIFO replacement, compute the number of page faults with 3 frames. Repeat for 4 frames.
2. Compute the number of page faults under LRU, NRU, the clock algorithm, and the optimal algorithm.

1. The number of page faults is 9 with 3 frames, 10 with 4.
2. The point of the exercise is to see that the performance of LRU, NRU, clock algorithm and optimal all offer better performance with larger number of frames.

## Problem 4

Explain the steps that an operating system goes through when the CPU receives an interrupt.

First, the hardware switches the machine to supervisor mode and causes control to transfer to a routine at a pre-specified address. The operating system has already loaded that address with a pointer to a function that handles the interrupt. The function starts by saving all registers, possibly setting the allowed interrupts to a level that guarantees correct execution of the operating system, and then executes the code to serve the interrupt. When the operating system is done, it restores the registers again and calls the "iret" to return to the user program that was running when the interrupt occurred. If there is no runnable program, the operating system jumps to the idle loop and waits for an interrupt. Finally, if the interrupt occurred while the operating system was running, then the OS simply restores the registers and jumps to the instruction following the one at which the interrupt occurred.