Basic Page Replacement 1. Find the location of the desired page on disk 2. 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 3. Allocate & read the desired page into the (newly) free frame. Update the page and frame tables. 4. Restart the process c.f., Use modify (dirty) bit to reduce overhead of page transfers only modified pages are written back into disk 9.1 Silberschatz, Galvin and Gagne 2009
Page Replacement 9.2 Silberschatz, Galvin and Gagne 2009
Page Replacement Algorithms We want the lowest page-fault rate Evaluate algorithm by running it on a particular string of memory references (called reference string) and computing the number of page faults on that string In all our examples, the reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 9.3 Silberschatz, Galvin and Gagne 2009
Graph of Page Faults vs. The Number of Frames : General Tendency 9.4 Silberschatz, Galvin and Gagne 2009
First-In-First-Out (FIFO) Algorithm FIFO page replacement Replace page that has been in the system the longest Can be implemented with relatively low overhead Evaluation Possible to replace heavily used pages Impractical for most systems 9.5 Silberschatz, Galvin and Gagne 2009
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 4 frames 3 3 2 4 1 1 5 4 2 2 1 5 10 page faults 3 3 2 4 4 3 FIFO Replacement Belady s Anomaly more frames, on the contrary, result in more page faults. 9.6 Silberschatz, Galvin and Gagne 2009
FIFO Illustrating Belady s Anomaly 9.7 Silberschatz, Galvin and Gagne 2009
FIFO Page Replacement 9.8 Silberschatz, Galvin and Gagne 2009
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 2 6 page faults 3 4 5 How do you know this? Used for measuring how well your algorithm performs 9.9 Silberschatz, Galvin and Gagne 2009
Optimal Page Replacement 9.10 Silberschatz, Galvin and Gagne 2009
Least Recently Used (LRU) Algorithm Exploits temporal locality by replacing the page that has spent the longest time in memory without being referenced Can provide better performance than FIFO Increased overhead of system implementation LRU can perform poorly if the least-recently used page is the next page to be referenced by a program E.g., iterating inside a loop that references several pages and iterating back to outside a loop 9.11 Silberschatz, Galvin and Gagne 2009
Least Recently Used (LRU) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 5 2 3 5 4 4 3 9.12 Silberschatz, Galvin and Gagne 2009
LRU Algorithm Implementation (Cont.) 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 one should be changed Additional space overhead in page table for counter bit Also needs additional search (or sort) time to least recent entry in page table 9.13 Silberschatz, Galvin and Gagne 2009
LRU Algorithm Implementation (Cont.) Stack implementation keep a stack of page numbers in an array or a double link form Page referenced: move it to the top requires array movement or pointers to be changed Stack size overhead No search for replacement 9.14 Silberschatz, Galvin and Gagne 2009
Use of Stack to Record the Most Recent Page References expensive operation 9.15 Silberschatz, Galvin and Gagne 2009
LRU Approximation Algorithms Reference bit each page is associated with a reference bit, initially = 0 When page is referenced, the bit set to 1 That is, 0 bit indicate that no reference at all after allocation Replace the page which has reference bit value 0 (if one exists). Problems We do not know the exact order. Problem1, The page which is just brought in (set to 0) is not LRU candidate, but can be replaced. Problem2, The oldest brought entry (set to 0) with immediate re-reference (set to 1, eventually) is actually LRU, but can not be replaced. 9.16 Silberschatz, Galvin and Gagne 2009
LRU Approximation Algorithms Second Chance algorithm Also called as Clock Replacement Need reference bit 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) if reference bit = 0 9.17 Silberschatz, Galvin and Gagne 2009
Second-Chance (clock) Page-Replacement Algorithm 9.18 Silberschatz, Galvin and Gagne 2009
LRU Approximation Algorithms NUR (Not Used Recently) algorithms A kind of LRU approximation Similar to LRU but low overhead Two additional hardware bits Reference bit and Modified bit Algorithm Search pages in which R=0, and replace one of them. If all pages R =1 then, Search M=0 and replace one of them. 9.19 Silberschatz, Galvin and Gagne 2009
Far Page Replacement Creates an access graph that characterizes a process s reference patterns Replace the unreferenced page that is furthest away from any referenced page in the access graph Performs at near-optimal levels Has not been implemented in real systems Access graph is complex to search and manage without hardware support 9.20 Silberschatz, Galvin and Gagne 2009
Far Page Replacement 9.21 Silberschatz, Galvin and Gagne 2009
Count based Replacement Algorithms Keep a counter of the number of references that have been made to each page LFU Algorithm: replaces page with smallest count Page which is just fetched could possibly be replaced. MFU Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used 9.22 Silberschatz, Galvin and Gagne 2009
Count based Replacement Algorithms Replacement Algorithm Confliction LFU vs. MFU LRU vs. LFU with reference string 1 1 1 1 1 2 3 4 LRU : entry 1 is candidate to replace LFU : entry 1 is the most frequent entry 9.23 Silberschatz, Galvin and Gagne 2009
Allocation of Frames Each process needs at least minimum number of pages Two major allocation schemes fixed allocation priority allocation 9.24 Silberschatz, Galvin and Gagne 2009
Fixed Allocation Equal allocation For example, if there are 100 frames and 5 processes, give each process 20 frames. Proportional allocation Allocate according to the size of process si = size of process pi ; Size of virtual memory for process S = s a i i m = total number of = allocation for m = 64 s s a a i 2 1 2 = 10 = 127 p i frames si = m S 10 = 64 5 137 127 = 64 59 137 9.25 Silberschatz, Galvin and Gagne 2009
Priority Allocation Use a proportional allocation scheme using priorities rather than size 9.26 Silberschatz, Galvin and Gagne 2009
Global vs. Local Page Replacement If process P i generates a page fault, select for replacement page within its frames select for replacement a frame from other process frame with lower priority number Global replacement process selects a replacement frame from the set of all frames; one process can take a frame from another Local replacement each process selects from only its own set of allocated frames 9.27 Silberschatz, Galvin and Gagne 2009
Thrashing If a process does not have enough pages, the page-fault rate is very high. This leads to: low CPU utilization operating system thinks that it needs to increase the degree of multiprogramming another process added to the system Thrashing a process is busy swapping pages in and out 9.28 Silberschatz, Galvin and Gagne 2009
Thrashing (Cont.) 9.29 Silberschatz, Galvin and Gagne 2009
Demand Paging and Thrashing Why does demand paging work? Locality model Process migrates from one locality to another Localities may overlap Why does thrashing occur? Σ size of locality > required memory size in total i.e., unbalance between locality size and the number of properly required page frames 9.30 Silberschatz, Galvin and Gagne 2009
Locality In A Memory-Reference Pattern 9.31 Silberschatz, Galvin and Gagne 2009
Working-Set Model Working set : number of page frames for a process Δ working-set window a fixed number of page references Example: 10,000 instruction WSS i (working set of Process P i ) = total number of pages referenced in the most recent working-set windows Δ (it may vary in time) if Δ too small, will not encompass entire locality if Δ too large, will encompass several localities if Δ = will encompass entire program D Σ WSS i (i.e., total sum of demand frames for each process) Let m be the total number of available frame if D > m Thrashing Need policy if D > m, then suspend one of the processes 9.32 Silberschatz, Galvin and Gagne 2009
Working-set model Here shows the difficulty of finding proper WS(t i ). 9.33 Silberschatz, Galvin and Gagne 2009
Working-set vs. Page Fault Rates 9.34 Silberschatz, Galvin and Gagne 2009
Approximation of Working-Set Model Example Δ = 10,000 Timer interrupts happen after every 5000 references Maintains 2 bit-wide reference bits for each page in page table Operation When we get a timer interrupt, we clear all reference bits for each page Future reference of processor will make the reference bit as one again If one of reference bits in memory = 1 the page is IN the working set Why is this not completely accurate? We cannot tell where, within an interval, the reference happens. Improvement Precise operation such as 10 reference bits and interrupt every 1000 time units. Cost to service will be correspondingly higher. 9.35 Silberschatz, Galvin and Gagne 2009
Page-Fault Frequency Scheme Another strategy to handle the thrashing : PFF (Page Fault Frequency) Establish acceptable page-fault rate for each process If actual rate too high, process gains frame If actual rate too low, process has too many frames so it loses frame to provide other process utilization. 9.36 Silberschatz, Galvin and Gagne 2009