Various page replacement algorithms in the context of virtual memory management, including fifo, lru, and optimal replacement. Additionally, it discusses the concept of working sets and their role in improving locality. Page fault handling, page allocation and replacement, and the need for page replacement. It also touches upon belady's anomaly and the tradeoffs between global and local allocation.
Download Virtual Memory: Page Replacement Algorithms and Working Sets - Prof. Michael W. Hicks and more Study notes Operating Systems in PDF only on Docsity!
CMSC 412
Virtual Memory
Virtual Memory
Paging is a typical feature of broader
support for virtual memory
Pages can be swapped to and from disk
Presents the illusion of a larger physical memory
Virtual addressing simplifies model
Swapping: can load pages into different addresses in physical memory
Addressing: different processes have their own address spaces, starting at 0, separate from other processes
Elements of Virtual Memory
• Page Fault Handler
What happens when the referenced page
is not accessible?
• Page Allocation and Replacement
If page is on disk, swapper must load it
into memory and update the page table.
If no pages available to load into
memory, must evict a page to make room
Which page? Depends on the page replacement algorithm.
Page Replacement
Prevent over-allocation of memory by modifying page-fault service routine to include page replacement.
Use modify (a.k.a.dirty) bit to reduce overhead of page transfers – only modified pages are written to disk.
Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory.
Page Replacement
Expected Page Faults Versus The
Number of Frames
FIFO Page Replacement
• Replace the page that was brought in
longest ago
• However
Old pages may be hot (frequently used)
Belady’s anomaly: number of page faults
may increase when one increases number
of page frames!
FIFO Example (3 frames)
Reference string: 1,2,3,4,1,2,5,1,2,3,4,
access 1 - (1) fault
access 2 - (1,2) fault
access 3- (1,2,3) fault
access 4 - (2,3,4) fault, replacement
access 1 - (3,4,1) fault, replacement
access 2 - (4,1,2) fault, replacement
access 5 - (1,2,5) fault, replacement
access 1- (1,2,5)
access 2 - (1,2,5)
access 3 - (2,5,3) fault, replacement
access 4 - (5,3,4) fault, replacement
access 5 - (5,3,4)
9 page faults
Optimal Page Replacement
• Replace the page that will be used
furthest in the future
• Good algorithm(!) but requires
knowledge of the future
With compiler assistance, knowledge of
the future is sometimes possible.
• Used as point of comparison with
other algorithms.
Optimal Example (4 frames)
Reference string: 1,2,3,4,1,2,5,1,2,3,4,
access 1 - (1) fault
access 2 - (1,2) fault
access 3- (1,2,3) fault
access 4 - (1,2,3,4) fault
access 1 - (1,2,3,4)
access 2 - (1,2,3,4)
access 5 - (1,2,3,5) fault, replacement
access 1 - (1,2,3,5)
access 2 - (1,2,3,5)
access 3 - (1,2,3,5)
access 4 - (4,2,3,5) fault, replacement
access 5 - (4,2,3,5)
6 Page faults
LRU Page Replacement
• Replace the page that was used
longest ago
Best known non-clairvoyant algorithm.
• Implementation of LRU can be
expensive
Maintain a stack of nodes representing
pages and put page on top of stack when
the page is accessed
Maintain a time stamp associated with
each page
LRU Example (3 frames)
Reference string: 1,2,3,4,1,2,5,1,2,3,4,
access 1 - (1) fault
access 2 - (1,2) fault
access 3- (1,2,3) fault
access 4 - (2,3,4) fault, replacement
access 1 - (3,4,1) fault, replacement
access 2 - (4,1,2) fault, replacement
access 5 - (1,2,5) fault, replacement
access 1- (2,5,1)
access 2 - (5,1,2)
access 3 - (1,2,3) fault, replacement
access 4 - (2,3,4) fault, replacement
access 5 - (3,4,5) fault, replacement
10 page faults
Approximate LRU
• Maintain reference bit(s) which are set
(by HW) whenever a page is used
at the end of a given time period (e.g., at
a page fault, regular timeout, when
idle…), reference bits are cleared.
How does the interval affect performance?
• Replace the one which is 0 (if one
exists, else FIFO).
Second-chance Algorithm
• For each page, maintain
Reference bit (by HW)
Page Arrival time (by OS)
• If page to be replaced (in clock order)
has reference bit = 1 then:
set reference bit to 0, set arrival time.
leave page in memory.
replace next page (in clock order),
subject to same rules.
Second-Chance (clock) Page-
Replacement Algorithm
Second Chance + Modify
• Use the modify bit as well as the
reference bit
• How would you choose the “best”
page given this information
see text, Sec. 9.4.5.3.
Priority Allocation
• Use a proportional allocation scheme
using priorities rather than size.
• If process Pi generates a page fault,
select for replacement one of its frames.
select for replacement a frame from a
process with lower priority number.
Global vs. Local Allocation
• Global replacement – process selects a
replacement frame from the set of all
frames; one process can take a frame
from another.
• Local replacement – each process
selects from only its own set of
allocated frames.
• Tradeoff: predictability vs. throughput
Thrashing
Can allocate so much virtual memory that
the system spends all its time getting pages
the situation is called thrashing
To stop this, swap a process
write all of the memory of a process out to disk
don’t run the process for a period of time
part of medium term scheduling
Why does thrashing happen? How to
identify it?
Paging and Thrashing
Why does paging work? Locality model - Process migrates from one locality to another. - Localities may overlap.
Why does thrashing occur? size of locality > allocated memory size
Improving Locality
Malloc may not ensure spatial locality
Two calls to malloc could get memory on different cache lines, pages, etc.
What about the stack?
Option 1:
Malloc a large chunk of memory and parcel it out yourself
Option 2:
Add a “near” hint parameter to malloc
Indicates that memory should be allocated near the target location - It’s only a performance hint, and malloc can ignore it - Allows locality improvement without major changes
Considering Working Sets
Δ ≡ working-set window ≡ a fixed number of
page references
Example: 10,000 instructions
WSSi (working set of Process Pi ) =
total number of pages referenced in the
most recent Δ (varies in time)
if Δ too small will not encompass entire locality.
if Δ too large will encompass several localities.
if Δ = ∞ ⇒ will encompass entire program.
D = Σ WSSi ≡ total demand frames
if D > m ⇒ Thrashing
Policy if D > m, then suspend one of the
processes. (Which?)
Working-set Example
Tracking the Working Set
Approximate with interval timer + a
reference bit
Example: Δ = 10,
Timer interrupts after every 5000 time units.
Track the last two reference bits per page.
Whenever the timer interrupts, save each reference bit to memory, then reset it to 0.
If either the reference bit or the saved bits in memory = 1 ⇒ page in working set.
Why is this not completely accurate?
Improvement = 10 bits and interrupt every
1000 time units.
Windows NT
Uses demand paging with clustering. Clustering brings in pages surrounding the faulting page.
Processes are assigned working set minimum and working set maximum.
Working set minimum is the minimum number of pages the process is guaranteed to have in memory.
A process may be assigned as many pages up to its working set maximum.
When the amount of free memory in the system falls below a threshold, automatic working settrimming is performed to restore the amount of free memory.
Working set trimming removes pages from processes that have pages in excess of their working set minimum.
Solaris 2
Maintains a list of free frames to assign faulting processes.
Lotsfree – threshold parameter to begin paging.
Paging is peformed by pageout process.
Pageout scans pages using modified clock algorithm.
Scanrate is the rate at which pages are scanned. This ranged from slowscan to fastscan.
Pageout is called more frequently depending upon the amount of free memory available.