Transcript Memory Management
Memory Management
Virtual Memory Page replacement algorithms Chapter 4 Sec. 4.4
1
Structure of a page table entry
2
Issues with page tables
• They can be very large • It takes more time to access a memory location – Virtual page first has to be mapped to its corresponding physical frame • The spectrum of solutions – – High speed registers to implement the table Put entire page table in main memory 3
Page swapping
• What to do if the memory reference is to a page that is not in main memory?
– This is called a page fault – “Swap” a page out of main memory and “swap” the needed page in its place • The decision about which page to swap out is made by a page replacement algorithm 4
When a page fault occurs…
• Page fault forces choice about which page should be overwritten • Modified page may first have to be saved to the hard drive – But if is unmodified, it can just be overwritten • Best not to choose “popular” page – It will probably need to be brought back in soon 5
Page replacement algorithms
• Choosing the page that gets swapped out – Optimal – – NRU FIFO – – – Second Chance Clock LRU – – NFU Working Set – WSClock 6
Optimal Page Replacement Algorithm
• Replace page that will be needed at the farthest point in future – Optimal but unrealizable • Can estimate by logging page use on previous runs of a given process – Impractical 7
Optimal algorithm example Using 3 page frames
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 7 7 7 2 0 1 1 2 2 2 2 0 0 0 3 4 0 3 3 1 7 0 0 1
Total number of page faults is ___ . This algorithm (although the best) is implausible to implement since it requires future knowledge. It is used mainly for comparison studies.
8
Not Recently Used (NRU)
• Each page has
reference
and
modified
bits – bits are set when page is referenced, modified • Pages are classified not referenced, not modified: not referenced, modified: 0 0 0 1 how can it be?
referenced, not modified: 1 0 referenced, modified: 1 1 • Removes page at random from the lowest numbered non-empty class 9
FIFO
• Maintain a linked list of all pages – in order in which they came into memory • The page at the head of the list swapped out • Disadvantage ?
• Advantage?
10
FIFO example
Using 3 frames 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 7 7 7 2 2 2 4 4 4 0 0 0 7 7 7 0 0 0 3 3 3 2 2 2 1 1 1 0 0 1 1 1 0 0 0 3 3 3 2 2 2 1
Total of ___ faults.
Suppose we had 4 frames instead of 3, would there be fewer page faults? 11
Another FIFO example
Consider the reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 With three frames there are With four frames there are faults faults.
This is an example of
Belady's Anomaly
. 12
FIFO with 3 page frames
Belady's Anomaly?!?
FIFO with 4 page frames
P
's show which page references show page faults 13
Second Chance
FIFO modification
• A FIFO linked list is maintained, as before.
– The page at the head of the list is chosen to be swapped out unless its R bit is 0 • • In that case, it goes to the end of the list And the next process on the list is examined for swapping… 14
Clock Variation of second chance
15
Least Recently Used (LRU)
• Rationale is that pages used recently will be used again soon – replace pages that have been unused for longest time • If implemented with a linked list of pages – the list must be updated with every memory reference • Other techniques require hardware support – Counter is maintained • When a page is referenced, it receives the counter’s value – LRU matrix 16
LRU matrix
LRU using a matrix – pages referenced in order
0,1,2,3,2,1,0,3,2,3
17
LRU example
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 7 7 7 2 2 4 4 4 0 1 1 1 0 0 0 0 0 0 3 3 3 0 0 1 1 3 3 2 2 2 2 2 7
Total number of page faults is _____. This is often the best page-replacement algorithm that we can use. 18
Additional reference bits (
LRU enhancement )
• Instead of just clearing the bits every so often, we can keep extra information by having a reference
byte
• At regular intervals (100 ms, e.g.) a clock interrupt occurs – OS shifts the reference
bit
into the high order position of the reference
byte
, and shifts all other bits down to the right 19
Additional reference bits example
• For example, the reference byte is
01101101
and the reference bit is
1
when the timer expires.
• The reference byte now becomes
10110110,
and the reference bit is set back to 0 • Interpreting these bytes as unsigned integers, the least recently referenced page is the one with the smallest reference byte. 20
Not frequently used (NFU)
• Counter in each page table entry is initialized to 0 • At each clock interrupt, the value of the reference bit is added to the counter, and then set back to 0.
• At a page fault, the page with the smallest counter is swapped out.
• Not very useful, but a variation of it accounts for the aging of pages.
– This is a simulation of LRU – See pp. 220-221 21
Working Set / Locality Model
• • The
working-set strategy
starts by looking at how many frames a process is actually using.
This approach defines the
locality model
of process execution – As a process executes, it moves from one locality to another. • A locality is a set of pages actively used together – A program is composed of several (possibly overlapping) localities 22
What is locality?
• For example,
when a procedure is called, it defines a new locality
. When the procedure is exited, the locality changes • If we allocate enough frames to accommodate the process’ current locality – it will fault for the pages in its current locality until they are all in memory. – it will not fault again until it changes its locality.
23
Working-Set Model
• This model uses a parameter, k to define the
working-set window
• The idea is to examine the most recent k page references.
The set of pages in the most recent
k
page references is the working set
– If a page is in active use, it will be in the working set – If it is no longer being used, it will drop from the working set k units after its last reference 24
Example
A page reference string, with
k
= 10 2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4
t1 t2
At time t1 the working set is {1,2,5,6,7} At time t2 the working set is {3, 4} The accuracy of the working set depends upon the selection of
k.
if it is too small, it may not encompass the working set if it is too large, it may overlap several localities
25
Implementation
• The OS monitors the working-set of each process and allocates to that working-set enough frames to provide it with its working-set size – If there are any extra frames, another process can be started.
• If the sum of the working-sets increases, exceeding the total number of frames, a process is suspended. That process’ pages are written out and its frames reallocated.
26
Working Set
27
WSClock
28
Summary
29