280 likes | 516 Views
Memory Replacement Policies. Stallings 8.1,8.2 (Ch22) Lawrence Angrave. Demand Paging Example. VM. fault. ref. Load M. Page table. Free frame. Concepts this Lecture. Terminology Demand Paging Replacement Issues Replacement Strategies. #1 Terminology.
E N D
Memory Replacement Policies Stallings 8.1,8.2 (Ch22) Lawrence Angrave
Demand Paging Example VM fault ref Load M Page table Free frame
Concepts this Lecture • Terminology • Demand Paging • Replacement Issues • Replacement Strategies
#1 Terminology • Reference string: the memory reference sequence generated by a program. • Paging – moving pages to (from) disk • Optimal – the best (theoretical) strategy • Eviction – throwing something out • Pollution – bringing in useless pages/lines
#2 Demand Paging - Paging Policies • Fetch Strategies • When should a page be brought into primary (main) memory from secondary (disk) storage? • Placement Strategies • When a page is brought into primary storage, where is it to be put? • Replacement Strategies • Which page now in primary storage is to be removed from primary storage when some other page or segment is to be brought in and there is not enough room?
Page Replacement Algorithm (review) • find a free page frame • if free page frame use it • otherwise, select a page frame using the page replacement algorithm • write the selected page to the disk and update any necessary tables • find location of page on disk • read the requested page from the disk. • restart the user process.
Page Replacement Issues 1/2 • if no frames are free, a disk read and write of page is required • if the page to be replaced has not been changed since it was read in, it can be discarded and does not need to be copied out to secondary storage. • read-only pages are never written but may be shared!
Page Replacement Issues 2/2 • a dirty bit is set in the page table by hardware to indicate that the page has been modified. • need to minimize page faults. • reference string is the memory references generated by a program.
Page Replacement Strategies 1/2 • optimal • replace the page that will not be used again the farthest time in the future. • random • choose a page randomly • FIFO - first in first out replace the page that has been in primary memory the longest
Page Replacement Strategies 2/2 • LRU - least recently used • replace the page not used for the longest time • LFU - least frequently used • replace the page that is used least often • NUR - not used recently • an approximation to LRU • working set • keep in memory those pages that the process is actively using.
Principal of Optimality • provides a basis for comparison with other schemes. • is difficult to implement because of trying to predict the reference string for a program. • but sometimes… compiler technology can help by providing hints.
Principal of Optimality • if the reference string can be predicted accurately, then don't use demand paging but should use pre-paging instead. • … because this would allow paging activity of pages needed in the future to be overlapped with computation.
Optimal Example 12 references, 7 faults
FIFO 12 references, 9 faults
Belady's Anomaly (for FIFO) As the number of page frames increase, so does the fault rate. 12 references, 10 faults
LRU 12 references, 10 faults
Least Recently Used Issues • does not suffer from Belady's anomaly • use time • record time of reference with page table entry • use counter as clock • search for smallest time. • use stack • remove reference of page from stack (linked list) • push it on top of stack • both approaches require large processing overhead, more space, and hardware support.
LRU and Anomalies Anomalies cannot occur, why? 12 references, 8 faults
NUR: A LRU Approximation • additional reference bits • a register is kept per page • a one bit is set in the register if the page is referenced • the register is shifted by one after some time interval • 00110011 would be accessed more recently than 00010111 • the page with register holding the lowest number is the least recently used. • the value may not be unique. use fifo to resolve conflicts.
Second Chance • second chance algorithm uses a register size one: a reference bit. • the page table entry has a reference bit • initially, the reference bit for a page is set to 0 • when a page is referenced, the page is set to 1 • pages are kept in FIFO order using a circular list. • select head of FIFO
Second Chance • if page has reference bit set, reset bit and select next page in FIFO list. • keep processing until reach page with zero reference bit and page that one out. • system v, r4 uses a variant of second chance.
Second Chance Example 12 references, 9 faults
Page Classes • 1.(0,0) neither referenced nor dirtied • 2.(0,1) not referenced (recently) but dirtied • 3.(1,0) referenced but clean • 4.(1,1) referenced and dirtied • select a page from lowest class • if conflict, use random or FIFO.
Summary • Fetch, placement, page replacement. • Demand paging algorithm. • Simple page replacement policies. • Optimal • FIFO • LRU • NUR • Second chance, page classes