More Memory Management: memory: Access time: [CPU] [cache] <10ns | [Main Mem] 100-300cpu cycles / 30ns | [Disk] 5-50ms | [tape/network] seconds or even minutes even though the size of memory in computers has grown huge, we are still concerned with memory management for moving things around in the hierarchy most programs spend there time running a small amount of code, so we can use the principal of locality: 1. The same information is likely to be reused. 2. nearby information is also likely to be used in the near future. If a page is not found in memory when a program tries to access it, a pagefault happens. When a page fault happens: not in memory. * Trap to OS - this is the only place we can trap to! * Verify that reference is to valid page; if not, abend (abnormal end.) * Find page frame to put page. * Find a page to replace, if no empty frame. * If dirty, put page in secondary storage * Remove page * Update page table. * Update map of secondary storage if necessary * Update memory (core) map * Flush TLB entry removed page * Operating system brings page into memory * Find page on secondary storage. * Transfer it. * Update page table * Update map of file system/disk * Update Core Map (memory map). * The process resumes execution. ->All of this happens at the same time, we switch processes while this happens Paging: * Page out - remove a page * Page out a process - remove from mem * Page in a process - loads to mem Resuming a process can be very tricky. What happens if a pagefault occurs in the middle of a copy? should you resume it? restart it? Restarting a process after a page fault can cause problem. if the page fault happens because you are trying to copy a string over a page border, and it just so happens that you are coping the string onto itsself this can cause serious problems. (ex) page 1: page2: ________ ________ | | |hijk | | | | | | | | | | | | | | | | | |abcdefg| | | -------- -------- say you are trying to move the string "defghi" one space left, resulting in the final string "abdefghijk" because the page fault happens in the middle of the move, if you simply restart the command, you will end up moving the first half twice. you could end up with the result "adefgghijk" this is surly not what you want. The solution: Check that the operation will work without a pagefault first, then run the operation. Programmers in the past have had to use overlays, where they have to break their programs into segments...It has been shown that they are not very good at this. What are some possible page replacement algorithms? Random FIFO - first come first out LRU - least recently used MRU - most recently used OPT/MIN - predicts the future to give optimum results Evaluating page replacement algorithms: * cost of a page fault: * 3000-10000 instructions, probably even more these days * Multiprogramming Idle * I/O busy * Main mem or cache interference * Real time delay to handle page faults | Mem | ___ Usage | __ __| |__ | __| |__| |__ |_| |__ |_______________________|_ Time the space time product = integral(s(t),0,t,dt) the discrete time version is: STP = sum(m(i)(1+PFT*f(i)),i=0,R) examples of the different algorithms: LRU: pages being used: 4 3 2 1 4 3 5 4 3 2 1 5 ----------------------------------------------------------------- pages (4) (3) (2) (1) [4] [3] (5) 4 3 (2) (1) (5) in 4 3 2 1 4 3 5 4 3 2 1 memory 4 3 2 1 4 3 5 4 3 2 4 3 2 1 1 1 5 4 3 () - page fault with 4 pages in table [] - page faults with 3 pages in table so, we can see with LRU that we had 8 faults with 4 pages and 10 faults with 3 pages. FIFO: pages being used: 4 3 2 1 4 3 5 4 3 2 1 5 ---------------------------------------------------------------- pages (4) 4 4 4 4 4 (5) 5 5 5 (1) 1 in (3) 3 3 3 3 3 (4) 4 4 4 (5) memory (2) 2 2 2 2 2 (3) 3 3 3 (1) 1 1 1 1 1 (2) 2 2 here we notice that with 4 pages, we had a total of 10 page faults and with 3 pages we had a total of 9 page faults! this is a case where adding more memory actually made the problem worse! strange OPT/Min: pages being used: 4 3 2 1 4 3 5 4 3 2 1 5 ---------------------------------------------------------------- pages (4) (3) (2) (1) 4 3 (5) 4 3 [2] (1) 5 in 4 4 4 1 4 4 5 5 5 5 1 memory 3 3 3 1 3 3 4 3 2 2 2 2 2 2 2 2 4 3 3 Using OPT/MIN we were able to get the best results yet, with 6 page falts with 4 pages and 7 page faults with 3 pages