Operating System Virtual Memory Virendra Singh Associate Professor


























- Slides: 26
Operating System Virtual Memory Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay http: //www. ee. iitb. ac. in/~viren/ E-mail: viren@ee. iitb. ac. in EE-717/453 Advanced Computing for Electrical Engineers Lecture 38
Benefits of Virtual Memory • Illusion of very large physical memory • Protection: by providing each process a separate virtual address space – Page table mechanism prevents one process from accessing the memory of another – Hardware is able to write-protect designated pages • Sharing – Common code (libraries, editors, etc. ) – For shared memory communication 07 Nov 2012 EE-717/EE-453@IITB 2
OS Responsibilities in VM Systems • Maintain/update page tables – Action initiated by addition of new process to memory or by a page fault • • Maintain list of free pages Execute page-replacement algorithms Write “dirty” pages back to disk Sometimes, update TLB (TLB may be either hardware or software managed) 07 Nov 2012 EE-717/EE-453@IITB 3
Page Replacement • Page replacement algorithms are needed when a page fault occurs and memory is full. • Use information collected by hardware to try to guess which pages are no longer in use • Most common approach: some variation of Least Recently Used (LRU), including various Clock – Keep pages that have been referenced recently on the assumption that they are in the current locality • Free frame pool is replenished periodically. 07 Nov 2012 EE-717/EE-453@IITB 4
Page Replacement - comments • Locality of reference is weaker today due mainly to OO programming – Lots of small functions & objects – Dynamic allocation/deallocation of objects on heap is more random than allocation on stack. • OS generally maintains a pool of free frames and uses them for both page replacement and for the file system cache. This also affects requirements for replacement. 07 Nov 2012 EE-717/EE-453@IITB 5
Page Replacement Algorithms • Page fault forces choice – which page must be removed – make room for incoming page • Modified page must first be saved – unmodified just overwritten • Better not to choose an often used page – will probably need to be brought back in soon 07 Nov 2012 EE-717/EE-453@IITB 6
Optimal Page Replacement Algorithm • Replace page needed at the farthest point in future – Optimal but unrealizable • Estimate by … – logging page use on previous runs of process – although this is impractical 07 Nov 2012 EE-717/EE-453@IITB 7
Not Recently Used Page Replacement Algorithm • Each page has Reference bit, Modified bit – bits are set when page is referenced, modified • Pages are classified 1. 2. 3. 4. not referenced, not modified not referenced, modified referenced, not modified referenced, modified • NRU removes page at random – from lowest numbered non empty class 07 Nov 2012 EE-717/EE-453@IITB 8
FIFO Page Replacement Algorithm • Maintain a linked list of all pages – in order they came into memory • Page at beginning of list replaced • Disadvantage – page in memory the longest may be often used 07 Nov 2012 EE-717/EE-453@IITB 9
Second Chance Page Replacement Algorithm • Operation of a second chance – pages sorted in FIFO order – Page list if fault occurs at time 20, A has R bit set (numbers above pages are loading times) 07 Nov 2012 EE-717/EE-453@IITB 10
Least Recently Used (LRU) • Assume pages used recently will used again soon – throw out page that has been unused for longest time • Must keep a linked list of pages – most recently used at front, least at rear – update this list every memory reference !! • Alternatively keep counter in each page table entry – choose page with lowest value counter – periodically zero the counter 07 Nov 2012 EE-717/EE-453@IITB 12
Simulating LRU in Software (1) LRU using a matrix – pages referenced in order 0, 1, 2, 3, 2, 1, 0, 3, 2, 3 07 Nov 2012 EE-717/EE-453@IITB 13
Simulating LRU in Software (2) • The aging algorithm simulates LRU in software • Note 6 pages for 5 clock ticks, (a) – (e) 07 Nov 2012 EE-717/EE-453@IITB 14
The Working Set Page Replacement Algorithm • The working set is the set of pages used by the k most recent memory references • w(k, t) is the size of the working set at time, t 07 Nov 2012 EE-717/EE-453@IITB 15
The Working Set Page Replacement Algorithm The working set algorithm 07 Nov 2012 EE-717/EE-453@IITB 16
The WSClock Page Replacement Algorithm 07 Nov 2012 EE-717/EE-453@IITB 17
Review of Page Replacement Algorithms 07 Nov 2012 EE-717/EE-453@IITB 18
Design Issues for Paging Systems Local versus Global Allocation Policies (1) • How memory should be allocated among the competing runnable processes ? • Original configuration • Local page replacement • Global page replacement 07 Nov 2012 EE-717/EE-453@IITB 23
Design Issues for Paging Systems Local versus Global Allocation Policies (2) • Global algorithm works better – Working set size vary over life time of process • Local algorithm – Size grows thrashing – Sizes shrinks memory wastage • Page allocation – Working set – Equal space to all processes – Process size - dynamic 07 Nov 2012 EE-717/EE-453@IITB 24
Local versus Global Allocation Policies (3) Page fault rate as a function of the number of page frames assigned 07 Nov 2012 EE-717/EE-453@IITB 25
Load Control • Despite good designs, system may still thrash • When PFF algorithm indicates – some processes need more memory – but no processes need less • Solution : Reduce number of processes competing for memory – swap one or more to disk, divide up pages they held – reconsider degree of multiprogramming 07 Nov 2012 EE-717/EE-453@IITB 26
Page Size (1) Small page size • Advantages – less internal fragmentation – better fit for various data structures, code sections – less unused program in memory • Disadvantages – programs need many pages, larger page tables 07 Nov 2012 EE-717/EE-453@IITB 27
Page Size (2) • Overhead due to page table and internal fragmentation page table space internal fragmentation • Where – s = average process size in bytes – p = page size in bytes – e = page entry 07 Nov 2012 EE-717/EE-453@IITB Optimized when 28
Separate Instruction and Data Spaces • One address space • Separate I and D spaces 07 Nov 2012 EE-717/EE-453@IITB 29
Shared Pages Two processes sharing same program sharing its page table 07 Nov 2012 EE-717/EE-453@IITB 30
Cleaning Policy • Need for a background process, paging daemon – periodically inspects state of memory • When too few frames are free – selects pages to evict using a replacement algorithm • It can use same circular list (clock) – as regular page replacement algorithmbut with diff ptr 07 Nov 2012 EE-717/EE-453@IITB 31