EECS 482 Introduction to Operating Systems Fall 2017 Manos Kapritsos Slides by: Harsha V. Madhyastha
Recap: Paging Both address spaces and physical memory broken up into fixed size pages Address Space Physical Memory Page 1 Page 2 Page 3 Page N October 26, 2017 EECS 482 Lecture 14 2
Paging Virt. page # Phys. page # resident protected 0 105 1 0 1 15 0 0 2 283 1 1 3 invalid invalid if (virtual page is invalid or non-resident or protected) { trap to OS fault handler; retry } else { physical page # = pagetable[virtual page #].physpagenum } October 26, 2017 EECS 482 Lecture 14 3
Page Replacement Not all valid pages may fit in physical memory Some pages are swapped out to disk To read in a page from disk, some resident page must be swapped out to disk Which page to evict when you need a free page? Goal: minimize page faults October 26, 2017 EECS 482 Lecture 14 4
Replacement policies Random FIFO Replace page brought into memory longest time ago May replace pages that continue to be frequently used Optimal? Replace page that won t be used for the longest time in the future Minimizes misses, but requires knowledge of the future October 26, 2017 EECS 482 Lecture 14 5
Replacement policies LRU (least recently used) Approximates OPT by using past reference pattern» If page hasn t been used for a while, it probably won t be used for a long time in the future LRU is hard to implement exactly Can we simplify LRU by approximating it? October 26, 2017 EECS 482 Lecture 14 6
The referenced bit Most MMUs maintain a referenced bit for each resident page Set by MMU when page is read or written Can be cleared by OS How to use reference bit to identify old pages? Why maintain reference bit in hardware? October 26, 2017 EECS 482 Lecture 14 7
Clock replacement algorithm Arrange resident pages around a clock F A B Algorithm to select page for eviction: Consider page pointed to by clock hand If not referenced, page hasn t been accessed since last sweep à Evict If referenced, page has been referenced since last sweep» What to do? What if all pages have been referenced since last sweep? What about new pages? E D C October 26, 2017 EECS 482 Lecture 14 8
LRU Clock P3: 1 P3: 0 P2: 1 P4: 0 P2: 0 P9: 1 P1: 1 P5: 1 P1: 0 P5: 1 P8: 1 P6: 1 P8: 1 P6: 1 P7: 0 P7: 0 A nice feature of clock is that it only adds overhead when you need to evict a page October 26, 2017 EECS 482 Lecture 14 9
Administrivia Almost finished grading midterm exams Will send out emails soon My office hours cancelled this Monday due to travel Start working on Project 3!!! Covered most of the material you need to know to do the project October 26, 2017 EECS 482 Lecture 14 10
Page eviction Where to evict page to? When do you NOT need to write page to disk? Rely on hardware/mmu to maintain dirty bit in PTE Why not write to disk on every store? The page that is brought from disk must wait while some page is evicted How could you optimize eviction? DON T use these optimizations in Project 3! October 26, 2017 EECS 482 Lecture 14 11
Page table contents Written by OS, Read by MMU Written by OS/MMU Read by OS Physical page # if (virtual page is non-resident or protected) { } else { trap to OS fault handler retry access physical page # = pagetable[virtual page #].physpagenum pagetable[virtual page #].referenced = true if (access is write) { pagetable[virtual page #].dirty = true } Resident access physical memory Read/Write enabled Dirty Referenced } October 26, 2017 EECS 482 Lecture 14 12
Page table contents Physical page # Resident Read/Write enabled Dirty Referenced Why no valid bit in PTE? All invalid virtual pages are non-resident For valid non-resident pages, does PTE contain disk block? OS must maintain this, MMU simply traps to OS Can we make do without resident bit? Use protection bits October 26, 2017 EECS 482 Lecture 14 13
Page table contents Physical page # Read/Write enabled Dirty Referenced Can we make do without dirty bit? Use protection bits Won t this increase # of page faults? October 26, 2017 EECS 482 Lecture 14 14
Page table contents Physical page # Read/Write enabled Referenced Can we make do without referenced bit? Application too may want to control protection Not in project 3 October 26, 2017 EECS 482 Lecture 14 15
Page table contents Physical page # read_enabled write_enabled October 26, 2017 EECS 482 Lecture 14 16
Project 3 Process view: Every process has an address space starting from VM_ARENA_BASEADDR of size VM_ARENA_SIZE When a process starts, entire address space is invalid Process calls vm_map to make pages valid Pages becomes invalid when process ends Pager view: One process runs at a time Sets up page table that MMU uses for translation Handles vm_create, vm_map, and vm_fault October 26, 2017 EECS 482 Lecture 14 17
Project 3 Swap-backed pages: Global swap file shared by all processes Pager controls where in swap file page is stored Private to a process File-mapped pages: Process specifies (file, offset) Can be shared across processes October 26, 2017 EECS 482 Lecture 14 18
Project 3 Do the project incrementally Swap-backed pages only without fork Then add support for fork and file-backed pages one after the other Pro Tip: Start with state diagrams Separate for swap-backed, file-backed pages October 26, 2017 EECS 482 Lecture 14 19
Project 3: State Diagram For each unique state, consider: Transitions? Read, write, clock, copy,... Attributes? Valid, resident, dirty,... Protections? Enable read, enable write? Mapped Valid: Yes Resident: Yes Dirty: No Zero-filled: Yes... Write Written Valid: Yes Resident: Yes Dirty: Yes Zero-filled: No... October 26, 2017 EECS 482 Lecture 14 20