OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Chapter 3 Memory Management (part 2) MMU Algorithm Let V be a virtual address. Let (i1, i2, ) be the page numbers of V and O be the offset a) If Search for (i1, i2, ) in TLB = SUCCESS a.1) re-order TLB according to LRU a.2) let E be the table entry corresponding to (i1, i2, ) in TLB a.3) if E.present/absent = PRESENT then a.3.1) if E.rights is compatible with memory access a.3.2) then do the memory access at E.frame + O a.3.3) else interrupt with Segmentation Fault a.4) else (E.present/absent = ABSENT) a.4.1) Prepare a Page Replacement to load the page in a frame (c.f. page replacement algorithms) a.4.2) mark the process as waiting for I/O a.4.3) schedule() b) Else (Search in TLB has failed) b.1) Let 2nd level Table = Top Table[i1] (using the same algorithm) b.2) Let 3rd level Table = 2nd level Table[i2] (using the same algorithm) b.3) etc b.4) Let Page = xth level Table[i(x-1)] (using the same algorithm) b.5) goto a) 1
Page Replacement Algorithms Optimal replacement Not recently used (NRU) replacement First-in, first-out (FIFO) replacement Second chance replacement Clock page replacement Least recently used (LRU) replacement Not Recently Used Class 0: not referenced, not modified Class 1: not referenced, modified Class 2: referenced, not moified Class 3: referenced, modified 2
First In First Out The page A is expelled in case of Page Fault. Newest pages are put in front. Second Chance Replacement Figure 4-14. Operation of second chance. (a) Pages sorted in FIFO order. (b) Page list if a page fault occurs at time 20 and A has its R bit set. The numbers above the pages are their loading times. 3
Clock Page Replacement Figure 4-15. The clock page replacement algorithm. 2) Column = 0 Simulating LRU in Software (1) 1) Row = 1 Figure 4-16. LRU using a matrix when pages are referenced in the order 0, 1, 2, 3, 2, 1, 0, 3, 2, 3. 4
Simulating LRU in Software (2) Figure 4-17. The aging algorithm simulates LRU in software. Shown are six pages for five clock ticks. The five clock ticks are represented by (a) to (e). The Working Set Model k Figure 4-18. The working set is the set of pages used by the k most recent memory references. The function w(k, t) is the size of the working set at time t. 5
Local versus Global Allocation Policies Figure 4-19. Local versus global page replacement. (a) Original configuration. (b) Local page replacement. (c) Global page replacement. Page Fault Frequency Figure 4-20. Page fault rate as a function of the number of page frames assigned. 6
Segmentation (1) Examples of tables saved by a compiler 1. The source text being saved for the printed listing (on batch systems). 2. The symbol table, containing the names and attributes of variables. 3. The table containing all the integer and floating-point constants used. 4. The parse tree, containing the syntactic analysis of the program. 5. The stack used for procedure calls within the compiler. These will vary in size dynamically during the compile process Segmentation (2) Figure 4-21. In a one-dimensional address space with growing tables, one table may bump into another. 7
Segmentation (3) Figure 4-22. A segmented memory allows each table to grow or shrink independently of the other tables. Segmentation (4)... Figure 4-23. Comparison of paging and segmentation. 8
Segmentation (4)... Figure 4-23. Comparison of paging and segmentation. Implementation of Pure Segmentation Figure 4-24. (a)-(d) Development of checkerboarding. (e) Removal of the checkerboarding by compaction. 9