820 likes | 830 Views
Explore basic concepts, swapping, virtual memory, page replacement, segmentation, and implementation challenges in memory management within operating systems.
E N D
Operating Systems 软件学院 高海昌 hchgao@xidian.edu.cn
Contents 1. Introduction ** 2. Processes and Threads ******* 3. Deadlocks ** 4. Memory Management ***** 5. Input/Output *** 6. File Systems **** 8. Multiple Processor Systems * 9. Security **
Chapter 4: Memory Management 4.1 Basic memory management 4.2 Swapping (交换) 4.3 Virtual memory (虚拟内存) 4.4 Page replacement algorithms 4.5 Design issues for paging systems 4.6 Implementation issues 4.7 Segmentation (分段)
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
Basic Memory Management • Memory Management Systems can be divided into two classes: • That move processes back and forth between main memory and disk during execution (swapping and paging). • That do not. • Program expand to fill the memory available to hold them
Monoprogramming without Swapping or Paging Three simple ways of organizing memory • an operating system with one user process • (a) Formally used on mainframes and minicomputers but is really used any more. • (b) used on some palmtop computers and embedded systems. • (c) used by early PC (MS-DOS), where the portion of the system in the ROM is called the BIOS.
Multiprogramming with Fixed Partitions • Fixed memory partitions • separate input queues for each partition • single input queue
Modeling Multiprogramming CPU utilization as a function of number of processes in memory Degree of multiprogramming
Modeling Multiprogramming • Suppose a computer has 32MB of memory, with the OS taking up 16MB and each user program taking up 4MB. • These sizes allow 4 programs to be in memory at once. With an 80% average I/O wait, we have a CPU utilization of 1-0.8^4≈60%. • Adding another 16MB of memory allow 8 programs, thus raising the CPU utilization to 83%. • Adding yet another 16MB of memory allow 12 programs, only increase CPU utilization to 93%. • …97% • …
Analysis of Multiprogramming System Performance • (a) Arrival and work requirements of 4 jobs • (b) CPU utilization for 1 – 4 jobs with 80% I/O wait • (c) Sequence of events as jobs arrive and finish • note numbers show amount of CPU time jobs get in each interval
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 addr • address locations larger than limit value is an error
Chapter 4: Memory Management 4.1 Basic memory management 4.2 Swapping (交换) 4.3 Virtual memory (虚拟内存) 4.4 Page replacement algorithms 4.5 Design issues for paging systems 4.6 Implementation issues 4.7 Segmentation (分段)
Swapping Sometimes there is not enough main memory to hold all the currently active processes. Two general approaches to memory management: Swapping, bringing in each process in its entirety, running it for a while, then putting it back on the disk. Virtual memory, allow programs to run even when they are only partially in main memory. • 14
Swapping (1) • Memory allocation changes as: • processes come into memory • leave memory • Shaded regions are unused memory. Addresses A must be relocated since it is now at a different location. • Hole, memory compaction. • Difference between fix and the variable partitions.
Swapping (2) • Allocating space for growing data segment • Allocating space for growing stack & data segment
Memory Management with Bit Maps • (a) Part of memory with 5 processes, 3 holes • tick marks show allocation units • shaded regions are free • (b) Corresponding bit map (Shortcoming: searching a bitmap for a run of a given length is a slow operation). • (c) Same information as a list.
Memory Management with Linked Lists Four neighbor combinations for the terminating process X
Memory Management with Linked Lists • Several algorithms can be used to allocate memory for a newly created process: • First fit. • Next fit. • Best fit. • Worst fit. • Quick fit. • 19
Lesson 2 Operating Systems
Chapter 4: Memory Management 4.1 Basic memory management 4.2 Swapping (交换) 4.3 Virtual memory (虚拟内存) 4.4 Page replacement algorithms 4.5 Design issues for paging systems 4.6 Implementation issues 4.7 Segmentation (分段)
Virtual Memory • The basic idea behind virtual memory is that the combined size of the program, data, and stack may exceed the amount of physical memory available for it. • The operating system keeps those parts of the program currently in use in main memory, and the rest on the disk.
Paging (1) The position and function of the MMU MMU maps the virtual addresses onto the physical memory addresses
Paging (2) • The virtual address space is divided up into units called pages. • The corresponding units in the physical memory are called page frames. MOVE REG, 0 → MOVE REG, 8192 MOVE REG, 8192 → MOVE REG, 24576 MOVE REG, 20500 (20480+20) → MOVE REG, 12308 MOVE REG, 32780 → page fault
Page Tables (1) Internal operation of MMU with 16 4KB pages MOVE REG, 8196 → MOVE REG, 24580
Page Tables (2) • Purpose of the page table is to map virtual pages onto page frame. Two major issues must be faced: • The page table can be extremely large. (232=220*4k) • The mapping must be fast. • The simplest design is to have a single page table consisting of an array of fast hardware registers, with one entry for each virtual page, indexed by virtual page number. • Advantage: it is straightforward and requires no memory references during mapping. • Disadvantage: it is potentially expensive, and hurts performance. • Another extreme, the page table can be entirely in main memory. All the hardware needs then is a single register that points to the start of the page table. • Disadvantage: requiring one or more memory references to read page table entries during the execution of each instruction.
Multilevel Page Tables Second-level page tables • 32 bit address with 2 page table fields • Two-level page tables (232=4G, 4M, 4K) • 0x00403004: PT1=1, PT2=3, Offset=4 PT1=1 → 4M~8M PT2=3 → 12K~16K (+4M) → 4206592~4210687
Page Tables (4) Typical page table entry Page frame number: most important field, the goal of the page mapping; Present /absent: if this bit is 1, the entry is valid and can be used; Protection: tell what kinds of access are permitted, R/W/X. Modified: useable in reclaiming a page frame. If dirty, write back to disk. Referenced: the bit is set whenever a page is referenced. Help the OS choose a page to evict when a page fault occurs. Caching disabled: useful for pages that map onto device register rather than memory.
Page Tables (exercise) In the paged memory management system, the address is composed of page number and the offset within the page. In the address structure showed in the following figure, . A.page size is 1K, 64 pages at most B.page size is 2K, 32 pages at most C.page size is 4K, 16 pages at most D.page size is 8K, 8 pages at most
Translation Lookaside Buffers TLB, 转换检测缓冲区 • Most programs tend to make a large number of references to a small number of pages. • Solution: equip computers with a small hardware device for mapping virtual addresses to physical addresses without going through the page table. The device called TLB.
TLBs A TLB to speed up paging
Inverted Page Tables Comparison of a traditional page table with an inverted page table Downside: virtual-to-physical translation becomes much harder. Solution: TLB, Hash table. For 64-bit computers, things change. There is one entry per page frame in real memory, rather than one entry per page of virtual address space.
Chapter 4: Memory Management 4.1 Basic memory management 4.2 Swapping (交换) 4.3 Virtual memory (虚拟内存) 4.4 Page replacement algorithms 4.5 Design issues for paging systems 4.6 Implementation issues 4.7 Segmentation (分段)
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 • “page replacement” occurs in other areas of computer design as well • Memory cache • Web server
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
Not Recently Used (NRU, 最近未使用) • Each page has Reference bit, Modified bit • bits are set when page is referenced, modified • Pages are classified: 0. not referenced, not modified 1. not referenced, modified 2. referenced, not modified 3. referenced, modified • NRU removes page at random from lowest numbered non empty class
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 being replaced may be often used
Second Chance Page Replacement Algorithm • Operation of a second chance • pages sorted in FIFO order • Looking for an old page that has not been referenced in the previous clock interval
Lesson 3 Operating Systems
Least Recently Used (LRU, 最近最少使用) • Assume 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
Simulating LRU in Hardware LRU using a matrix – pages referenced in order 0, 1, 2, 3, 2, 1, 0, 3, 2, 3 LRU: 3, 3, 3, 0, 0, 0, 3, 2, 1, 1
Simulating LRU in Software • The aging algorithm simulates LRU in software • Note 6 pages for 5 clock ticks, (a) – (e)
The Working Set • Demand paging (请求调页): pages are loaded only on demand, not in advance. • Locality of reference: during any phase of execution, the process references only a relatively small fraction of its pages. • Working set: the set of pages that a process is currently using. • What to do when a process is brought back in again? • Working set model: Many paging system keep track of each process’ working set and make sure that it is in memory before letting the process run.
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 k
The Working Set Page Replacement Algorithm (2) The working set algorithm
The WSClock Page Replacement Algorithm Operation of the WSClock algorithm
Chapter 4: Memory Management 4.1 Basic memory management 4.2 Swapping (交换) 4.3 Virtual memory (虚拟内存) 4.4 Page replacement algorithms 4.5 Design issues for paging systems 4.6 Implementation issues 4.7 Segmentation (分段)