1 / 74

Chapter 9: Virtual Memory

Chapter 9: Virtual Memory. Chapter 9: Virtual Memory. Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel Memory Other Considerations Operating-System Examples. Objectives.

Télécharger la présentation

Chapter 9: Virtual Memory

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. Chapter 9: Virtual Memory

  2. Chapter 9: Virtual Memory • Background • Demand Paging • Copy-on-Write • Page Replacement • Allocation of Frames • Thrashing • Memory-Mapped Files • Allocating Kernel Memory • Other Considerations • Operating-System Examples

  3. Objectives • To describe the benefits of a virtual memory system • To explain the concepts of demand paging, page-replacement algorithms, and allocation of page frames • To discuss the principle of the working-set model

  4. Background • Virtual memory– a technique that allows a process to be executed even if it is not completely in physical memory • Only part of the program needs to be in memory for execution • Separates logical memory from physical memory • Logical address space can therefore be much larger than physical address space • Allows address spaces to be shared by several processes • Allows for more efficient process creation via copy-on-write. • Virtual memory can be implemented via: • Demand paging • Demand segmentation

  5. Virtual Memory that is Larger than Physical Memory swap out swap in  a reference not in memory disk MMU can be much larger than physical memory

  6. Virtual-address Space This hole is part of the virtual address space It requires actual physical pages only if the heap or stack grows Virtual address spaces that include holes are known as sparse address spaces contiguous in virtual address space

  7. Shared Library Using Virtual Memory Actual pages in physical memory Virtual address space of Process A Virtual address space of Process B

  8. Demand Paging • Demand paging: a technique that loads pages only when they are needed. • Advantages: • Less I/O needed if a process does not use all of its pages • Less memory needed (only pages used are loaded) • More users (each user takes less physical memory) • When a page is needed (when there is a reference to it): • invalid reference  abort • not-in-memory  bring to memory • Lazy swapper– never swaps a page into memory unless page will be needed • Swapper that deals with pages is called a pager

  9. Transfer of a Paged Memory to Contiguous Disk Space A demand paging system is similar to a paging system with swapping

  10. Hardware Support: using Valid-Invalid Bit • With each page table entry a valid–invalid bit is associated(v in-memory,i  not-in-memory or invalid) • Initially valid–invalid bit is set to i on all entries • Example of a page table snapshot: • During address translation, if valid–invalid bit in page table entry is i  page fault Frame # valid-invalid bit v v v v i …. i i page table

  11. Page Table When Some Pages Are Not in Main Memory

  12. Page Fault • If there is a reference to a page, first reference to that page will trap to operating system  causing a page fault • Steps in handling a page fault • Operating system looks an internal table (usually kept with PCB) to decide if the reference is • invalid reference  abort • or just not in memory (details are maintained in PCB regarding address ranges) • If not in memory, get empty frame • Swap page from disk into frame • Update tables • Set validation bit = v • Restart the instruction that was interrupted by the trap

  13. Steps in Handling a Page Fault

  14. Performance of Demand Paging • Page Fault Rate 0  p  1.0 • if p = 0 no page faults , every page referenced is in physical memory • if p = 1, every reference is a fault • Effective Access Time (EAT): EAT = (1 – p) x memory access + p ( page fault overhead + swap page out + swap page in + restart overhead ) See p.365-366 for the sequence involved in a page fault.

  15. Demand Paging Example • Memory access time = 200 nanoseconds • Average page-fault service time = 8 milliseconds • Effective Access Time (EAT) = (1 – p) x 200 + p (8 milliseconds) = (1 – p x 200 + p x 8,000,000 = 200 + p x 7,999,800 p: probability of a page fault • If one access out of 1,000 causes a page fault, then EAT = 8.2 microseconds. This is a slowdown by a factor of 40 !!

  16. Process Creation • Virtual memory allows other benefits during process creation: - Copy-on-Write - Memory-Mapped Files (later)

  17. Copy-on-Write • Copy-on-Write (COW) : a common technique that allows both parent and child processes to initially share the same pages in memory.If either process modifies a shared page, only then is the page copied • COW allows more efficient process creation as only modified pages are copied • Free pages are allocated from a pool of zeroed-out pages (pages whose previous contents have been erased before being allocated), typically allocated for a process when its stack and heap must expand or when there are COW pages to be managed

  18. Before Process 1 Modifies Page C (parent) (child)

  19. AfterProcess 1 Modifies Page C

  20. What happens if there is no free frame? • Over-allocation of memory While a process is executing, a page fault occurs. The OS determines where the page is residing on the disk but then finds that there is no free frames  all memory is in use Options: 1. Terminate the user process 2. Swap out a process, freeing its frames 3. Page replacement

  21. Page Replacement • Page replacement – find some page in memory, but not really in use, swap it out • Replacement algorithm • Performance – want an algorithm which will result in minimum number of page faults • Same page may be swapped in and out of memory several times

  22. Need For Page Replacement no free frame Page M not yet in the memory, but no free frame for M

  23. Basic Page Replacement • Find the location of the desired page on disk • Find a free frame: - If there is a free frame, use it - If there is no free frame, use a page replacement algorithm to select a victim frame - Write the victim frame to the disk, change the page table • Bring the desired page into the (newly) free frame; update the page table • Restart the process

  24. Page Replacement

  25. Page Replacement • If no frames are free, two page transfers (one out and one in) are required per page fault  doubles the page-fault services time. • We can use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk • A modify bit is associated with each page or frame in the hardware. This bit is set by the hardware whenever any word or byte in the page is written into (modified). • Page replacement completes the separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory

  26. Page Replacement Algorithms • We want lowest page-fault rate • The algorithm can be evaluated by running it on a particular string of memory references (reference string) and computing the number of page faults on that string • The reference string can be generated artificially or can be obtained by tracing a given system and recording the address of each memory references. • In all our first example, there are 5 pages. The reference string (pages referenced) is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

  27. Graph of Page Faults Versus The Number of Frames In general, we expect a curve like this.

  28. First-In-First-Out (FIFO) Algorithm • Reference string (12 pages): 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 • 3 frames (3 pages per process can be in memory at a time) • 4 frames • Belady’s Anomaly: the page fault rate may increase as the number of allocated frames increases, for some page replacement algorithms 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 1 4 5 2 2 9 page faults 1 3 3 3 2 4 1 1 5 4 2 2 10 page faults 1 5 3 3 2 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 4 4 3

  29. FIFO Illustrating Belady’s Anomaly

  30. FIFO Page Replacement 2nd Example 15 page faults

  31. Optimal (OPT) Algorithm • Replace page that will not be used for longest period of time • 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 • Q: How do you know which will not be used for the longest period of time? • A: Requires future knowledge of the reference string not feasible • Used for measuring how well your algorithm performs 1 4 2 6 page faults  3 4 5

  32. Optimal Page Replacement : the page that will not be used for the longest period of time. : the page that causes a page fault

  33. Least Recently Used (LRU) Algorithm • The FIFO algorithm uses the time when it was brought into memory • The OPT algorithm uses the time when a page is not to be used. • OPT is not feasible  find an approximation by using the recent past  Least recently used algorithm (LRU) • LRU replaces the page that has not been used for the longest period of time. • Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 1 1 5 1 2 2 2 2 2 5 4 3 4 5 3 3 4 3 4

  34. Implementations of the LRU Algorithm • Two implementations are feasible: 1. Counters 2. Stack • Counter implementation • Associate with each page the time of that page’s last use • Include a time-of-use field in each page entry and add a logical clock(counter) to the CPU • The clock is incremented for every memory reference. Whenever a reference to a page is made, the contents of the clock register are copied to the time-of-use field. • When a page needs to be replaced, look at the counters to determine which one to change (the one with the smallest value).

  35. LRU Page Replacement

  36. LRU Algorithm (Cont.) • Stack implementation – keep a stack of page numbers in a doubly linked list form, with a head pointer (to the top) and a tail pointer (to the bottom): • Whenever a page is referenced: • move it to the top • this requires removing a page from the middle of the stack  that is why a doubly linked list is used. • Removing a page and putting it on the top of the stack requires 6 pointers to be changed (at most) • Each update is a little more expensive. But there is no search for replacement since the page to be replaced is at the tail. head … null … null tail

  37. Use Of A Stack to Record The Most Recent Page References

  38. LRU Approximation Algorithms • The updating of the clock field or stack must be done for every memory reference. Software update would incur too much overhead. Most systems do not provide sufficient hardware support. Many systems, however, provide some help in the form of a reference bit. Algorithms that approximate LRU. • Reference bit • With each page associate a bit, initially = 0 • When page is referenced, bit set to 1 (by hardware) • Replace the one which is 0 (if one exists) • We do not know the order of use, however

  39. LRU Approximation Algorithms • Additional-Reference-Bits Algorithm • To gain additional ordering info, we can keep an 8-bit byte for each page in a table in memory • At regular intervals, the reference bit is shifted to the high-order bit of this byte, other bits shifted right by 1 bit and discarding the low-order bit. • These 8-bit bytes contain the history of page use for the last eight time periods 0000 0000  has not been used for the eight time periods 1111 1111  has been used at least once in period 1100 0100 has been used more recently than 0111 0111 • If we interpret these eight-bit bytes as unsigned integers, the page with the lowest number is the LRU page, and it can be replaced.

  40. LRU Approximation Algorithms • Second-chance Algorithm • The basic algorithm of this is FIFO. However, it needs a reference bit. • When a page has been selected, we inspect its reference bit. If the bit is 0, we proceed and replace the page. If the page to be replaced (in clock order) has reference bit = 1, then • Set its reference bit 0 • Leave the page in memory (give it a second chance) • Move on to select the next FIFO page (in clock order), subject to the same rules • Sometimes referred to as the clock algorithm

  41. Implementation ofSecond-Chance (clock) Page-Replacement Algorithm replaced with the new page

  42. Counting Algorithms • Keep a counter of the number of references that have been made to each page • LFU Algorithm: replaces page with smallest count • MFU Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used • Implementation is expensive • Do not approximate OPT replacement well

  43. Allocation of Frames • Each process needs a minimum number of pages • Example: IBM 370 – 6 pages to handle SS (storage to storage) MOVE instruction: • The MVC nstruction is 6 bytes long ( more than one word), might span 2 pages • 2 pages to handle characters at the from address (source) • 2 pages to handle characters at the to address (target) • Two major allocation schemes • fixed allocation • priority allocation

  44. Fixed Allocation • Equal allocation – For example, if there are 100 frames and 5 processes, give each process 20 frames. • Proportional allocation – Allocate according to the size of process

  45. Priority Allocation • Use a proportional allocation scheme using priorities rather than size • If process Pi generates a page fault, • select for replacement one of its frames • select for replacement a frame from a process with lower priority number

  46. Global vs. Local Allocation • Global replacement– process selects a replacement frame from the set of all frames; one process can take a frame from another • Local replacement– each process selects from only its own set of allocated frames (So much for this chapter)

  47. Thrashing • If a process does not have “enough” pages, the page-fault rate is very high. This leads to: • low CPU utilization • operating system thinks that it needs to increase the degree of multiprogramming • another process added to the system • Thrashing a process is busy swapping pages in and out

  48. Thrashing (Cont.)

  49. Demand Paging and Thrashing • Why does demand paging work?Locality model • Process migrates from one locality to another • Localities may overlap • Why does thrashing occur? size of locality > total memory size

  50. Locality In A Memory-Reference Pattern

More Related