Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.
Transcript of Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.
![Page 1: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/1.jpg)
Chapter 9:Virtual Memory
Joe McCarthy
CSS 430: Operating Systems - Virtual Memory 1
![Page 2: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/2.jpg)
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
![Page 3: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/3.jpg)
Virtual Memory
3CSS 430: Operating Systems - Virtual Memory
![Page 4: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/4.jpg)
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
![Page 5: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/5.jpg)
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
![Page 6: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/6.jpg)
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
![Page 7: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/7.jpg)
Swapping vs. Paging
CSS 430: Operating Systems - Virtual Memory 7
Swapping Paging
![Page 8: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/8.jpg)
Resident & Non-resident Pages
CSS 430: Operating Systems - Virtual Memory 8
Resident: A, C, F
Non-resident: B, D, E, G, H
![Page 9: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/9.jpg)
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
![Page 10: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/10.jpg)
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
![Page 11: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/11.jpg)
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
![Page 12: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/12.jpg)
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
![Page 13: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/13.jpg)
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
![Page 14: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/14.jpg)
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
![Page 15: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/15.jpg)
Effective Access Time
CSS 430: Operating Systems - Virtual Memory 15
• Page Fault Rate 0 p 1.0
• Effective Access TimeEAT =
![Page 16: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/16.jpg)
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)
![Page 17: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/17.jpg)
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 18: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/18.jpg)
Page Fault rates & Effective Access Times
CSS 430: Operating Systems - Virtual Memory 18
![Page 19: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/19.jpg)
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
![Page 20: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/20.jpg)
Need For Page Replacement
20CSS 430: Operating Systems - Virtual Memory
![Page 21: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/21.jpg)
Page Replacement
CSS 430: Operating Systems - Virtual Memory 21
![Page 22: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/22.jpg)
Page Replacement Algorithms
• Goal: low page-fault rate
• Evaluation?
CSS 430: Operating Systems - Virtual Memory 22
![Page 23: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/23.jpg)
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
![Page 24: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/24.jpg)
FIFO Page Replacement
CSS 430: Operating Systems - Virtual Memory 24
Replace frame which has been in memory for longest time
![Page 25: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/25.jpg)
FIFO Page Replacement
CSS 430: Operating Systems - Virtual Memory 25
Replace frame which has been in memory for longest time
![Page 26: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/26.jpg)
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
![Page 27: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/27.jpg)
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
![Page 28: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/28.jpg)
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
![Page 29: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/29.jpg)
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
![Page 30: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/30.jpg)
Optimal Page Replacement
CSS 430: Operating Systems - Virtual Memory 30
Replace frame which will not be used for longest time
![Page 31: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/31.jpg)
Optimal Page Replacement
CSS 430: Operating Systems - Virtual Memory 31
Replace frame which will not be used for longest time
![Page 32: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/32.jpg)
Optimal Page Replacement
CSS 430: Operating Systems - Virtual Memory 32
Replace frame which will not be used for longest timeIs this knowable?
![Page 33: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/33.jpg)
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?
![Page 34: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/34.jpg)
LRU Page Replacement
CSS 430: Operating Systems - Virtual Memory 34
Replace frame which has not been used for longest time
![Page 35: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/35.jpg)
LRU Page Replacement
CSS 430: Operating Systems - Virtual Memory 35
Replace frame which has not been used for longest time
![Page 36: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/36.jpg)
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
![Page 37: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/37.jpg)
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
![Page 38: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/38.jpg)
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
![Page 39: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/39.jpg)
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
![Page 40: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/40.jpg)
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
![Page 41: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/41.jpg)
Counting Algorithms
• Keep a counter of the number of references that have been made to each page
CSS 430: Operating Systems - Virtual Memory 41
![Page 42: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/42.jpg)
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
![Page 43: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/43.jpg)
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
![Page 44: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/44.jpg)
Fixed Allocation
CSS 430: Operating Systems - Virtual Memory 44
![Page 45: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/45.jpg)
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
![Page 46: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/46.jpg)
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)
![Page 47: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/47.jpg)
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
![Page 48: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/48.jpg)
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
![Page 49: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/49.jpg)
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
![Page 50: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/50.jpg)
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
![Page 51: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/51.jpg)
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
![Page 52: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/52.jpg)
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
![Page 53: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/53.jpg)
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
![Page 54: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/54.jpg)
Memory Reference Patterns
CSS 430: Operating Systems - Virtual Memory 54
![Page 55: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/55.jpg)
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
![Page 56: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/56.jpg)
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
![Page 57: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/57.jpg)
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 58: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/58.jpg)
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
![Page 59: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/59.jpg)
End of Chapter 9
59CSS 430: Operating Systems - Virtual Memory
![Page 60: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/60.jpg)
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
![Page 61: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/61.jpg)
Working Sets and Page Fault Rates
CSS 430: Operating Systems - Virtual Memory 61
![Page 62: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/62.jpg)
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
![Page 63: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/63.jpg)
Memory Mapped Files
63CSS 430: Operating Systems - Virtual Memory
![Page 64: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/64.jpg)
Memory-Mapped Shared Memoryin Windows
CSS 430: Operating Systems - Virtual Memory 64
![Page 65: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/65.jpg)
Memory-Mapped Files in Java
CSS 430: Operating Systems - Virtual Memory 65
![Page 66: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/66.jpg)
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
![Page 67: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/67.jpg)
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
![Page 68: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/68.jpg)
Buddy System
CSS 430: Operating Systems - Virtual Memory 68
Operating Systems Internals & Design Principles, 7th Edition
by William Stallings
![Page 69: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/69.jpg)
Tree Representation
CSS 430: Operating Systems - Virtual Memory 69
Operating Systems Internals & Design Principles, 7th Edition
by William Stallings
![Page 70: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/70.jpg)
Slab Allocation
CSS 430: Operating Systems - Virtual Memory 70
![Page 71: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/71.jpg)
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
![Page 72: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/72.jpg)
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
![Page 73: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/73.jpg)
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
![Page 74: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/74.jpg)
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
![Page 75: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/75.jpg)
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
![Page 76: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/76.jpg)
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
![Page 77: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/77.jpg)
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
![Page 78: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/78.jpg)
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
![Page 79: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/79.jpg)
Operating System Examples
• Windows XP
• Solaris
CSS 430: Operating Systems - Virtual Memory 79
![Page 80: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/80.jpg)
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
![Page 81: Chapter 9: Virtual Memory Joe McCarthy CSS 430: Operating Systems - Virtual Memory1.](https://reader036.fdocuments.us/reader036/viewer/2022062308/56649d305503460f94a08df9/html5/thumbnails/81.jpg)
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