1 / 78

第四章 記憶體管理

第四章 記憶體管理. 鄧姚文 http://www.ywdeng.idv.tw. 大綱. Basic memory management Swapping Virtual memory 虛擬記憶體 Page replacement algorithms 分頁置換 Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation. Memory Management .

jesus
Télécharger la présentation

第四章 記憶體管理

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第四章記憶體管理 鄧姚文 http://www.ywdeng.idv.tw Tanenbaum作業系統2Ed

  2. 大綱 • Basic memory management • Swapping • Virtual memory 虛擬記憶體 • Page replacement algorithms 分頁置換 • Modeling page replacement algorithms • Design issues for paging systems • Implementation issues • Segmentation Tanenbaum作業系統2Ed

  3. Memory Management • Ideally programmers want memory that is • large • fast • non volatile 內容不因斷電而消失 • Memory hierarchy • 快取記憶體:small amount of fast, expensive memory – cache • 主記憶體:some medium-speed, medium price main memory • 磁碟:gigabytes of slow, cheap disk storage • Memory manager handles the memory hierarchy Tanenbaum作業系統2Ed

  4. Basic Memory ManagementMonoprogramming without Swapping or Paging一部電腦、單一使用者、單一程式 Three simple ways of organizing memory - an operating system with one user process Tanenbaum作業系統2Ed

  5. Multiprogramming with Fixed Partitions記憶體分割:固定大小(有的大有的小) 程式必須整個全部載入 放在一個連續的區域 • Fixed memory partitions • separate input queues for each partition • single input queue Tanenbaum作業系統2Ed

  6. Modeling Multiprogramming執行許多支I/O-bound的程式可以提高CPU的使用率 CPU utilization as a function of number of processes in memory Degree of multiprogramming Tanenbaum作業系統2Ed

  7. Analysis of Multiprogramming System Performance 0.8 0.64 0.512 0.4096 • Arrival and work requirements of 4 jobs • CPU utilization for 1 – 4 jobs with 80% I/O wait • Sequence of events as jobs arrive and finish • note numbers show amount of CPU time jobs get in each interval Tanenbaum作業系統2Ed

  8. Relocation and Protection重新定位&保護 • Cannot be sure where program will be loaded in memory • address locations of variables, code routines cannot be absolute • must keep a program out of other processes’ partitions • Use base and limit values • 基底暫存器:address locations added to base value to map to physical address • 限制暫存器:address locations larger than limit value is an error Tanenbaum作業系統2Ed

  9. Swapping (1) • Memory allocation changes as • processes come into memory • leave memory • Shaded regions are unused memory Tanenbaum作業系統2Ed

  10. Memory Compaction • Combine multiple small holes into a single big one • Usually not done • On a 256MB machine • Copy 4 bytes in 40 ns • Takes about 2.7 sec. • All activities must be suspended during compaction Tanenbaum作業系統2Ed

  11. Swapping (2) • Allocating space for growing data segment • Allocating space for growing stack & data segment 堆疊向下長 資料向上長 Tanenbaum作業系統2Ed 分開一點 多配一點

  12. Memory Management with Bit Maps • Part of memory with 5 processes, 3 holes • tick marks show allocation units • shaded regions are free • Corresponding bit map • Same information as a list IF 1 unit = 4B 2MB bitmap for 1GB memory Tanenbaum作業系統2Ed

  13. Memory Management with Linked Lists 鏈結串列 • Four neighbor combinations for the terminating process X Tanenbaum作業系統2Ed

  14. Memory allocation for new processes (1) • Processes and holes are kept on a list sorted by address • First fit: start from list head, find the first one that is big enough • Next fit: circular linked list, starts searching from the next node • Best fit: find the smallest hole that is big enough • Worst fit: always takes the largest hole Tanenbaum作業系統2Ed

  15. Memory allocation for new processes (2) • Quick fit • Multiple linked list, each for a fix-sized memory block • 簡單就好 Tanenbaum作業系統2Ed

  16. Virtual Memory 虛擬記憶體 • When programs are too big to fit in memory… • 程式不必全部載入記憶體,也可以執行 • 只載入目前用到的部分 • 其餘存在硬碟上 • Overlay 覆蓋、覆疊 • Virtual memory Tanenbaum作業系統2Ed

  17. Virtual Memory 虛擬記憶體Paging (1) 分頁 • The position and function of the MMU Tanenbaum作業系統2Ed

  18. Paging 分頁 • Virtual address 虛擬位址 • 程式的觀點 • 程式指令送出的位址 • Physical address 實體位址 • 作業系統的觀點 • 實際的記憶體位址 • MMU maps the virtual address onto the physical address • Page table Tanenbaum作業系統2Ed

  19. Paging (2) The relation between virtual addressesand physical memory addresses given by page table Tanenbaum作業系統2Ed

  20. Page Tables (1) • Internal operation of MMU with 16 4 KB pages MOV AX, 6004H 1 page = 4 KB MOV AX, 2004H Tanenbaum作業系統2Ed

  21. Page Tables (2) Second-level page tables • 32 bit address with 2 page table fields • Two-level page tables Top-level page table Tanenbaum作業系統2Ed

  22. Page Tables (3) • Typical page table entry Present/absent bit = 0 Page fault: the referenced page is not in memory Dirty: modified bit = 1 Tanenbaum作業系統2Ed

  23. TLBs – Translation Lookaside Buffers • Associative memory • A TLB to speed up paging Tanenbaum作業系統2Ed

  24. Inverted Page Tables 反向分頁表 • Comparison of a traditional page table with an inverted page table 64-bit CPU Tanenbaum作業系統2Ed

  25. 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 Tanenbaum作業系統2Ed

  26. 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 Tanenbaum作業系統2Ed

  27. Not Recently Used Page Replacement Algorithm 最近沒用過 • Each page has Reference bit, Modified bit • bits are set when page is referenced, modified • Pages are classified • not referenced, not modified 首選 • not referenced, modified 可能嗎? • referenced, not modified • referenced, modified 最好不要選這個 • NRU removes page at random • from lowest numbered non empty class Tanenbaum作業系統2Ed

  28. 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 • Belady’s anomaly 記憶體增加,Page Fault 的次數未必降低 Tanenbaum作業系統2Ed

  29. 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) Tanenbaum作業系統2Ed

  30. The Clock Page Replacement Algorithm 時鐘 Tanenbaum作業系統2Ed

  31. Least Recently Used (LRU) 最近最少用 • 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 Tanenbaum作業系統2Ed

  32. LRU Hardware LRU using a matrix – pages referenced in order 0,1,2,3,2,1,0,3,2,3 Tanenbaum作業系統2Ed

  33. Not Frequently Used (NFU) 不常用 • Implement LRU in software • A software counter associated with each page, initially 0 • At each clock interrupt • Scan all the pages in memory • Add R bit into the counter • When a page fault occurs • The page with lowest counter is chosen for replacement Tanenbaum作業系統2Ed

  34. Aging 老化 • Modification of NFU • The counters are each shifted right 1 bit before the R bit is added • The R bit is added to the leftmost, rather than the rightmost bit Tanenbaum作業系統2Ed

  35. Aging • The aging algorithm simulates LRU in software • Note 6 pages for 5 clock ticks, (a) – (e) Tanenbaum作業系統2Ed

  36. Working Set 工作集合 • Demand paging 需求分頁 • 分頁被用到的時候才載入 • Locality of reference 參照的區域性 • 行程參照的範圍有侷限性 • Working Set 工作集合 • 現階段常常會使用到的分頁 • Threshing 擺盪 • 剛換出去的分頁,又被換進來 • Prepaging 預先分頁 • 在讓行程執行前,先載入一些分頁 Tanenbaum作業系統2Ed

  37. The Working Set Page Replacement Algorithm (1) • 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 Tanenbaum作業系統2Ed

  38. The Working Set Page Replacement Algorithm (2) The working set algorithm Tanenbaum作業系統2Ed

  39. The WSClock Page Replacement Algorithm Current virtual time 2204 Tanenbaum作業系統2Ed

  40. Review of Page Replacement Algorithms Tanenbaum作業系統2Ed

  41. Modeling Page Replacement AlgorithmsBelady's Anomaly • FIFO with 3 page frames • FIFO with 4 page frames • P's show which page references show page faults Tanenbaum作業系統2Ed

  42. Stack Algorithms 堆疊演算法 State of memory array, M, after each item in reference string is processed 2 3 5 3 Tanenbaum作業系統2Ed

  43. The Distance String • Characterizes stack algorithms by how far page frames have to move to the top of the stack • Distances depend on • The algorithm being executed • The paging method used • Probability analysis will • tell you how efficient your algorithm will run on a particular machine • Help predict Page Fault rates Tanenbaum作業系統2Ed

  44. The Distance String Probability density functions for two hypothetical distance strings k page frames is good enough Simply bad luck Tanenbaum作業系統2Ed

  45. Predicting Page Fault Rates • Computation of page fault rate from distance string • the C vector • the F vector 20 3 3 14 3 11 9 1 8 0 Tanenbaum作業系統2Ed

  46. Design Issues for Paging Systems • Local versus Global Allocation Policies • PFF: page fault frequency • Load control • Page size • Separate instruction and data spaces • Shared pages • Cleaning policy • Virtual memory interface Tanenbaum作業系統2Ed

  47. Local versus Global Allocation Policies (1) • Original configuration • Local page replacement • Global page replacement Tanenbaum作業系統2Ed

  48. Local versus Global Allocation Policies (2) Page fault rate as a function of the number of page frames assigned Tanenbaum作業系統2Ed

  49. 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 Tanenbaum作業系統2Ed

  50. 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 Tanenbaum作業系統2Ed

More Related