notes os
notes os
size
speed
< 1 cycle
registers cost
a few cycles
cache
memory <100 ns
disk a few ms
1
Virtual memory motivation
Previous approach to memory management
Must completely load user process in memory
One large AS or too many AS out of memory
2
Virtual memory idea
3
Virtual memory illustration
4
Virtual memory operations
Detect reference to page on disk
5
Detect reference to page on disk and
recognize disk location of page
Overload the valid bit of page table entries
6
Steps in handling a page fault
7
OS decisions
Page selection
When to bring pages from disk to memory?
Page replacement
When no free pages available, must select victim
page in memory and throw it out to disk
8
Page selection algorithms
Demand paging: load page on page fault
Start up process with no pages loaded
Wait until a page absolutely must be in memory
9
Page replacement algorithms
Optimal: throw out page that won’t be used for
longest time in future
10
Evaluating page replacement algorithms
11
Optimal algorithm
Throw out page that won’t be used for longest
time in future
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 1 1 1 1 4 4
2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 5 5 5 5 5 5
6 page faults
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 5 5 5 5 4 4
2 2 2 2 2 2 1 1 1 1 5
3 3 3 3 3 3 2 2 2 2
4 4 4 4 4 4 3 3 3
10 page faults
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 4 4 4 5 5 5 5 5 5
2 2 2 1 1 1 1 1 3 3 3
3 3 3 2 2 2 2 2 4 4
9 page faults
15
FIFO illustrating belady’s anomaly
16
Least-Recently-Used (LRU) algorithm
8 page faults
18
Implementing LRU: software
19
LRU: concept vs. reality
LRU is considered to be a reasonably good
algorithm
20
Clock (second-chance) algorithm
Idea
A reference bit per page
Memory reference: hardware sets bit to 1
Page replacement: OS finds a page with reference
bit cleared
OS traverses all pages, clearing bits over time
22
A single step in Clock algorithm
23
Clock algorithm example
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 1 1 1 1 1 1 5 1 5 1 5 1 5 1 4 1 4 1
2 1 2 1 2 1 2 1 2 1 2 0 1 1 1 1 1 1 1 0 5 1
3 1 3 1 3 1 3 1 3 0 3 0 2 1 2 1 2 0 2 0
4 1 4 1 4 1 4 0 4 0 4 0 3 1 3 0 3 0
10 page faults
25
Clock algorithm extension (cont.)
Use dirty bit to give preference to dirty pages
On page reference
Read: hardware sets reference bit
Write: hardware sets dirty bit
Page replacement
reference = 0, dirty = 0 victim page
reference = 0, dirty = 1 skip (don’t change)
reference = 1, dirty = 0 reference = 0, dirty = 0
reference = 1, dirty = 1 reference = 0, dirty = 1
advance hand, repeat
If no victim page found, run swap daemon to flush
unreferenced dirty pages to the disk, repeat
26
Summary of page replacement algorithms
Optimal: throw out page that won’t be used for longest time
in future
Best algorithm if we can predict future
Good for comparison, but not practical
Random: throw out a random page
Easy to implement
Works surprisingly well. Why? Avoid worst case
Random
FIFO: throw out page that was loaded in first
Easy to implement
Fair: all pages receive equal residency
Ignore access pattern
LRU: throw out page that hasn’t been used in longest time
Past predicts future
With locality: approximates Optimal
Simple approximate LRU algorithms exist (Clock)
27
Current trends in memory management
28