Principles of Operating Systems: Design & Applications
530 likes | 748 Vues
Principles of Operating Systems: Design & Applications. Chapter 9 Principles of Memory Management. Objectives. understand the relationships among the various types of memory in the memory hierarchy understand the purpose and general idea of address translation
Principles of Operating Systems: Design & Applications
E N D
Presentation Transcript
Principles of Operating Systems: Design & Applications Chapter 9 Principles of Memory Management
Objectives • understand the relationships among the various types of memory in the memory hierarchy • understand the purpose and general idea of address translation • understand segmented address translation • understand paged address translation • be able to identify the application of each of the memory services
Objectives • have a solid mental image of the various approaches to memory layout, both globally and per-process • be able to compare free space representations and apply them at appropriate times • understand the difference between internal and external fragmentation • understand the various memory allocation techniques • understand the various page replacement policies
The Memory Hierarchy • Engineering tradeoff: • Cost and volumeversus • Speed and capacity • For a given point in time, increased speed and capacity require greater cost and volume Principles of Operating Systems: Design & Applications 4
Memory Hierarchy (cont.) Principles of Operating Systems: Design & Applications 5
Address Translation • Relocation • Protection • Convert virtual addresses to physical addresses • Needs to be fast: done in hardware Principles of Operating Systems: Design & Applications 6
Base/Limit Translation Principles of Operating Systems: Design & Applications 7
Segmented Translation Principles of Operating Systems: Design & Applications 8
Page Table Principles of Operating Systems: Design & Applications 9
Two-Level Page Table Principles of Operating Systems: Design & Applications 10
Page Table Entry • Page Frame Number (PFN): becomes upper bits of physical address • Protection bits: determine allowed accesses • Present bit (P): indicates whether page is resident in memory • Dirty bit (D): indicates whether page has been written to • Accessed bit (A): indicates whether page has been referenced Principles of Operating Systems: Design & Applications 11
Translation Lookaside Buffer • Page table lookups are usually memory references • Slow memory references by factor of 2, 3, 4, or even 5 • TLB caches most recent PTEs Principles of Operating Systems: Design & Applications 12
Inverted Page Table • Full page table can be prohibitively large • Often page frames in use are a small fraction of virtual address space • Alternative: translate page frame numbers to page numbers • Use TLB for recent translations Principles of Operating Systems: Design & Applications 13
Memory Services • Explicit allocation: process specifies virtual address range • Implicit allocation: kernel decides virtual range • Garbage collection: identify and free unused blocks • Memory mapped file: file whose contents are directly associated with a region in the virtual address space Principles of Operating Systems: Design & Applications 14
Typical System Memory Layout Principles of Operating Systems: Design & Applications 15
Typical Process Memory Layout Principles of Operating Systems: Design & Applications 16
Free Space Management • Free bit map: • Good for fixed-sized blocks, like pages • Low overhead • Free list: • Sometimes tree or hash table • Standard trick: store overhead structures in free blocks themselves Principles of Operating Systems: Design & Applications 17
Free List Example Principles of Operating Systems: Design & Applications 18
Fragmentation • External fragmentation: wasted space outside allocation units • Internal fragmentation: wasted space within allocation units Principles of Operating Systems: Design & Applications 19
Partitions • Statically sized • Allocated in whole • Usually set up at boot time • Usually administratively determined Principles of Operating Systems: Design & Applications 20
Variable-Sized Allocation • Among free blocks at least as large as the request: • First fit: pick the first • Next fit: pick the next • Best fit: pick the smallest • Worst fit: pick the biggest Principles of Operating Systems: Design & Applications 21
First Fit Example Principles of Operating Systems: Design & Applications 22
Next Fit Example Principles of Operating Systems: Design & Applications 23
Best Fit Example Principles of Operating Systems: Design & Applications 24
Worst Fit Example Principles of Operating Systems: Design & Applications 25
Buddy System • Allocation algorithm: • If n is less than the smallest allocation unit, set n to that smallest size • Round n up to the nearest power of 2 times the smallest allocation unit • If there is no block of the needed size, recursively allocate the next larger size and split it into two • Return the first block of the required size Principles of Operating Systems: Design & Applications 26
Buddy System (cont.) Principles of Operating Systems: Design & Applications 27
Overallocation • Swapping: copying full processes between main memory and backing store • Segment swapping: copying individual segments between main memory and backing store • Paging (page swapping): copying individual pages between main memory and backing store Principles of Operating Systems: Design & Applications 28
Paging Concepts • Demand paging: reading pages into memory in response to page faults • Page fault: an interrupt that occurs when a translation is not valid, often because the page is not resident in memory • Prepaging: loading pages in anticipation of later needing them Principles of Operating Systems: Design & Applications 29
Paging Concepts (cont.) • Page reference string: list of page numbers in the order in which they are referenced • Global page replacement policy: page replacement policy that selects pages without regard to the processes that own them • Local page replacement policy: page replacement policy that selects pages only from a given process Principles of Operating Systems: Design & Applications 30
Belady's Min • Optimal page replacement policy • Pick the page for which it will be longest before it's used again: • Unrealizable—noncausal • Standard of comparison for realizable policies Principles of Operating Systems: Design & Applications 31
First In, First Out • Simple page replacement policy • Select the page that has been resident for the longest period of time • Implemented with a queue • Can “jump the gun” and swap out frequently used pages just because they've been there a long time Principles of Operating Systems: Design & Applications 32
Second Chance • Address FIFO weakness • If the process at the head of the queue has A=1, clear A and reinsert it at the tail, otherwise, swap it out • Allows frequently used pages to stay in memory for a long time • If all pages have A=1, then all get cleared and the oldest is swapped out—reduces to FIFO Principles of Operating Systems: Design & Applications 33
Clock Algorithm • Implementation of second chance • Pages are arranged in a circle and a “clock hand” sweeps them • At each step, if A=1, clear A and advance, otherwise, copy the page out Principles of Operating Systems: Design & Applications 34
Two Hand Clock • If there are many pages, nearly all may have A=1 • Reduce time between clearing A and testing • Two “clock hands” move in lockstep: first clears, second tests Principles of Operating Systems: Design & Applications 35
Not Recently Used • If D=0 (M=0), the page need not be written; the new one can be copied directly in to the frame • Pages with D=0, can be swapped faster • Let AD (AM) be a two bit number: 00, 01, 10, 11 (0, 1, 2, 3) • Select a page from the set with the lowest number Principles of Operating Systems: Design & Applications 36
Least Recently Used • Pages not used for a long time may not be needed for a long time • Least recently used is a realizable approximation to Belady's min • Rarely implemented directly—hardware rarely records access time • Approximate by periodically clearing P and recording the time of the page fault before resetting P Principles of Operating Systems: Design & Applications 37
Not Frequently Used • Approximate LRU by looking at recent frequency of usage • For each sweep of pages compute:where • Pick a page with the lowest value of c Principles of Operating Systems: Design & Applications 38
Paging Policies Comparison Principles of Operating Systems: Design & Applications 39
Paging Policies Comparison (cont.) Principles of Operating Systems: Design & Applications 40
The Working Set • Set of pages currently “in use” by a process • Keeping more pages is unneeded • Keeping fewer pages leads to a large number of page faults • Attempt to keep the number of pages between two thresholds • Page locally if above the upper • Swap out whole processes that are below the lower Principles of Operating Systems: Design & Applications 41
Page Fault Frequency • Technique to adjust working set thresholds • Keep an average of the process's page fault frequency • If too high, increase the working set size • If very low, decrease the working set size Principles of Operating Systems: Design & Applications 42
Copy on Write (COW) • Technique to avoid copying pages unnecessarily • Map both process's pages to same frames • Make the “copied” pages read-only • When a process writes, the fault triggers: • Make a copy of the page • Map the two process's pages to separate copies • Change the access back to its original value Principles of Operating Systems: Design & Applications 43
Average Access Time • Average access time is given by:where is the time to access memory, is the time to access the disk, and is the fraction of accesses causing page faults • must be very small to keep average time reasonable Principles of Operating Systems: Design & Applications 44
Thrashing • Effect of active working sets being larger than the physical memory • The system spends more time swapping than computing • System performance intolerably slow • Can be partially ameliorated with two-level scheduling Principles of Operating Systems: Design & Applications 45
Belady's Anomaly • Phenomenon where adding more page frames leads to more page faults • Some policies, such as FIFO, are susceptible • Some policies, such as LRU, are immune Principles of Operating Systems: Design & Applications 46
Belady's Anomaly Principles of Operating Systems: Design & Applications 47
Summary • Memory hierarchy gives perspective on tradeoffs of speed and size • Address translation is needed for relocation and protection • Translation can be in terms of segments, using base and limit registers • Translation can be in term of pages using page tables containing page table entries Principles of Operating Systems: Design & Applications 48
Summary (cont.) • TLB caches recently used PTEs • Both system and per-process memory layouts start at address 0 • Typically process data segments grow up toward stack and stack down toward data • Free memory can be represented by bitmaps and by lists • Free lists typically keep pointers and sizes inside the free blocks Principles of Operating Systems: Design & Applications 49
Summary (cont.) • Fragmentation reduces usable memory • Partitioning is a simple and old technique being used again with virtualization • Variable-sized blocks can be allocated by first fit, next fit, best fit, worst fit, or buddy system • Memory can be overallocated by using swapping, segment swapping, or page swapping Principles of Operating Systems: Design & Applications 50