Chapter 9:Virtual Memory
Joe McCarthy
CSS 430: Operating Systems - Virtual Memory 1
Chapter 9: Virtual Memory• Background• Demand Paging• Copy-on-Write• Page Replacement• Allocation of Frames • Thrashing• Memory-Mapped Files• Allocating Kernel Memory• Other Considerations• Operating-System Examples
Material derived, in part, fromOperating Systems Concepts with Java, 8th Ed.
© 2009 Silberschatz, Galvin & Gagne
2CSS 430: Operating Systems - Virtual Memory
Virtual Memory
3CSS 430: Operating Systems - Virtual Memory
Background
• Virtual memory – separation of logical memory from physical memory– Only part of the program needs to be in memory
for execution– Logical address space vs. physical address space?
4CSS 430: Operating Systems - Virtual Memory
Background
• Virtual memory – separation of logical memory from physical memory– Only part of the program needs to be in memory
for execution– Logical address space >> physical address space– Degree of multiprogramming?
5CSS 430: Operating Systems - Virtual Memory
Background
• Virtual memory – separation of logical memory from physical memory– Only part of the program needs to be in memory
for execution– Logical address space >> physical address space– Higher degree of multiprogramming
6CSS 430: Operating Systems - Virtual Memory
Swapping vs. Paging
CSS 430: Operating Systems - Virtual Memory 7
Swapping Paging
Resident & Non-resident Pages
CSS 430: Operating Systems - Virtual Memory 8
Resident: A, C, F
Non-resident: B, D, E, G, H
Demand Paging• Bring a page into memory only when it is needed
– Alternative: anticipatory or pre-paging• When page is needed but not [yet] in memory
– page fault– not-in-memory bring into memory– Swapper that deals with pages is a pager
• Advantages of demand vs. anticipatory paging?
CSS 430: Operating Systems - Virtual Memory 9
Demand Paging• Bring a page into memory only when it is needed
– Alternative: anticipatory or pre-paging• When page is needed but not [yet] in memory
– page fault– not-in-memory bring into memory– Swapper that deals with pages is a pager
• Advantages of demand vs. anticipatory paging:– Less I/O needed– Less memory needed – Faster response– More users / programs
CSS 430: Operating Systems - Virtual Memory 10
Handling a Page Fault
CSS 430: Operating Systems - Virtual Memory 11
Page fault:Reference tonon-resident page
[Page table hasentries for all pages,but all pages maynot be resident]
1. Reference to non-resident page2. Trap to OS; look at PCB:
If invalid reference abortOtherwise, bring page into memory
3. Find empty frame4. Swap page into frame5. Update tables (page table, PCB)
Set valid-invalid bit = v6. Restart the instruction that caused the page fault
Copy on Write
CSS 430: Operating Systems - Virtual Memory 12
• Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory
• Shared page is copied only if either process modifies it• COW allows more efficient process creation as only
modified pages are copied
What if there is no free frame?
CSS 430: Operating Systems - Virtual Memory 13
Page fault:Reference tonon-resident page
1. Reference to non-resident page2. Trap to OS; look at PCB:
If invalid reference abortOtherwise, bring page into memory
3. Find empty frame
4. Swap page into frame
5. Update tables (page table, PCB)Set valid-invalid bit = v
6. Restart the instruction that caused the page fault
What if there is no free frame?
CSS 430: Operating Systems - Virtual Memory 14
Page fault:Reference tonon-resident page
1. Reference to non-resident page2. Trap to OS; look at PCB:
If invalid reference abortOtherwise, bring page into memory
3. Find empty frame(or select victim frame)
4. Swap page into frame(possibly swap victim out)
5. Update tables (page table, PCB)Set valid-invalid bit = v
6. Restart the instruction that caused the page fault
Effective Access Time
CSS 430: Operating Systems - Virtual Memory 15
• Page Fault Rate 0 p 1.0
• Effective Access TimeEAT =
Effective Access Time
CSS 430: Operating Systems - Virtual Memory 16
• Page Fault Rate 0 p 1.0
• Effective Access TimeEAT = (1 – p) x memory access time+ p (page fault overhead + swap page out + swap page in + restart overhead)
Effective Access Time Example• Memory access time = 200 nanoseconds• Average page-fault service time = 8 milliseconds
• EAT = (1 – p) * 200 ns + p (8 ms) = (1 – p) * 200 ns + p * 8,000,000 ns
= 200 + p * 7,999,800 ns• If 1 access out of 1,000 (0.1%, p = 0.001)
causes a page fault, then EAT = 8.2 microseconds This is a slowdown by a factor of 40 (!)
CSS 430: Operating Systems - Virtual Memory 17
Page Fault rates & Effective Access Times
CSS 430: Operating Systems - Virtual Memory 18
Page Replacement• Prevent over-allocation of memory by modifying
page-fault service routine to include page replacement
• Use a modify (“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
CSS 430: Operating Systems - Virtual Memory 19
Need For Page Replacement
20CSS 430: Operating Systems - Virtual Memory
Page Replacement
CSS 430: Operating Systems - Virtual Memory 21
Page Replacement Algorithms
• Goal: low page-fault rate
• Evaluation?
CSS 430: Operating Systems - Virtual Memory 22
Page Replacement Algorithms
• Goal: low page-fault rate
• Evaluation: simulate algorithm using a particular string of memory references (reference string) and computing the number of page faults on that string
• Example reference string:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
CSS 430: Operating Systems - Virtual Memory 23
FIFO Page Replacement
CSS 430: Operating Systems - Virtual Memory 24
Replace frame which has been in memory for longest time
FIFO Page Replacement
CSS 430: Operating Systems - Virtual Memory 25
Replace frame which has been in memory for longest time
First-In-First-Out (FIFO) Algorithm• Reference string:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames
• 4 frames
CSS 430: Operating Systems - Virtual Memory 26
1
2
3
1
2
3
4
First-In-First-Out (FIFO) Algorithm• Reference string:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames
• 4 frames
CSS 430: Operating Systems - Virtual Memory 27
1
2
3
1
2
3
4
1
2
5
3
4
9 page faults
1
2
3
4
First-In-First-Out (FIFO) Algorithm• Reference string:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames
• 4 frames
CSS 430: Operating Systems - Virtual Memory 28
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
First-In-First-Out (FIFO) Algorithm• Reference string:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5• 3 frames
• 4 frames
• Belady’s Anomaly: more frames more page faults
CSS 430: Operating Systems - Virtual Memory 29
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
Optimal Page Replacement
CSS 430: Operating Systems - Virtual Memory 30
Replace frame which will not be used for longest time
Optimal Page Replacement
CSS 430: Operating Systems - Virtual Memory 31
Replace frame which will not be used for longest time
Optimal Page Replacement
CSS 430: Operating Systems - Virtual Memory 32
Replace frame which will not be used for longest timeIs this knowable?
Optimal Page Replacement
CSS 430: Operating Systems - Virtual Memory 33
Replace frame which will not be used for longest timeIs this knowable?
Is this useful?
LRU Page Replacement
CSS 430: Operating Systems - Virtual Memory 34
Replace frame which has not been used for longest time
LRU Page Replacement
CSS 430: Operating Systems - Virtual Memory 35
Replace frame which has not been used for longest time
LRU Implementations
• Counter implementation– Every page table entry has a counter– When page is referenced, counter = current time– When a page needs to be replaced, look at the
counters to determine which is oldest
• Advantages / disadvantages?
CSS 430: Operating Systems - Virtual Memory 36
LRU Implementations
• Stack implementation– Maintain a stack of page numbers– When page is referenced, move it to the top– When page needs to be replaced, select bottom– Stack: doubly linked list
• Advantages / disadvantages?
CSS 430: Operating Systems - Virtual Memory 37
LRU Approximations• Reference bit
– Associated with each page table entry– When page is referenced, set ref bit = 1– Replace a page with ref bit = 0 (if it exists)
• pages with ref bit = 0 less recently used than pages with ref bit = 1
• Second chance algorithm– aka “Clock” replacement– For each page,
• If ref bit = 0, replace this page, stop
• Else set ref bit = 0 (for next round), continue
CSS 430: Operating Systems - Virtual Memory 38
Second View of Second Chance
CSS 430: Operating Systems - Virtual Memory 39
Operating Systems Internals & Design Principles, 7th Edition
by William Stallings
Find a frame for page 727
Second View of Second Chance
CSS 430: Operating Systems - Virtual Memory 40
Operating Systems Internals & Design Principles, 7th Edition
by William Stallings
Find a frame for page 727
Counting Algorithms
• Keep a counter of the number of references that have been made to each page
CSS 430: Operating Systems - Virtual Memory 41
Counting Algorithms
• Keep a counter of the number of references that have been made to each page
• Least Frequently Used (LFU) Algorithm: – Replace page with smallest count
• Most Frequently Used (MFU) Algorithm: – Replace page with largest count– Idea: the pages with the smallest counts were
probably just brought in and have yet to be used
CSS 430: Operating Systems - Virtual Memory 42
Allocation of Frames
• Each process needs a minimum number of pages– Example: IBM 370
• May require 6 pages to handle a single SS MOVE instruction• Instruction is 6 bytes, might span 2 pages• from block might span 2 pages• to block might span 2 pages
• Two major allocation schemes– fixed allocation– priority allocation
CSS 430: Operating Systems - Virtual Memory 43
Fixed Allocation
CSS 430: Operating Systems - Virtual Memory 44
Fixed Allocation• Equal allocation: all processes get the same
number of frames– E.g., if there are 100 frames and 5 processes,
give each process 20 frames.• Proportional allocation: differentially allocate
according to the size of process (in pages)
CSS 430: Operating Systems - Virtual Memory 45
mSs
pa
m
sS
ps
iii
i
ii
for allocation
frames of number total
process of size
5964137127
56413710
127
10
64
2
1
2
a
a
s
s
m
i
Fixed Allocation
CSS 430: Operating Systems - Virtual Memory 46
mSs
pa
m
sS
ps
iii
i
ii
for allocation
frames of number total
process of size
5964137127
56413710
127
10
64
2
1
2
a
a
s
s
m
i
• Equal allocation: all processes get the same number of frames– E.g., if there are 100 frames and 5 processes,
give each process 20 frames.• Proportional allocation: differentially allocate
according to the size of process (in pages)
Priority Allocation
• 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
CSS 430: Operating Systems - Virtual Memory 47
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– Priority allocation must use global
replacement
• Local replacement – each process selects from only its own set of allocated frames
48CSS 430: Operating Systems - Virtual Memory
Sufficient Allocation• If a process is not allocated enough pages,
the page-fault rate is very high. This leads to:– ? CPU utilization
49CSS 430: Operating Systems - Virtual Memory
Sufficient Allocation• If a process is not allocated enough pages,
the page-fault rate is very high. This leads to:– low CPU utilization
50CSS 430: Operating Systems - Virtual Memory
Sufficient Allocation• If a process is not allocated enough pages,
the page-fault rate is very high. This leads to:– low CPU utilization– OS decides it needs to increase the degree of
multiprogramming
51CSS 430: Operating Systems - Virtual Memory
Sufficient Allocation• If a process is not allocated enough pages,
the page-fault rate is very high. This leads to:– low CPU utilization– OS decides it needs to increase the degree of
multiprogramming– another process added to the system
52CSS 430: Operating Systems - Virtual Memory
Thrashing• If a process does not have “enough” pages, the
page-fault rate is very high. This leads to:– low CPU utilization– OS decides it needs to increase the degree of
multiprogramming– another process added to the system
• Thrashing more time spent paging than executing user processes
53CSS 430: Operating Systems - Virtual Memory
Memory Reference Patterns
CSS 430: Operating Systems - Virtual Memory 54
Demand Paging & Thrashing
• Why does demand paging work?– Locality model
• Memory references tend to cluster together• Process migrates from one locality to another• Localities may overlap
• Why does thrashing occur?– size of localities > total memory size allocated
CSS 430: Operating Systems - Virtual Memory 55
Working-Set Model• working-set window sequence of page references (e.g., 10,000)• 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 ( suspend a process)
CSS 430: Operating Systems - Virtual Memory 56
Working-Set Model• working-set window sequence of page references (e.g., 10,000)• 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 ( suspend a process)
CSS 430: Operating Systems - Virtual Memory 57
Page-Fault Frequency Scheme
• Establish “acceptable” page-fault rate– If actual rate too low, process loses frame– If actual rate too high, process gains frame
CSS 430: Operating Systems - Virtual Memory 58
End of Chapter 9
59CSS 430: Operating Systems - Virtual Memory
Keeping Track of the Working Set• Approximate with interval timer + a reference bit• Example: = 10,000
– Timer interrupts after every 5000 time units– Keep in memory 2 bits for each page– Whenever a timer interrupts, copy & set the values of
all reference bits to 0– If one of the bits in memory = 1 page in working set
• Why is this not completely accurate?• Improvement = 10 bits and interrupt every 1000
time units
CSS 430: Operating Systems - Virtual Memory 60
Working Sets and Page Fault Rates
CSS 430: Operating Systems - Virtual Memory 61
Memory-Mapped Files• Memory-mapped file I/O allows file I/O to be treated
as routine memory access by mapping a disk block to a page in memory.
• A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses.
• Simplifies file access by treating file I/O through memory rather than read() write() system calls.
• Also allows several processes to map the same file allowing the pages in memory to be shared.
CSS 430: Operating Systems - Virtual Memory 62
Memory Mapped Files
63CSS 430: Operating Systems - Virtual Memory
Memory-Mapped Shared Memoryin Windows
CSS 430: Operating Systems - Virtual Memory 64
Memory-Mapped Files in Java
CSS 430: Operating Systems - Virtual Memory 65
Allocating Kernel Memory
• Treated differently from user memory
• Often allocated from a free-memory pool– Kernel requests memory for various structures,
many of which have fixed sizes– Some kernel memory needs to be contiguous– All kernel memory accesses needs to be fast
CSS 430: Operating Systems - Virtual Memory 66
Buddy System• Allocates memory from
fixed-size segment consisting of physically-contiguous pages
• Power-of-2 allocator– Satisfies requests in units sized as
power of 2 (rounded up)– When smaller allocation needed
than is available, split a chunk into two buddies of next-lower power of 2
• Continue as needed• Coalescing:
– Combine adjacent chunks when freed
CSS 430: Operating Systems - Virtual Memory 67
Buddy System
CSS 430: Operating Systems - Virtual Memory 68
Operating Systems Internals & Design Principles, 7th Edition
by William Stallings
Tree Representation
CSS 430: Operating Systems - Virtual Memory 69
Operating Systems Internals & Design Principles, 7th Edition
by William Stallings
Slab Allocation
CSS 430: Operating Systems - Virtual Memory 70
Slab Allocator• Alternate strategy• Slab is one or more physically contiguous pages• Cache consists of one or more slabs• Single cache for each unique kernel data structure
– Each cache filled with objects – instantiations of the data structure• When cache created, filled with objects marked as free• When structures stored, objects marked as used• If slab is full of used objects, next object allocated from empty slab
– If no empty slabs, new slab allocated• Benefits?
CSS 430: Operating Systems - Virtual Memory 71
Slab Allocator• Alternate strategy• Slab is one or more physically contiguous pages• Cache consists of one or more slabs• Single cache for each unique kernel data structure
– Each cache filled with objects – instantiations of the data structure• When cache created, filled with objects marked as free• When structures stored, objects marked as used• If slab is full of used objects, next object allocated from empty slab
– If no empty slabs, new slab allocated• Benefits include no fragmentation, fast memory request
satisfaction
CSS 430: Operating Systems - Virtual Memory 72
Other Issues
• Pre-paging:– [when] is pre-paging worthwhile?
• Page size– What factors influence page size?– What factors are influenced by page size?
CSS 430: Operating Systems - Virtual Memory 73
Other Issues -- Prepaging• Prepaging
– To reduce the large number of page faults that occurs at process startup
– Prepage all or some of the pages a process will need, before they are referenced
– But if prepaged pages are unused, I/O and memory was wasted
– Assume s pages are prepaged and α of the pages is used
• Is cost of s * α save pages faults > or < than the cost of prepaging s * (1- α) unnecessary pages?
• α near zero prepaging loses
CSS 430: Operating Systems - Virtual Memory 74
Other Issues – Page Size
• Page size selection must take into consideration:– fragmentation– table size – I/O overhead– locality
CSS 430: Operating Systems - Virtual Memory 75
Other Issues – TLB Reach • TLB Reach - The amount of memory accessible from the TLB• TLB Reach = (TLB Size) X (Page Size)• Ideally, the working set of each process is stored in the TLB
– Otherwise there is a high degree of page faults
• Increase the Page Size– This may lead to an increase in fragmentation as not all
applications require a large page size
• Provide Multiple Page Sizes– This allows applications that require larger page sizes the
opportunity to use them without an increase in fragmentation
CSS 430: Operating Systems - Virtual Memory 76
Other Issues – Program Structure• Program structure
– Int[128,128] data;– Each row is stored in one page – Program 1
for (j = 0; j < 128; j++) for (i = 0; i < 128; i++) data[i,j] = 0;
Page faults:
– Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0;
Page faults:
CSS 430: Operating Systems - Virtual Memory 77
Other Issues – Program Structure• Program structure
– Int[128,128] data;– Each row is stored in one page – Program 1
for (j = 0; j < 128; j++) for (i = 0; i < 128; i++) data[i,j] = 0;
Page faults: 128 x 128 = 16,384
– Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0;
Page faults: 1 x 128 = 128
CSS 430: Operating Systems - Virtual Memory 78
Operating System Examples
• Windows XP
• Solaris
CSS 430: Operating Systems - Virtual Memory 79
Windows XP• Uses demand paging with clustering. Clustering brings in pages
surrounding the faulting page.• Processes are assigned working set minimum and working set
maximum (typically 50 and 345 pages)• 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 (clock or FIFO algorithm)
• Working set trimming removes pages from processes that have pages in excess of their working set minimum.
CSS 430: Operating Systems - Virtual Memory 80
Solaris • Maintains free list with
thresholds– Lotsfree: start paging
(1/64 size of phys. memory)– Desfree: increase paging– Minfree: start swapping
• Pageout process• 2-handed clock algorithm
– Handspread– Scanrate: slowscan
fastscan
CSS 430: Operating Systems - Virtual Memory 81
Operating Systems Internals & Design Principles, 7th Edition
by William Stallings