page replacement
TRANSCRIPT
VIRTUAL MEMORY MANAGEMENT• Key concept : Demand paging
Load pages into memory only when a page fault occurs
• Issues :
1. Placement strategies
Place pages anywhere
2. Replacement strategies
What to do when there exist more jobs than can fit in memory
3. Load control strategies
Determining how many jobs can be in memory at one time
PAGE REPLACEMENT
• With demand paging, physical memory fills quickly
• When a process faults & memory is full, some page must
be swapped out
• Which page should be replaced?
1. Local replacement — Replace a page of the faulting
process
2. Global replacement — Possibly replace the page of
another process
PAGE REPLACEMENT
• Assume a normal page table User-program is executing
and Page Invalid Fault occurs!
• The page needed is not in memory
• Look at user process and figure out which page was
needed , Read the needed page into this frame
• Restart the interrupted process
• Retry the same instruction ,manipulates the machine
state
EVALUATION METHODOLOGY• Record a trace of the pages accessed by a process
Example: (Virtual) address trace...
(3,0), (1,9), (4,1), (2,1), (5,3), (2,0), (1,9), (2,4), (3,1), (4,8)• generates page trace 3, 1, 4, 2, 5, 2, 1, 2, 3, 4• Hardware can tell OS when a new page is loaded into the
TLB
a) Set a used bit in the page table entry
b) Increment or shift a register
• Simulate the behaviour of a page replacement algorithm on the trace and record the number of page faults generated
Fewer faults better performance
PAGE REPLACEMENT ALGORITHMS
• Which frame to replace?
• Algorithms:
1. Optimal Page Replacement Algorithm
2. FIFO
3. Least Recently Used (LRU)
4. Not Recently Used
5. Second Chance
6. Clock
7. Not Frequently Used (NFU)
• Suppose we have the same reference stream: – A B C A B D A D B C B
• Consider MIN Page replacement:
– MIN: 5 faults
EXAMPLE: MIN
C
DC
B
A
BCBDADBACBA
3
2
1
Ref:
Page:
A. Frank - P. Weisberg
OPTIMAL ALGORITHM
• Reference string : 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 4 frames example
• How do you know future use? You don’t!• Used for measuring how well your algorithm
performs.
1
2
3
4
6 page faults
4 5
First-In, First-Out (FIFO) algorithm
• Always replace the oldest page.
• “Replace the page that has been in memory for
the longest time.”
• Maintain a linked list of all pages
– Maintain the order in which they entered memory
• Page at front of list replaced
• Add new page to end of list
• Suppose we have 3 page frames, 4 virtual pages, and following reference stream:
– A B C A B D A D B C B• Consider FIFO Page replacement:
– FIFO: 7 faults.
– When referencing D, replacing A is bad choice, since need A again right away
Example: FIFO
C
B
A
D
C
B
A
BCBDADBACBA
3
2
1
Ref:
Page:
A. Frank - P. Weisberg
First-In-First-Out (FIFO) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames(3 pages can be in memory
at a time per process):
• 4 frames:
• FIFO Replacement manifests Belady’s Anomaly:– more frames ⇒ more page faults
1
2
3
1
2
3
4
1
2
5
3
4
9 page faults
1
2
3
1
2
3
5
1
2
4
5 10 page faults
44 3
FIFO
0
2
1
6
4
2
1
6
4
0
1
6
4
0
3
6
4
0
3
1
2
0
3
1
4 0 3 1 2
Fault Rate = 9 / 12 = 0.75
•Consider the following reference string: 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1 x x x x x x x x x
Compulsory Misses
First-In, First-Out (FIFO) algorithm
• Advantage: (really) easy to implement
• Disadvantage: page in memory the longest may be often
used
– This algorithm forces pages out regardless of usage
– Usage may be helpful in determining which pages to
keep
LRU (LEAST RECENTLY USED) ALGORITHM
• Replaces the page that has not been referenced
for the longest time:
– By the principle of locality, this should be the
page least likely to be referenced in the near
future.
– performs nearly as well as the optimal policy.
A. Frank - P. Weisberg
LEAST RECENTLY USED (LRU) ALGORITHM
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
8 page faults
5
2
4
3
1
2
3
4
1
2
5
4
1
2
5
3
1
2
4
3
Comparison of OPT with LRU
• Example: A process of 5 pages with an OS that fixes the resident set size to 3.
• Consider the following: A B C D A B C D A B C D• LRU Performs as follows (same as FIFO here):
– Every reference is a page fault!• MIN Does much better:
D
When will LRU perform badly?
C
B
A
D
C
B
A
D
C
B
A
CBADCBADCBA D
3
2
1
Ref:
Page:
B
C
DC
B
A
CBADCBADCBA D
3
2
1
Ref:
Page:
• Each page could be tagged (in the page table entry) with
the time at each memory reference.
• The LRU page is the one with the smallest time value
(needs to be searched at each page fault).
• This would require expensive hardware and a great deal
of overhead.
• Consequently very few computer systems provide
sufficient hardware support for true LRU replacement
policy. Other algorithms are used instead.
IMPLEMENTATION OF THE LRU POLICY
1. Counter implementation:• Every page entry has a counter; every time a page is
referenced through this entry, copy the clock into the counter.
• When a page needs to be changed, look at the counters to determine which are to change.
2. Stack implementation • keep a stack of page numbers in a double link form:• Page referenced:
- move it to the top- requires 6 pointers to be changed
• No search for replacement.
LRU IMPLEMENTATIONS
Reference Bit :
• With each page associate a bit, initially = 0
• When page is referenced, bit is set to 1.
• Replace the one which is 0 (if one exists)
• we do not know the real order of use, however.
LRU APPROXIMATION ALGORITHMS
NOT Recently Used Page Replacement Alg.
• The Not Recently Used Page Replacement Alg.
Use the Referenced Bit and the Dirty Bit• Initially, all pages have Referenced Bit = 0, Dirty Bit = 0
(e.g. whenever a clock tick (timer interrupt) occurs)
• Clear the Referenced Bit
•The Not Recently Used Page Replacement Alg.
•When a page fault occurs... Categorize each page...•Class 1:
Referenced = 0 Dirty = 0•Class 2:
Referenced = 0 Dirty = 1
•Class 3:
Referenced = 1 Dirty = 0•Class 4:
Referenced = 1 Dirty = 1
• Choose a page from class 1.
•If none, choose a page from class 2.
•If none, choose a page from class 3.•If none, choose a page from class 4.
• The set of frames candidate for replacement is considered as a circular buffer.
• When a page is replaced, a pointer is set to point to the next frame in buffer.
• A reference bit for each frame is set to 1 whenever: - a page is first loaded into the frame.- the corresponding page is referenced.
• When it is time to replace a page, the first frame encountered with the reference bit set to 0 is replaced:- During the search for replacement, each reference bit set to 1 is changed to 0.
THE CLOCK (SECOND CHANCE) POLICY
A. Frank - P. Weisberg
Comparison of Clock with FIFO and LRU
• Asterisk indicates that the corresponding use bit is set to 1.• The arrow indicates the current position of the pointer.
• Keep a counter of the number of references that have
been made to each page.
• Two possibilities: Least/Most Frequently Used
(LFU/MFU).
• LFU Algorithm: replaces page with smallest count;
others were and will be used more.
• MFU Algorithm: based on the argument that the page
with the smallest count was probably just brought in
and has yet to be used.
COUNTING-BASED ALGORITHMS
PAGE BUFFERING
• Pages to be replaced are kept in main memory for a while to guard against poorly performing replacement algorithms such as FIFO.
• Two lists of pointers are maintained: each entry points to a frame selected for replacement:1. a free page list for frames that have not been
modified since brought in (no need to swap out).2. a modified page list for frames that have been
modified (need to write them out).• A frame to be replaced has a pointer added to the tail of
one of the lists and the present bit is cleared in corresponding page table entry; but the page remains in the same memory frame.
• At each page fault the two lists are first examined to see if the needed page is still in main memory:If it is, we need to set the present bit in the corresponding page table entry (and remove the matching entry in the relevant page list).If it is not, then the needed page is brought in, it is placed in the frame pointed by the head of the free frame list the head of the free frame list is moved to the next entry.
• The modified list also serves to write out modified pages in cluster (rather than individually).
PAGE BUFFERING