Page Replacement: The Problem
Chapter Eighty-Four
Syllabus topic Module 2, "Virtual Memory Management - Page Replacement"
Pages 337 to 340 of 452
In one line
When a page must come in and no frame is free, the operating system chooses a page already in memory, writes it out if it has been changed, and takes its frame.
How the machine gets into this state
Demand paging lets the operating system promise more memory than it has, and the promise is usually safe because no process uses all of its pages at once. It is called over allocation, and it is deliberate.
| The machine has | The processes have been promised | Why it works |
|---|---|---|
| 262,144 frames | forty processes of a million pages each | each is using a few hundred pages at any moment |
It works until a moment when everybody's working part grows at once. Then a page is needed, and every frame is taken.
The three things that can be done
| Option | Why it is or is not acceptable |
|---|---|
| terminate the process | unacceptable. Paging is meant to be invisible to the program, and a program that dies because the machine is busy is not invisible |
| swap a whole process out, as in Chapter sixty nine | acceptable, and still used when memory is desperately short. It is crude: a whole process is stopped to free frames a few of which are needed |
| page replacement | the answer: free one frame by choosing a page nobody will need soon |
The fault service routine, with replacement
Chapter eighty one's list, with the new steps in it. This is the sequence a question asks for.
| Step | What happens |
|---|---|
| 1 | find where the wanted page is on disk |
| 2 | look for a free frame |
| 3 | if there is one, use it |
| 4 | if there is not, run a page replacement algorithm to choose a victim |
| 5 | if the victim has been modified, write it out to disk |
| 6 | mark the victim invalid in its owner's page table, and remove it from the TLB |
| 7 | read the wanted page into the freed frame |
| 8 | set the entry valid, and restart the faulting instruction |
Steps 5 and 6 are where the marks are.
Step 5 can double the cost of the fault, because it is a second disk transfer: one out, one in. Chapter eighty two priced it exactly: with 70 per cent of victims dirty the average fault went from 8 milliseconds to 13.6, and the program from 81 to 137 times slower.
Step 6 is the one that is forgotten and the one that corrupts memory. The victim's owner must not be able to reach that frame any more, and a stale TLB entry is a way to reach it. Chapter seventy six said the same thing about any change to a page table entry.
Page Replacement: The Problem
The modify bit earns its keep
The hardware sets the modify bit, also called the dirty bit, when a page is written to. The replacement code reads it.
| The victim is | What has to happen | Cost |
|---|---|---|
| clean: never written since it came in | nothing. The copy on disk is still correct, so the frame is simply taken | one transfer, the read |
| dirty: written to | it must be written out before the frame is reused | two transfers |
So an algorithm that can choose between two equally good victims should choose the clean one, and a page of read-only code is always clean, which makes program text the cheapest thing in memory to throw away: it can always be read again from the program file.
What makes one algorithm better than another
The lowest page fault rate on the same reference string with the same number of frames. Nothing else, and in particular not how clever it sounds.
| Term | What it means |
|---|---|
| reference string | the sequence of page numbers a program touches, in order |
| frames | how many frames this process is allowed. It is the other half of every question |
| page fault rate | faults divided by references, which is what Chapter eighty two's p is |
A reference string is page numbers, not addresses. The offset inside the page makes no difference to whether the page is in memory, so the addresses 0, 100 and 4000 with a 4096 byte page are all page 0 and are one reference for this purpose.
And a repeat costs nothing: two references in a row to the same page can cause at most one fault, so a string 1 1 1 2 behaves exactly like 1 2. That is worth knowing in the hall, because it shortens a long string before the work starts.
More frames, fewer faults
The relationship every question assumes, computed here on the reference string this book uses throughout the next four chapters:
Faults: frames 1 to 7
Reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Twenty references over six distinct pages. The faults, counted by sim/pagerepl.py:
| Frames | FIFO | Optimal | LRU |
|---|---|---|---|
| 1 | 20 | 20 | 20 |
| 2 | 15 | 13 | 17 |
| 3 | 15 | 9 | 12 |
| 4 | 10 | 8 | 8 |
| 5 | 9 | 7 | 7 |
| 6 | 6 | 6 | 6 |
| 7 | 6 | 6 | 6 |
Three things to take from that table, and each is a question in itself.
One: with one frame every reference faults. Twenty references, twenty faults, because no page survives to the next reference.
Two: with six frames all three algorithms give six faults, which is the number of distinct pages. Once there are as many frames as the program has pages, the algorithm stops mattering: every page is brought in once and never thrown out. That is why a question always gives a frame count smaller than the number of distinct pages.
Page Replacement: The Problem
Three: more frames is not a guarantee. FIFO gives 15 faults at two frames and 15 again at three: the extra frame bought nothing at all on this string. Chapter eighty five shows a string where an extra frame makes FIFO worse, which is Belady's anomaly, and it is found by search rather than asserted.
The five algorithms this module needs
| Chapter | Algorithm | Chooses as victim |
|---|---|---|
| 30 | FIFO | the page that came in earliest |
| 31 | Optimal | the page that will not be used for the longest time to come |
| 32 | LRU | the page that was used longest ago |
| 33 | Second chance, and the counting algorithms LFU and MFU | the oldest page that has not been used since last time, or the least or most frequently used |
| 34 | (comparison) |
Every one of them is the same three steps, and only the choice of victim changes.
Distinctions that carry marks
| Swapping out a process | Page replacement | |
|---|---|---|
| Frees | every frame the process has | one frame |
| Stops | the whole process | nothing: the faulting process was waiting anyway |
| Decided by | the medium term scheduler | the replacement algorithm, at the fault |
| Clean victim | Dirty victim | |
|---|---|---|
| The modify bit is | 0 | 1 |
| Transfers needed | one | two |
| Example | a page of program code | a page of the heap that has been written |
What it does not mean
Replacement is not swapping. One page moves, not a process.
The victim need not belong to the faulting process. Whether it may belong to another one is the global against local question of Chapter ninety.
A page fault is not the algorithm's fault. The algorithm decides only which page leaves, and therefore how many faults happen later.
More frames does not always mean fewer faults. It usually does, and FIFO can break the rule.
The reference string is not the addresses. It is the page numbers, and repeats next to each other collapse.
Quick revision
- Demand paging over allocates memory on purpose; when every frame is taken and a page is
needed, something must give.
- Of the three options, terminating the process is unacceptable, swapping a whole process out is
crude, and page replacement frees one frame.
- The service routine: find the page on disk, look for a free frame, else choose a victim,
write it out if dirty, invalidate it in its page table and in the TLB, read the page in, restart the instruction.
- A dirty victim costs two transfers and a clean one costs a single read; program
Page Replacement: The Problem
text is always clean.
- An algorithm is judged by the
page fault rate on the same reference string with the same number of frames.
- A reference string is page numbers, and consecutive repeats of a page cause at most one
fault.
- On the standard string of twenty references over six pages: one frame gives 20 faults
for every algorithm; six frames give 6 for every algorithm; three frames give 15 for FIFO, 12 for LRU and 9 for optimal.
- FIFO gives 15 faults at both two and three frames, and Chapter eighty five shows it getting
worse with more frames.
Test yourself
- How can the operating system run out of frames if it is managing memory properly? Because
demand paging lets it promise more memory than exists, which is safe while no process needs all its pages, and stops being safe when several processes need more at the same moment.
- Why is terminating a process not an acceptable answer? Paging is meant to be invisible to
the program; a program that dies because other programs are busy is not.
- Give the steps of servicing a fault when no frame is free. Find the page on disk; find no
free frame; choose a victim by the replacement algorithm; write the victim out if it is dirty; mark it invalid in its page table and remove it from the TLB; read the wanted page in; set the entry valid; restart the instruction.
- Why does the modify bit matter, and by how much? A clean victim needs one transfer and a
dirty one needs two, so the bit can halve the cost of a fault; Chapter eighty two's sum put the difference at 81 against 137 times slower.
- What is forgotten at step 6, and why is it serious? Removing the victim from the TLB. A
stale entry lets its former owner reach a frame that now belongs to somebody else.
- On what basis is a replacement algorithm judged? The page fault rate it gives on the same
reference string with the same number of frames. 7. The addresses 0, 100 and 4000 are touched in turn, with a 4096 byte page. How long is the reference string? One reference: all three addresses are in page 0.
- With six frames and six distinct pages, which algorithm is best? None of them: every page
is loaded once and never replaced, so all give six faults. The algorithm matters only when the frames are fewer than the pages in use.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself, or the past papers, for the same subject.