1 / 82

Operating Systems

Explore basic concepts, swapping, virtual memory, page replacement, segmentation, and implementation challenges in memory management within operating systems.

ppagano
Download Presentation

Operating Systems

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. Operating Systems 软件学院 高海昌 hchgao@xidian.edu.cn

  2. Contents 1. Introduction ** 2. Processes and Threads ******* 3. Deadlocks ** 4. Memory Management ***** 5. Input/Output *** 6. File Systems **** 8. Multiple Processor Systems * 9. Security **

  3. 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 (分段)

  4. 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

  5. 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

  6. 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.

  7. Multiprogramming with Fixed Partitions • Fixed memory partitions • separate input queues for each partition • single input queue

  8. Modeling Multiprogramming CPU utilization as a function of number of processes in memory Degree of multiprogramming

  9. 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% • …

  10. 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

  11. 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

  12. Base and Limit register

  13. 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 (分段)

  14. 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

  15. 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.

  16. Swapping (2) • Allocating space for growing data segment • Allocating space for growing stack & data segment

  17. 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.

  18. Memory Management with Linked Lists Four neighbor combinations for the terminating process X

  19. 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

  20. Lesson 2 Operating Systems

  21. 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 (分段)

  22. 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.

  23. Paging (1) The position and function of the MMU MMU maps the virtual addresses onto the physical memory addresses

  24. 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

  25. Address Translation Architecture

  26. Page Tables (1) Internal operation of MMU with 16 4KB pages MOVE REG, 8196 → MOVE REG, 24580

  27. 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.

  28. 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

  29. 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.

  30. 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

  31. 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.

  32. TLBs A TLB to speed up paging

  33. 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.

  34. 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 (分段)

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. The Clock Page Replacement Algorithm

  41. Lesson 3 Operating Systems

  42. 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

  43. 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

  44. Simulating LRU in Software • The aging algorithm simulates LRU in software • Note 6 pages for 5 clock ticks, (a) – (e)

  45. 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.

  46. 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

  47. The Working Set Page Replacement Algorithm (2) The working set algorithm

  48. The WSClock Page Replacement Algorithm Operation of the WSClock algorithm

  49. Review of Page Replacement Algorithms

  50. 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 (分段)

More Related