Computer Systems II
Total Page:16
File Type:pdf, Size:1020Kb
Computer Systems II Virtual Memory Recap: Paging • Memory is divided into frames • Process is divided into pages (same size as frames) Frame Main Memory 0 • Pages are loaded into frames (need not be 1 contiguous) 2 • Page table translates page addresses to frames 3 addresses 4 5 6 Process A (1) Process B (0) Process C (0) Process D (0) 7 Process A (2) Process B (1) Process C (1) Process D (1) 8 Process A (3) Process D (2) Process B (2) Process C (2) 9 Process A (4) Process C (3) Process D (3) Process B (3) 10 Process D (4) Process B (4) 11 Process B (5) Process D (5) 12 13 14 15 Recap: Page Tables • One page table for each process Frame Main Memory 0 Process AD (1)(0) • Page table used to translate virtual 1 Process DA (2)(1) addresses to physical addresses 2 Process DA (3)(2) 3 Process DA (4)(3) 4 Process B (0) 5 Process B (1) Process B Process C Process D 6 Process B (2) Page Frame Page Frame Page Frame 0 4 0 10 0 0 7 Process B (3) 1 5 1 11 1 1 8 Process B (4) 2 6 2 12 2 2 9 Process B (5) 3 7 3 13 3 3 10 Process C (0) 4 8 4 14 5 9 5 15 11 Process C (1) 12 Process C (2) 13 Process C (3) 14 Process D (4) 15 Process D (5) Recap: Problems that Still Remain • Should all pages be loaded in memory? – Limits the number of active processes • Solution: Virtual Memory – Load pages only when needed – Less need to swap blocked processes out to disk Virtual Memory allows the OS to load pieces only when needed (demand paging) Virtual Addressing • Virtual (logical) address is mapped to a real address using a page table • Size of physical address limited by memory size • What limits the size of a virtual address?? Page Number Offset Virtual Address Page table mapping Frame Number Offset Physical Address Virtual Memory Expands Memory Size • Virtual memory can be larger than physical memory – A process can use more memory – Only limited by swap space on disk • Pages not in main memory are stored in a dedicated swap area on the disk Main Memory Disk Pages 0 3 Swap Area 4 6 7 5 2 1 Demand Paging Frame Main Memory • Only load those pages needed 0 Process A (1) by process 1 Process A (2) 2 Process D (1) • Keep a permanent store of 3 Process B (3) pages on disk 4 Process C (1) 5 Process D (4) • Allows more processes to be 6 Process E (2) active 7 Process C (4) 8 Process F (1) • Eliminates needless loading 9 Process G (2) and unloading 10 Process B (1) 11 Process H (11) 12 Process I (1) 13 Process J (3) 14 Process E (3) 15 Process J (5) Page Faults • Imagine process A attempting to Frame Main Memory execute: 0 Process A (1) 1 Process A (2) mov ecx, [ebx] 2 Process D (1) 3 Process B (3) Register ebx contains a virtual 4 Process C (1) 5 Process D (4) address that corresponds to page 6 Process E (2) 3 of process A 7 Process C (4) 8 Process F (1) 9 Process G (2) • What happens? 10 Process B (1) 11 Process H (11) 12 Process I (1) PAGE FAULT 13 Process J (3) 14 Process E (3) 15 Process J (5) Page Fault Processing • Occurs when a process accesses a page not in memory Initial page fault processing When page is loaded (Disk Interrupt) Block current process Replacement Policy? Update page Memory yes Evict a page table Full? from memory No Mark process Setup I/O HW ready to read page from disk Start a new ready process Thrashing Frame Main Memory • Occurs when pages are 0 Process A (1)(2) swapped in and out 1 Process D (2) 2 Process D (1) frequently 3 Process B (3) • In steady state, memory will 4 Process C (1) be full 5 Process D (4) 6 Process E (2) 7 Process C (4) 8 Process F (1) for ( i=0; i<LIMIT; i++ ) { 9 Process G (2) /* access the first page */ 10 Process B (1) a = array[i]; /* access the second page */ 11 Process H (11) b = array[i+5000]; 12 Process I (1) sum = sum + a + b; Process J (3) } 13 14 Process E (3) 15 Process J (5) Aside: Programming Considerations • Program structure int A[1024][1024]; • Assume – each row is stored in one page – Only one page fits in memory • Program 1: ________ page faults for (j = 0; j < 1024; j++) for (i = 0; i < 1024; i++) A[i][j] = 0; • Program 2: ________ page faults for (i = 0; i < 1024; i++) for (j = 0; j < 1024; j++) A[i][j] = 0; Locality Limits Thrashing • Memory references tend to cluster • Different types of locality: – Spatial: If we access one memory item, we are likely to access other items close to it (i.e. code and array accesses) – Temporal: Over time, memory tends to be accessed multiple times (i.e. a loop) • OS may also limit the number of active processes to control thrashing Operating System Policies Operating System Policies • Fetch Policy: When to bring in pages • Replacement Policy: Which pages to replace • Resident Set Policy: How many pages to allow for each process • Load Control: How many active processes to allow Fetch Policy • Demand paging (most widely used) – Bring in page only when needed – Flurry of page transfers when process starts • Prepaging – Bring in extra pages on a page fault – Block transfers are more efficient – Ineffective if extra pages are not used Replacement Policy • When a page fault occurs, a victim page must be evicted from memory to make room for the new one • Goal is to replace the page needed furthest in the future (minimize page faults) • Some pages not subject to replacement – Frames are locked • Policies include: – Optimal: not possible but something to try for – First-in, first-out (FIFO) – Least recently used (LRU) Optimal • Selects page furthest referenced in the future – gives lower bound on the number of page faults • Three page replacements for example: Page Address Stream 2 3 2 1 5 2 4 5 3 2 5 2 2 2 2 2 2 2 4 4 4 2 2 2 3 3 3 3 3 3 3 3 3 3 3 1 5 5 5 5 5 5 5 5 F F F FIFO: First-In First-Out • Replace the page that is the oldest (has been in memory longest) • Six page replacements for example: Page Reference Stream 2 3 2 1 5 2 4 5 3 2 5 2 2 2 2 2 5 5 5 5 3 3 3 3 3 3 3 3 2 2 2 2 2 5 5 1 1 1 4 4 4 4 4 2 F F F F F F LRU: Least Recently Used • Replace the page that was referenced furthest in the past • Four page replacements for example: Page Address Stream 2 3 2 1 5 2 4 5 3 2 5 2 2 2 2 2 2 2 2 3 3 3 3 3 3 5 5 5 5 5 5 5 1 1 1 4 4 4 2 2 F F F F Resident Set Policy • How many frames to give each process? • Tradeoffs are: – few pages: load more processes, but more page faults – large number of pages: fewer resident processes, may waste page frames • Number of frames can be – fixed: decide at load time – variable: dynamically adjust the allocation Variable Frame Allocation • Keep track of the page fault count for each process – Define upper bound U , lower bound L for page fault rates – Allocate more frames to a process if fault rate > U – Allocate fewer frames to a process if fault rate < L • Load Control: – suspend a process with page fault rate > U, if all frames occupied Measuring the Memory Usage Virtual memory usage Physical memory usage (“resident set size”) Unix CPU time used by this process so far % ps -l F UID PID PPID PRI VSZ RSS STAT TIME COMMAND 0 115 7264 7262 17 4716 1400 SN 0:00 -csh 0 115 7290 7264 17 15380 10940 SN 5:52 emacs 0 115 3283 7264 23 2864 812 RN 0:00 ps l Windows Recap • Virtual memory can be larger than physical memory – Bring pages in memory as needed – Use disk swap area for pages not in memory • Fetch policy – Demand: bring page in memory only when needed – Prepaging: bring extra pages in on page fault • Replacement policy – FIFO: replace oldest page – LRU: replace page least recently referenced • Resident Set Policy – Adjust page allocation based on fault rate.