280 likes | 470 Views
Memory Replacement Policies. Lecture 32 Klara Nahrstedt. CS241 Administrative. Read Stallings Chapter 8.1 and 8.2 about VM LMP2 (Part II due on Monday, April 16) See also lecture notes, self-assessment quiz, discussion section LMP2 quiz on Monday, April 16
E N D
Memory Replacement Policies Lecture 32 Klara Nahrstedt
CS241 Administrative • Read Stallings Chapter 8.1 and 8.2 about VM • LMP2 (Part II due on Monday, April 16) • See also lecture notes, self-assessment quiz, discussion section • LMP2 quiz on Monday, April 16 • If you found Dr. Cerf’s lecture interesting, you can • Pay attention next two weeks in cs241 were we cover socket interfaces to TCP/IP - Internet • Learn more about Internet material in classes cs438 (Communication Networking – TCP/IP), cs425 (Distributed Systems), ece435 (Computer Networking Lab – Implementation of TCP/IP), cs414 (Multimedia Systems), cs463 (Computer Security
Concepts this Lecture • Introduction to Demand Paging • Replacement • Principle of Optimality • Replacement Policies
Introduction to 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.
Demand Paging Example VM fault ref Load M i Page table Free frame
Page Replacement Algorithm • 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 • It is necessary to be careful of synchronization problems. For example, page faults may occur for pages being paged out.
Page Replacement Issues • 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 • 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 • the principle of optimality • replace the page that will not be used again the farthest time in the future. • random page replacement • choose a page randomly • FIFO - first in first out replace the page that has been in primary memory the longest
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
Page Replacement Strategies • 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. • compiler technology can help by providing hints.
Principal of Optimality • if the reference string can be predicted accurately, then shouldn't use demand paging but should use pre-paging instead as 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