Lesson
The theory — Page Replacement: FIFO, LRU and the Optimal Policy
Memory is divided into frames of equal size, and a program's data into pages of the same size, so any page fits any frame. The reference string is the sequence of pages a program touches. A reference to a page not in memory is a page fault: the page is loaded, and if no frame is free a replacement policy chooses which page to evict. With F frames, a policy shows Bélády's anomaly on a string if it faults more with F + 1 frames than with F.
What each symbol means
F- the number of frames
n- the number of pages in a loop (worked problem)
r- how many times the loop runs
- Assumes
- Demand paging: a page is loaded only when it is asked for, and memory starts empty, so the first reference to every page faults under every policy. When several pages are never needed again, OPT here evicts the lowest-numbered. Each reference is one page; reads and writes are not told apart, and an evicted page costs nothing to throw out. Exact LRU needs the time of every memory access, which is too costly to record, so real kernels approximate it with a referenced bit per page; the clock algorithm is the best known of these.
Problem solved in full
-
When LRU faults on every reference 5 steps
Press Loop, OPT and say where its 7 faults fall. Then take a program that sums an array filling 100 pages, 1,000 times over, with 99 frames. How many faults does LRU make, how many does OPT make, and how many with a 100th frame?
-
On the page, the first five references fault: four fill the empty frames and the fifth evicts. After that the faults fall at steps 9 and 13, four references apart: 5 + 2 = 7.
-
Why every four. With n − 1 frames and a loop of n pages, exactly one page of the loop is out of memory. At a fault, the page needed furthest ahead is the one read just before, which comes round again in n − 1 references (on the last pass some pages are not needed again, and OPT may evict one of those instead). Evicting it leaves the next n − 2 references in memory, so the next fault is n − 1 references later.
-
LRU with 99 frames: when page p faults, the frames hold every other page, and the one used longest ago is p + 1, the next page the loop asks for. Every one of the 100 × 1,000 = 100,000 references faults, as on the page, where all 15 did. FIFO evicts the same pages.
-
OPT: the first 99 references fill the frames. From the 100th on, a fault every 99 references, at references 100, 199, 298, …: 1 + ⌊(100,000 − 100) / 99⌋ = 1 + 1,009 = 1,010 of them. In all 99 + 1,010 = 1,109, which is n + ⌊n(r − 1)/(n − 1)⌋ with n = 100 and r = 1,000.
-
With a 100th frame the whole array fits: 100 faults under any of the three policies, one per page, and none after the first pass.
Answer
LRU 100,000, OPT 1,109, and 100 with one frame more. LRU is at its worst on a loop one page bigger than memory, and the one frame that closes the gap is worth a thousandfold.
-
Learning path
What a data structure makes cheap
References (1)
- The first insight's date and attribution: the anomaly, and its formal treatment for FIFO: L. A. Belady, R. A. Nelson and G. S. Shedler, "An anomaly in space-time characteristics of certain programs running in a paging machine," Communications of the ACM 12(6), 349–353, June 1969.