Virtual Memory: Page Replacement
description
Transcript of Virtual Memory: Page Replacement
![Page 1: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/1.jpg)
Virtual Memory: Page Replacement
![Page 2: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/2.jpg)
Realizing Virtual Memory Hardware support
Memory Management Unit (MMU): address translation, bits, interrupts
Operating system supportPage replacement policyResident set managementLoad control degree of multiprogramming
![Page 3: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/3.jpg)
Page Replacement Policy Resident set maintenance
Fixed or variable allocation Per-process or global replacement
Page replacement problemA fixed number of frames, M, is used to map the process virtual memory pagesWhich page should be replaced when a page fault occurs and all M frames are occupied?
![Page 4: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/4.jpg)
Requirements and Metrics Workload: a sequence of virtual
memory references (page numbers) Page fault rate =
#page faults/#memory references Minimize the page fault rate for
workloads obeying the principle of locality
Keep hardware/software overhead as small as possible
![Page 5: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/5.jpg)
Algorithms Optimal (OPT) Least Recently Used (LRU) First-In-First-Out (FIFO) Clock
![Page 6: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/6.jpg)
Optimal Policy (OPT) Replace the page which will be
referenced again in the most remote future
Impossible to implementWhy?
Serves as a baseline for other algorithms
![Page 7: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/7.jpg)
Least Recently Used (LRU) Replace the page that has not been
referenced for the longest time The best approximation of OPT for
the locality constrained workloads Possible to implement Infeasible as the overhead is high
Why?
![Page 8: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/8.jpg)
First-In-First-Out (FIFO) Page frames are organized in a circular
buffer with a roving pointer Pages are replaced in round-robin style
When page fault occur, replace the page to which the pointer points to
Simple to implement, low overhead High page fault rate, prone to
anomalous behavior
![Page 9: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/9.jpg)
Clock (second chance) Similar to FIFO but takes page usage
into accountCircular buffer + page use bitWhen a page is referenced: set use_bit=1When a page fault occur: For each page:
if use_bit==1: give page a second chance: use_bit=0; continue scan;
if use_bit==0: replace the page
![Page 10: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/10.jpg)
Example: Page 727 is needed
0
1
2
3
4
56
7
8
n
.
.
.
Page 9use = 1
Page 19use = 1
Page 1use = 0
Page 45use = 1
Page 191use = 1
Page 556use = 0
Page 13use = 0
Page 67use = 1
Page 33use = 1
Page 222use = 0
next frame pointer
![Page 11: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/11.jpg)
After replacement0
1
2
3
4
56
7
8
n
.
.
.
Page 9use = 1
Page 19use = 1
Page 1use = 0
Page 45use = 0
Page 191use = 0
Page 727use = 0
Page 13use = 0
Page 67use = 1
Page 33use = 1
Page 222use = 0
next frame pointer
![Page 12: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/12.jpg)
Example of all algorithms
![Page 13: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/13.jpg)
LRU and non-local workloads Workload: 1 2 3 4 5 1 2 3 4 5…
Typical for array based applications What is the page fault rate for M=1,
…,5? A possible alternative is to use a
Most Recently Use (MRU) replacement policy
![Page 14: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/14.jpg)
Belady’s Anomaly It is reasonable to expect that regardless
of a workload, the number of page faults should not increase if we add more frames: not true for the FIFO policy:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1
2
3
1
2
3
4
1
2
5
3
4
1
2
3
1
2
3
5
1
2
4
5
44 3
![Page 15: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/15.jpg)
Algorithm comparison
![Page 16: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/16.jpg)
Clock algorithm with 2 bits Use “modified” bit to evict unmodified
(clean) pages in preference over modified (dirty) pages
Four classes:u=0; m=0: not recently used, cleanu=0; m=1: not recently used, dirtyu=1; m=0: recently used, cleanu=1; m=1: recently used, dirty
![Page 17: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/17.jpg)
First scan: look for (0,0) frame, do not change the use bit
If (0,0) frame is found, replace it Second scan: look for (0,1) frame, set
use bit to 0 in each frame bypassedIf (0,1) frame is found, replace it
If all failed, repeat the above procedure
this time we will certainly find something
Clock algorithm with 2 bits
![Page 18: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/18.jpg)
Page buffering Evicted pages are kept on two lists:
free and modified page lists Pages are read into the frames on the
free page list Pages are written to disk in large
chunks from the modified page list If an evicted page is referenced, and it
is still on one of the lists, it is made valid at a very low cost
![Page 19: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/19.jpg)
Page BufferingB
B
B
B
B36N21N3N78N2N47N22N39N4N8N
55N
B
B
B
B36N21N3N78N2N47N22B39N4N8N
Page fault:55 is needed22 is evicted
Bufferedframes(B)
Normalframes(N)
![Page 20: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/20.jpg)
Resident set management With multiprogramming, a fixed
number of memory frames are shared among multiple processes
How should the frames be partitioned among the active processes?
Resident set is the set of process pages currently allocated to the memory frames
![Page 21: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/21.jpg)
Global page replacement All memory frames are candidates
for page evictionA faulting process may evict a page of other process
Automatically adjusts process sizes to their current needs
Problem: can steal frames from “wrong” processes
Leads to thrashing
![Page 22: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/22.jpg)
Local page replacement Only the memory frames of a
faulting process are candidates for replacement
Dynamically adjust the process allocation
Working set modelPage-Fault Frequency (PFF) algorithm
![Page 23: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/23.jpg)
The working set model [Denning’68] Working set is the set of pages in
the most recent page references
Working set is an approximation of the program locality
![Page 24: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/24.jpg)
The working set strategy Monitor the working set for each
currently active process Adjust the number of pages
assigned to each process according to its working set size
Monitoring working set is impractical The optimal value of is unknown
and would vary
![Page 25: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/25.jpg)
Page-Fault Frequency (PFF) Approximate the page-fault frequency:
Count all memory references for each active processWhen a page fault occurs, compare the current counter value with the previous page fault counter value for the faulting processIf < F, expand the WS; Otherwise, shrink the WS by discarding pages with use_bit==0
![Page 26: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/26.jpg)
Swapping If a faulting process cannot expand its
working set (all frames are occupied), some process should be swapped out
The decision to swap processes in/out is the responsibility of the long/medium term scheduler
Another reason: not enough memory to run a new process
![Page 27: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/27.jpg)
Long (medium) term scheduling Controls multiprogramming level Decision of which processes to swap
out/in is based onCPU usage (I/O bound vs. CPU bound)Page fault ratePrioritySizeBlocked vs. running
![Page 28: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/28.jpg)
UNIX process statesrunning
user
runningkernel
readyuser
readykernel blocked
zombie
sys. callinterrupt
schedule
created
return
terminated
wait for event
event done
schedule
preempt
interrupt
readyswapped
blockedswapped
Swap out
event done
Swap outSwap in
![Page 29: Virtual Memory: Page Replacement](https://reader033.fdocuments.in/reader033/viewer/2022042617/568167e7550346895ddd5409/html5/thumbnails/29.jpg)
Next: File system, disks, etc