When a page must be removed from memory to make room for another,
this is a replacement.
The rules for deciding which page form a replacement policy.
[ Worksheet ]
Page references.
As a program runs, it generates a series of memory references.
The page number parts of these are a series of page references.
The first new page references are placed in unused frames. When
they're all full, a replacement algorithm will need to decide
which to remove.
Optimal replacement.
Replace the page that will remain unused for the longest time.
Cannot be implemented without knowledge of the future.
Represents the upper limit of any possible algorithm.
Least-Recently Used (LRU).
Replace the page that has remained unused for the longest time.
Best approximation of optimal.
Too expensive to be practical.
Would need to keep all the pages in a linked list.
Update on each memory reference.
(Or something worse.)
The base practical algorithms approximate LRU.
The page table contains to bits manipulated by hardware.
The referenced bit it set whenever the PTE is used.
The modified bit is set whenever the PTE is used for any operation
which stores into memory.
The OS can clear these bits.
If the referenced bit is cleared periodically, it means
“used recently.”
Not Recently Used
Periodically clears the ref bit.
Pages fall into one four categories based on those bits:
M
R
Class
0
0
Class 0, not referenced, not modified
0
1
Class 1, referenced, not modified
1
0
Class 2, not referenced, modified
1
1
Class 3, referenced and modified
Chooses to a random page from the lowest-numbered non-empty
category. Can't all be empty, unless there are no pages.
Un-modified pages are preferred, since they can be removed without
writing back to disk.
FIFO and friends.
First-In, First-Out (FIFO) Algorithm.
Simply remove the page that has been in memory the longest.
Keep the pages in a queue.
This is a terrible algorithm.
FIFO with second-chance. FIFO with this change:
When a page reaches the front of the queue, if its R bit is 0,
replace it.
If its R bit is 1, clear the bit, move the page to the end of
the queue, and repeat.
Clock algorithm:
A simpler implementation of second chance.
Keep the pages in a circular list. Probably just rotate through the real page numbers using
a modular increment.
On a fault, the hand moves and clears referenced pages. It evicts
the first un-referenced one found.
Page aging.
Each page is assigned an “age.”
It is a measure of how often the page has been referenced recently.
It is not a time measure.
It's not really anything like an age.
The system updates ages of all pages at some regular period.
For each page:
The age is shift right, and the referenced bit is shifted into
the left bit position.
The referenced bit is cleared.
The age is larger when
It is used more often (contains more ones).
Was used more recently (one shifted into the left more recently).
Not Frequently Used (NFU)
Like aging, except the referenced bit is simply added to the
age instead of shifted into the left. The age is simply a sum of the reference bits over time.
Performs poorly because it treats two references the same
regardless of when they occurred.
Working Set
Programs need a certain set of pages.
Working set algorithm is based on keeping an estimate of that set
in memory.
Working set.
The working set of size k at time t, given as
w(k,t), is the set of pages referred to in the last
k memory references at time t.
The time t should be a count of memory references, but usually
time is used.
For instance:
Given the sequence of page references (ending at t at left,
with older references to the right):
10 15 10 12 10 15 27 10 15 27 19 10 15 19 12 15
Produce the following values:
w(1,t)
= {10}
w(2,t) through w(3,t)
= {10,15}
w(4,t) through w(6,t)
= {10,12,15}
w(7,t) through w(10,t)
= {10,12,15,27}
w(11,t) through w(17,t)
= {10,12,15,19,27}
Increasing function of k:
OS Furniture.
Each process has an age, which is just the amount of time it has run.
When the OS moves the process to the running state, record the
current clock time as the start time.
When the OS moves the process from the running state,
add the difference between the current clock time and
the start time to the process age.
Each page has a last-referenced time.
Periodically check each page. If the referenced bit is one,
the last-referenced time of the page is set to
the age of the process which the page belongs to.
Then clear the ref bit.
Each page has an age, which is simply the current time less
the last-referenced time.
Basic procedure
Don't know the optimal k value, so pick one and call it
τ. Try to keep w(τ,t) pages in memory for each
process.
On a fault, evict a page whose age is greater than τ.
WS-Clock algorithm. On a fault:
Examine the current page. If R = 1, clear it
and try the next page.
If the page age ≤ τ, try the next page. (Third chance?)
Else (the page age > τ)
If M = 0, evict this page. Done.
If M = 1, schedule a write, and try the next page. (Not
a fourth chance, just house arrest.)
The number of writes scheduled may be limited to a reasonable maximum.
If the hand goes all around and finds not candidate,
If some writes were issued, wait for one to finish, and use that.