Virtual Memory Management-Operating Systems
Virtual Memory Management-Operating Systems
Virtual Memory Management-Operating Systems
Operating System Concepts – 8th Edition 9.2 Silberschatz, Galvin and Gagne
Objectives
To describe the benefits of a virtual memory system
Operating System Concepts – 8th Edition 9.3 Silberschatz, Galvin and Gagne
Background
Virtual memory – separation of user logical memory from
physical memory.
Only part of the program needs to be in memory for
execution
Logical address space can therefore be much larger than
physical address space
Allows address spaces to be shared by several processes
Allows for more efficient process creation
Operating System Concepts – 8th Edition 9.4 Silberschatz, Galvin and Gagne
Virtual Memory That is Larger Than Physical Memory
Operating System Concepts – 8th Edition 9.5 Silberschatz, Galvin and Gagne
Virtual-address Space
Operating System Concepts – 8th Edition 9.6 Silberschatz, Galvin and Gagne
Shared Library Using Virtual Memory
Operating System Concepts – 8th Edition 9.7 Silberschatz, Galvin and Gagne
Demand Paging
Bring a page into memory only when it is needed
Less I/O needed
Less memory needed
Faster response
More users
Operating System Concepts – 8th Edition 9.8 Silberschatz, Galvin and Gagne
Transfer of a Paged Memory to Contiguous Disk Space
Operating System Concepts – 8th Edition 9.9 Silberschatz, Galvin and Gagne
Valid-Invalid Bit
With each page table entry a valid–invalid bit is associated
(v in-memory, i not-in-memory)
Initially valid–invalid bit is set to i on all entries
Example of a page table snapshot:
i
i
page table
Operating System Concepts – 8th Edition 9.11 Silberschatz, Galvin and Gagne
Page Fault
Operating System Concepts – 8th Edition 9.12 Silberschatz, Galvin and Gagne
Page Fault (Cont.)
Restart instruction
block move
Operating System Concepts – 8th Edition 9.13 Silberschatz, Galvin and Gagne
Steps in Handling a Page Fault
Operating System Concepts – 8th Edition 9.14 Silberschatz, Galvin and Gagne
Operating System Concepts – 8th Edition 9.15 Silberschatz, Galvin and Gagne
Operating System Concepts – 8th Edition 9.16 Silberschatz, Galvin and Gagne
Performance of Demand Paging
Page Fault Rate 0 p 1.0
if p = 0 no page faults
if p = 1, every reference is a fault
Operating System Concepts – 8th Edition 9.17 Silberschatz, Galvin and Gagne
Demand Paging Example
Memory access time = 200 nanoseconds
Operating System Concepts – 8th Edition 9.18 Silberschatz, Galvin and Gagne
Copy-on-Write
Copy-on-Write (COW) allows both parent and child processes
to initially share the same pages in memory
Operating System Concepts – 8th Edition 9.19 Silberschatz, Galvin and Gagne
Before Process 1 Modifies Page C
Operating System Concepts – 8th Edition 9.20 Silberschatz, Galvin and Gagne
After Process 1 Modifies Page C
Operating System Concepts – 8th Edition 9.21 Silberschatz, Galvin and Gagne
What happens if there is no free frame?
Operating System Concepts – 8th Edition 9.22 Silberschatz, Galvin and Gagne
Page Replacement
Prevent over-allocation of memory by modifying page-fault
service routine to include page replacement
Operating System Concepts – 8th Edition 9.23 Silberschatz, Galvin and Gagne
Need For Page Replacement
Operating System Concepts – 8th Edition 9.24 Silberschatz, Galvin and Gagne
Basic Page Replacement
Operating System Concepts – 8th Edition 9.25 Silberschatz, Galvin and Gagne
Page Replacement
Operating System Concepts – 8th Edition 9.26 Silberschatz, Galvin and Gagne
Page Replacement Algorithms
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Operating System Concepts – 8th Edition 9.27 Silberschatz, Galvin and Gagne
Graph of Page Faults Versus The Number of Frames
Operating System Concepts – 8th Edition 9.28 Silberschatz, Galvin and Gagne
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)
1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
4 frames
1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3
Operating System Concepts – 8th Edition 9.29 Silberschatz, Galvin and Gagne
FIFO Page Replacement
Operating System Concepts – 8th Edition 9.30 Silberschatz, Galvin and Gagne
FIFO Illustrating Belady’s Anomaly
Operating System Concepts – 8th Edition 9.31 Silberschatz, Galvin and Gagne
Optimal Algorithm
Replace page that will not be used for longest period of time
4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 4
6 page
2
faults
3
4 5
How do you know this?
Used for measuring how well your algorithm performs
Operating System Concepts – 8th Edition 9.32 Silberschatz, Galvin and Gagne
Optimal Page Replacement
Operating System Concepts – 8th Edition 9.33 Silberschatz, Galvin and Gagne
Least Recently Used (LRU) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 1 1 1 5
2 2 2 2 2
3 5 5 4 4
4 4 3 3 3
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
Operating System Concepts – 8th Edition 9.34 Silberschatz, Galvin and Gagne
LRU Page Replacement
Operating System Concepts – 8th Edition 9.35 Silberschatz, Galvin and Gagne
LRU Algorithm (Cont.)
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
Operating System Concepts – 8th Edition 9.36 Silberschatz, Galvin and Gagne
Use Of A Stack to Record The Most Recent Page References
Operating System Concepts – 8th Edition 9.37 Silberschatz, Galvin and Gagne
LRU Approximation Algorithms
Reference bit
With each page associate a bit, 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
Second chance
Need reference bit
Clock replacement
If page to be replaced (in clock order) has reference bit =
1 then:
set reference bit 0
leave page in memory
replace next page (in clock order), subject to same
rules
Operating System Concepts – 8th Edition 9.38 Silberschatz, Galvin and Gagne
Second-Chance (clock) Page-Replacement Algorithm
Operating System Concepts – 8th Edition 9.39 Silberschatz, Galvin and Gagne
Counting Algorithms
Keep a counter of the number of references that
have been made to each page
Operating System Concepts – 8th Edition 9.40 Silberschatz, Galvin and Gagne
Allocation of Frames
Operating System Concepts – 8th Edition 9.41 Silberschatz, Galvin and Gagne
Fixed Allocation
Operating System Concepts – 8th Edition 9.42 Silberschatz, Galvin and Gagne
Priority Allocation
Operating System Concepts – 8th Edition 9.43 Silberschatz, Galvin and Gagne
Global vs. Local Allocation
Operating System Concepts – 8th Edition 9.44 Silberschatz, Galvin and Gagne
Thrashing
Operating System Concepts – 8th Edition 9.45 Silberschatz, Galvin and Gagne
Thrashing (Cont.)
Operating System Concepts – 8th Edition 9.46 Silberschatz, Galvin and Gagne
Demand Paging and Thrashing
Operating System Concepts – 8th Edition 9.47 Silberschatz, Galvin and Gagne
Locality In A Memory-Reference Pattern
Operating System Concepts – 8th Edition 9.48 Silberschatz, Galvin and Gagne
Working-Set Model
working-set window a fixed number of page
references
Example: 10,000 instruction
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
Operating System Concepts – 8th Edition 9.49 Silberschatz, Galvin and Gagne
Working-set model
Operating System Concepts – 8th Edition 9.50 Silberschatz, Galvin and Gagne
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 and sets 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
Operating System Concepts – 8th Edition 9.51 Silberschatz, Galvin and Gagne
Page-Fault Frequency Scheme
Operating System Concepts – 8th Edition 9.52 Silberschatz, Galvin and Gagne
Working Sets and Page Fault Rates
Operating System Concepts – 8th Edition 9.53 Silberschatz, Galvin and Gagne
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
Operating System Concepts – 8th Edition 9.54 Silberschatz, Galvin and Gagne
Memory Mapped Files
Operating System Concepts – 8th Edition 9.55 Silberschatz, Galvin and Gagne
Memory-Mapped Shared Memory in Windows
Operating System Concepts – 8th Edition 9.56 Silberschatz, Galvin and Gagne
Allocating Kernel Memory
Treated differently from user memory
Often allocated from a free-memory pool
Kernel requests memory for structures of varying sizes
Some kernel memory needs to be contiguous
Operating System Concepts – 8th Edition 9.57 Silberschatz, Galvin and Gagne
Buddy System
Allocates memory from fixed-size segment consisting of
physically-contiguous pages
Memory allocated using power-of-2 allocator
Satisfies requests in units sized as power of 2
Request rounded up to next highest power of 2
When smaller allocation needed than is available, current
chunk split into two buddies of next-lower power of 2
Continue until appropriate sized chunk available
Operating System Concepts – 8th Edition 9.58 Silberschatz, Galvin and Gagne
Buddy System Allocator
Operating System Concepts – 8th Edition 9.59 Silberschatz, Galvin and Gagne
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
Operating System Concepts – 8th Edition 9.60 Silberschatz, Galvin and Gagne
Slab Allocation
Operating System Concepts – 8th Edition 9.61 Silberschatz, Galvin and Gagne
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
Operating System Concepts – 8th Edition 9.62 Silberschatz, Galvin and Gagne
Other Issues – Page Size
Operating System Concepts – 8th Edition 9.63 Silberschatz, Galvin and Gagne
Other Issues – TLB Reach
Operating System Concepts – 8th Edition 9.64 Silberschatz, Galvin and Gagne
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;
Program 2
for (i = 0; i < 128; i++)
for (j = 0; j < 128; j++)
data[i,j] = 0;
Operating System Concepts – 8th Edition 9.65 Silberschatz, Galvin and Gagne
Other Issues – I/O interlock
Operating System Concepts – 8th Edition 9.66 Silberschatz, Galvin and Gagne
Reason Why Frames Used For I/O Must Be In Memory
Operating System Concepts – 8th Edition 9.67 Silberschatz, Galvin and Gagne
Operating System Examples
Windows XP
Solaris
Operating System Concepts – 8th Edition 9.68 Silberschatz, Galvin and Gagne
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
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
Operating System Concepts – 8th Edition 9.69 Silberschatz, Galvin and Gagne
Solaris
Maintains a list of free pages to assign faulting processes
Lotsfree – threshold parameter (amount of free memory) to
begin paging
Desfree – threshold parameter to increasing paging
Minfree – threshold parameter to being swapping
Paging is performed by pageout process
Pageout scans pages using modified clock algorithm
Scanrate is the rate at which pages are scanned. This ranges
from slowscan to fastscan
Pageout is called more frequently depending upon the
amount of free memory available
Operating System Concepts – 8th Edition 9.70 Silberschatz, Galvin and Gagne
Solaris 2 Page Scanner
Operating System Concepts – 8th Edition 9.71 Silberschatz, Galvin and Gagne
End of Chapter 9