Title: Virtual Memory
1Virtual Memory
- Virtual memory separation of user logical
memory from physical memory. - Only part of the program needs to be in memory
for execution - some can be on disk - Disk address can be stored in place of frame
number - A validinvalid bit is associated with each page
table entry (1 ? in-memory, 0 ? not-in-memory) - During address translation, if validinvalid bit
in page table entry is 0 ? page fault ? bring in
to memory
2Page Table When Some Pages Are Not in Main Memory
- Sum logical address space can therefore be much
larger than physical address space. - More processes
3Page Faults
- Bringing a Page into Memory
- Get empty frame.
- Swap page into frame.
- Reset tables, validation bit 1.
- Restart instruction
4Need For Page Replacement
5What happens if there is no free frame?
- Page replacement find some page in memory, but
not really in use, swap it out. - Use modify (dirty) bit to reduce overhead of page
transfers only modified pages are written to
disk.
6Basic Page Replacement
- Find the location of the desired page on disk.
- Find a free frame
- If there is a free frame, use it.
- If there is no free frame, use a page
replacement algorithm to select a victim frame. - If victim is dirty, write it out to disk
- Read the desired page into the (newly) free
frame. Update the page and frame tables. - Restart the process
7Page Replacement Policies
- 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 - only for
crucial OS processes - Local replacement each process selects from
only its own set of allocated frames - the normal
case - I/O interlock
- Pages that are used for copying a file from a
device must be locked
8Thrashing
- VM works because of locality
- Process migrates from one locality to another.
- Localities may overlap
- What happens if? size of localities gt total
memory size
9Thrashing
- If a process does not have enough pages for its
locality, the page-fault rate is very high. - Thrashing ? a process is busy swapping pages in
and out. - Thrashing may lead to
- low CPU utilization.
- operating system thinks that it needs to increase
the degree of multiprogramming. - another process added to the system
- It is important to allocate enough frames to each
process - If thats not possible, the process must be
swapped out
10Allocation of Frames
- Each process needs minimum number of frames
- Example IBM 370 6 pages to handle SS MOVE
instruction - instruction is 6 bytes, might span 2 pages.
- 2 pages to handle from.
- 2 pages to handle to.
- Two major allocation schemes.
- fixed allocation
- priority allocation
11Fixed Allocation
- Equal allocation e.g., if 100 frames and 5
processes, give each 20 frames. - Proportional allocation Allocate according to
the size of process.
12Priority 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.
13Page-Fault Frequency Scheme
- Establish acceptable page-fault rate.
- If actual rate too low, process loses frame.
- If actual rate too high, process gains frame.
14Initial Load
- Load pages predicted to be needed (compiler
flags) - or
- Load no pages
- Allows for more efficient process creation
15Performance of Demand Paging
- Page Fault Rate 0 ? p ? 1.0 (hopefully low, e.g.
5) - if p 0 no page faults
- if p 1, every reference is a fault
- Effective Access Time (EAT)
- EAT (1 p) x memory access
- p (page fault overhead
- swap page out
- swap page in
- restart overhead)
- Values
- Assume memory access roughly 1 (with TLB hits)
- Page fault overhead is small
- Swap is about 10000
- Restart is small
16Page Replacement Algorithms
- Want lowest page-fault rate (disk is slow)
- Evaluate algorithm by running it on a particular
string of page references (reference string) and
computing the number of page faults on that
string. - A page fault means a disk access (or two), and
thats where the time goes. - Assume no initial load, local replacement, no I/O
(no interlock issues)
17Optimal Algorithm
- Replace page that will not be used for longest
period of time in future. - 3 frames example 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0
1 7 0 1 (9) - 4 frames example 1 2 3 4 1 2 5 1 2 3 4 5 (6)
- But the future is unknown (like SJF scheduling)
18First-In-First-Out (FIFO) Algorithm
- 3 frames example 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0
1 7 0 1 (15)
19FIFO Illustrating Beladys Anamoly
- 4 frames example 1 2 3 4 1 2 5 1 2 3 4 5 (10)
- 3 frames example 1 2 3 4 1 2 5 1 2 3 4 5 (9)
- Beladys Anomaly - more frames does not imply
less page faults
20Counting Algorithms
- Keep a counter of the number of references that
have been made to each page. - Count is reset each time a page is brought in
- LFU Algorithm replaces page with smallest
count. - May delay start of counting to avoid bias from an
initial rush - MFU Algorithm based on the argument that the
page with the smallest count was probably just
brought in and has yet to be used. - See example in Geoffs notes
21LRU Page Replacement
- 3 frames example 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0
1 7 0 1 (11) - 4 frames example 1 2 3 4 1 2 5 1 2 3 4 5 (?)
22Least Recently Used (LRU) Algorithm
- Counter implementation
- Every page entry has a counter every time 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. - 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
- Both need HW support, as they are done very very
often
23Use Of A Stack to Record The Most Recent Page
References
24LRU Approximation Algorithms
- Usually not enough HW support for full LRU, so
- Reference bit in the page table
- With each page associate a bit, in HW initially
0 - When page is referenced bit set to 1.
- Replace the one which is 0 (if one exists). We
do not know the order, however. - Clear when all 1
- Reference bytes in the page table
- Make top bit the reference bit
- Shift right reference byte at regular intervals
- Victim is page with smallest value byte
- Second chance bit associated with each frame
- Looks through frames in order, with wrap-around,
starting at frame after last loaded - If frame has bit 1 then
- set bit 0.
- leave page in memory.
- replace next page, subject to same rules.
25Second-Chance (clock) Page-Replacement Algorithm
26Enhanced Second Chance
- Keep a reference bit and a modified bit with each
frame - 0,0 gt not recently used or modified gt best to
replace - 0,1 gt not recently used but modified gt must be
written out - 1,0 gt recently used but not modified gt may be
used again soon - 1,1 gt Recently used and modified gt best keep
this one in RAM - Victim selection
- After each load, the current position is set to
the frame after the loaded one. - Scan from current position round for a 0,0 frame
- If not found, scan round for a 0,1 frame,
reseting the use bit (ala 2nd chance) - If not found, start again (a victim must be
found) - Used by Mac OS
27Page Buffering
- Keep some frames free -a frame pool
- Immediately swap in on page fault
- Write out while first process uses CPU
- Remember what pages are in the frame pool, in
case they are requested again - used by VMS with
FIFO to recover from preemptive removal - Write out modified pages whenever the swap disk
is idle
28Other Techniques
- Pre-paging - predicting page needs
- Windows NT clustering
- Copy-on-Write
- Copy-on-Write (COW) allows both parent and child
processes to initially share the same pages in
memory. - If either process modifies a shared page, only
then is the page copied. - COW allows more efficient process creation as
only modified pages are copied.
29Other Considerations
- Program structure
- int A new int10241024
- Each row is stored in one page
- Program 1 for (j 0 j lt A.length j) for
(i 0 i lt A.length i) Ai,j 01024 x
1024 page faults - Program 2 for (i 0 i lt A.length i) for
(j 0 j lt A.length j) Ai,j 0 - 1024 page faults
30Windows 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 set
trimming 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. - On single x86 CPUS, uses a second chance style
algorithm to select victims - On Alpha and SMP, uses a modified FIFO
31Solaris 2
- Maintains a list of free pages to assign faulting
processes. - Lotsfree threshold parameter to begin pageout.
- Pageout is called more frequently depending upon
the amount of free memory available. - Pageout scans pages using variation on the second
chance algorithm (2 handed clock algorithm) - Scanrate is the rate at which pages are scanned.
This ranged from slowscan to fastscan. This
increases as free memory decreases - Handspread affects time process has to reuse a
page before the big-hand send the page out - Pages from shared libraries are kept in memory
(recent variation)