250 likes | 340 Views
CS423UG Operating Systems. Memory Management - IV. Indranil Gupta Lecture 16 Sep 30, 2005. Content. Review Demand paging Page Replacement. Virtual-To-Physical Lookups. Programs only know virtual addresses The page table can be extremely large
E N D
CS423UG Operating Systems Memory Management - IV Indranil Gupta Lecture 16 Sep 30, 2005
Content • Review • Demand paging • Page Replacement CS 423UG - Operating Systems, Indranil Gupta
Virtual-To-Physical Lookups • Programs only know virtual addresses • The page table can be extremely large • Each virtual address must be translated into physical address • Done through page table stored in memory CS 423UG - Operating Systems, Indranil Gupta
PTBR=Page Table Base Register (in hardware) CPU Virtual Memory Virtual Address (003006) PTBR=001 003 Page Table 003 006 0 006 4 1 Contents(3006) 0 003, 004 1 1 0 Physical Memory 1 004 006 004 Physical Address (F,D) Page size 1000 Number of Possible Virtual Pages 1000 Number of Page Frames 8 006 Contents(4006) CS 423UG - Operating Systems, Indranil Gupta
Address Translation: Birth to Death Virtual address (P,D) 1 (PTBR,P*4)=phy. address of page table entry (4 B per entry) CPU 2 PTBR Return via address bus 3 6 Get page table entry (P,F) (F,D) 4 Access (F,D)’th byte of phy. memory Physical address 5 CS 423UG - Operating Systems, Indranil Gupta
So Far… • Process address space split up into pages • Main memory split into frames • Page Table maps page numbers frame numbers • TLB: cache of page table entries • TLB miss • Multi-level Paging • Inverted Page Table • Sharing and Protection CS 423UG - Operating Systems, Indranil Gupta
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 • If primary storage is full, which page is to be evicted to make room for the new page? CS 423UG - Operating Systems, Indranil Gupta
“Demand Paging” • Algorithm: Never bring a page into primary memory until it is absolutely and necessarily needed and requested. • Page fault • Check if a valid virtual memory address. Kill job if not. • If valid reference, check page is stored in memory already (perhaps for some other process.) If so, skip to 7. • Find a free page frame. • Map address into disk block and fetch disk block into page frame. Suspend user process. • When disk read finished, add/change VM mapping for page frame. • If necessary, restart process instruction. What if there is a multi-level paging scheme? CS 423UG - Operating Systems, Indranil Gupta
Demand Paging Example VM fault ref Load M i Page table Free frame CS 423UG - Operating Systems, Indranil Gupta
Page Replacement • Find location of page on disk • Find a page frame in phy. memory to store it in • If there is an empty/free page frame, use it • Otherwise, select a victim page frame using the page replacement algorithm • Evict (Write) the selected page to the disk and update any necessary tables • Read the requested page from the disk • Restart the faulting process instruction • Be aware that paged-out pages may be requested again (perhaps soon?) CS 423UG - Operating Systems, Indranil Gupta
Issue: Eviction • Hopefully, kick out a less-useful page • Dirty(modified) pages require writing, Clean (un-modified) pages don’t • Hardware has a dirty bit for each page frame indicating this page is dirty or not – dirty bit is maintained in TLB and/or PTE • Where do you write? To Swap Space • Special space on disk where pages can be quickly saved and accessed • Goal: kick out the page that is least useful • Problem: “least useful”? • Heuristic: programs show spatial and temporal locality in accessing data and instructons • Kick out pages that aren’t likely to be used again CS 423UG - Operating Systems, Indranil Gupta
Terminology/Rules of the Game • Reference string: the sequence of memory references (page numbers) generated by a process • E.g., 1, 4, 3, 7, 6, 7, 6… • Paging – moving pages to (from) disk • Optimal – the best (theoretical) strategy • Reduce number of page faults CS 423UG - Operating Systems, Indranil Gupta
Page Replacement Strategies • The Principle of Optimality • Replace the page that will not be used again the farthest time in the future. Un-implementable (why?) • Random page replacement • Choose a page randomly • FIFO - First in First Out • Replace the page that has been in primary memory the longest (oldest) • LRU - Least Recently Used • Replace the page that has not been used for the longest time in the past • LFU - Least Frequently Used • Replace the page that has been used least often • NRU - Not Recently Used • An approximation to LRU • Working Set • Keep in memory those pages that the process is actively using CS 423UG - Operating Systems, Indranil Gupta
Principal of Optimality • Description: • Assume that each page P can be labeled with the number of other page references that will occur before page P is referenced next time. Can know this only if we would know the future reference string for a program. • Choose the page whose next-reference is farthest out in the future, and evict that page • Provably optimal (minimal number of page faults among all page replacement algorithms for a given reference string) • Provides a basis for comparison with other schemes • Impractical because it needs future references • If future references are known (e.g., real-time systems) • should not use demand paging • should use pre-paging to allow paging to be overlapped with computation CS 423UG - Operating Systems, Indranil Gupta
Optimal Example 12 references, 7 faults CS 423UG - Operating Systems, Indranil Gupta
FIFO 12 references, 9 faults CS 423UG - Operating Systems, Indranil Gupta
Paging Behavior for Process with Rising Number of Page Frames CS 423UG - Operating Systems, Indranil Gupta
Belady's Anomaly (for FIFO) Same reference string as with 3 frames (9 page faults). 12 references, 10 faults Belady’s Anomaly for FIFO: (Sometimes) as the number of page frames increase, so does the fault rate. CS 423UG - Operating Systems, Indranil Gupta
LRU 12 references, 10 faults CS 423UG - Operating Systems, Indranil Gupta
Least Recently Used Issues • Does not suffer from Belady's anomaly • How to track “recency”? • Use time • Record last time of access with page table entry. Eviction: Find smallest. • Or keep track of count since last access, increment every clock tick. Eviction: Find largest. • Use stack of page numbers • When a page is accessed, remove page number from stack and push page number on top of stack • Eviction: Find bottom of stack. • Both approaches require large processing overhead, more space, and hardware support. CS 423UG - Operating Systems, Indranil Gupta
LRU and Anomalies Anomalies cannot occur. 12 references, 8 faults CS 423UG - Operating Systems, Indranil Gupta
(different from NFU and NRU in textbook) NRU: A LRU Approximation • NRU: Evict a page that is NOT recently used;(LRU was: evict a page that is LEAST recently used) • NRU Implementation: simpler than LRU • additional reference bits per page (in PTE) • a byte maintained per page (in PTE) • If page referenced, most significant bit in this byte is set to 1 • Periodically, the byte is shifted to the right by 1 bit • So, 00110011 would be accessed more recently than 00010111 • The page with this byte value that is the lowest is the least recently used (well, almost…) • The value may not be unique. Use FIFO to resolve conflicts. CS 423UG - Operating Systems, Indranil Gupta
Second Chance: Even Simpler • Only one reference bit in the page table entry. • 0 initially • Set to 1 when a page is referenced • Pages are kept in FIFO order using a circular list. • Choosing “victim” to evict • Select head of FIFO • If page has reference bit set, reset bit and go to next page in FIFO list. • Keep processing until you reach page with 0 reference bit. Evict that page (if none, then go back to page at front of FIFO, and evict it – its reference bit is now 0). • System V uses a variant of second chance CS 423UG - Operating Systems, Indranil Gupta
Second Chance Example 12 references, 9 faults CS 423UG - Operating Systems, Indranil Gupta
Reminders • Reading for this lecture: Section 4.4 • Reading for next lecture: Rest of Chapter 4 • Midterm on Oct 10 • No more HW’s until midterm • Next MP2 due Oct 17 CS 423UG - Operating Systems, Indranil Gupta