Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Page replacement: FIFO and LRU

After this lesson: I can simulate FIFO and LRU page replacement and count the page faults.

55 min lesson2 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

Which page do you throw out of memory?

Page replacement is the choice of which page to remove from memory when a new page must come in and every frame is full. A good choice avoids slow trips to the disk, so programs stay quick.

Two simple rules, first in first out and least recently used, show how the choice is made.

By the end of this lesson

Note

I can simulate FIFO and LRU page replacement and count the page faults.

Key words

WordWhat it meansExample
Page faulta request for a page that is not in memory
Reference stringthe list of pages a program asks for, in order1, 2, 3, 1
FIFOfirst in first out: remove the page that arrived earliest
LRUleast recently used: remove the page unused for longest
Swapdisk space used as spare memory
Hita request for a page that is already in memory

The cells students fill in are left blank in this preview.

A page fault is a missing page

Memory holds only some pages of each process. The rest wait on the disk, in a place called swap.

When a process asks for a page that is not in a frame, that is a page fault. The system must fetch the page from the disk. A disk is thousands of times slower than memory, so every fault costs real time.

If every frame is full, the system must also choose a page to remove.

Read the fault and swap counters since boot

Command
grep -E '^(pswpin|pswpout|pgfault) ' /proc/vmstat

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read how eagerly Linux uses swap

Command
cat /proc/sys/vm/swappiness

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

FIFO removes the page that arrived first

First in, first out keeps the pages in the order they arrived. When a frame is needed, the oldest page goes.

It is easy to build, because it is a plain queue. It ignores how often a page is used. The table follows the reference string 1, 2, 3, 1, 4, 1, 2, 5, 1, 2 with three frames.

Worked example: FIFO with three frames

Asks forResultFrames after, oldest first
1fault1
2fault1, 2
3fault1, 2, 3
1hit1, 2, 3
4fault, 1 removed2, 3, 4
1fault, 2 removed3, 4, 1
2fault, 3 removed4, 1, 2
5fault, 4 removed1, 2, 5
1hit1, 2, 5
2hit1, 2, 5
Total7 faults

The cells students fill in are left blank in this preview.

LRU removes the page unused for longest

Least recently used guesses that a page used a moment ago will be used again soon. It removes the page that has gone unused the longest.

On every hit the page moves to the newest end of the list. The same string now gives fewer faults, because page 1 is kept while it is in use.

Worked example: LRU with three frames

Asks forResultFrames after, least recent first
1fault1
2fault1, 2
3fault1, 2, 3
1hit2, 3, 1
4fault, 2 removed3, 1, 4
1hit3, 4, 1
2fault, 3 removed4, 1, 2
5fault, 4 removed1, 2, 5
1hit2, 5, 1
2hit5, 1, 2
Total6 faults

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think LRU never does worse than FIFO. On many strings it does better, but not on all of them. Take the string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 with three frames. FIFO has 9 faults and LRU has 10. Every rule is a guess about the future.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.