CS330 · Annotated SlidesChapter 9 — Virtual Memory
Chapter 9 · Annotated slides

Virtual Memory

Virtual memory larger than physical memory, demand paging and the valid–invalid bit, page faults and their handling, demand-paging performance, page replacement (FIFO and Belady’s anomaly, optimal, LRU) and thrashing.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up
Slide 1: Slide 1
Exam weight Low

    Outline of this lecture

    In this lecture, we will discuss the following:

    • Background
    • Demand Paging
    • Performance of Demand Paging
    • Page Replacement
    • Page-Replacement Algorithms
    • Thrashing
    2
    Exam weight Low

      Recall from previous chapter

      • One goal of memory management is to keep as many processes in memory simultaneously to allow multiprogramming.
      • Yet, they require that an entire process be in memory before it can be executed.
      • Therefore, the size of the program cannot exceed the size of physical memory.
      • Usually the entire code is not needed I,e.
        • Code to handle error conditions, which seldom occur doesn't need to be in main memory.
        • Even if entire program is needed it may not be needed at the same time.
      • Virtual memory is a technique that allows the execution of processes that are only partially in memory.
      3
      Exam weight Low

        Background

        • Virtual memory – separation of user logical memory from physical memory.
          • Only part of the program needs to be in memory for execution.
          • Logical address space can therefore be much larger than physical address space.
          • More programs can run at the same time - each user program takes up less physical memory. This increases CPU utilization and throughput.
        • Virtual memory can be implemented via:
          • Demand paging
          • Demand segmentation- more complex
        4
        Exam weight Low
        1. Virtual memory separates logical memory from physical memory: only part of a program needs to be in memory.
        Slide 5: Virtual Memory That is Larger Than Physical Memory
        Exam weight Low

          Demand paging

          Suppose a program starts with a list of available options from which the user is to select. Loading the entire program into memory results in loading the executable code for all options, regardless of whether or not an option is ultimately selected by the user. An alternative strategy is to load pages only as they are needed. This technique is known as demand paging and is commonly used in virtual memory systems.

          6
          Exam weight Low

            Demand Paging

            • In demand paging, processes reside on secondary storage (usually a hard disk).
            • Bring a page into memory only when it is needed using a lazy swapper (never swaps a page into memory unless that page will be needed).
            • When the process to be swapped in, the pager guesses which pages will be needed and only swaps those in.
              • Less I/O needed for loading and swapping
              • Less memory needed
              • Faster response
              • More users
            7
            Exam weight Low
            1. Demand paging: load a page only when it is needed (lazy swapper / pager).
            Slide 8: Transfer of a Paged Memory to Contiguous Disk Space
            Exam weight Low
              Slide 9: Slide 9
              Exam weight Low
              1. Valid–invalid bit: v = in memory, i = not in memory (or not legal).
              Slide 10: Page Table When Some Pages Are Not in MM
              Exam weight Low
                Slide 11: Page Fault
                Exam weight Low
                1. Referencing an invalid page → page fault trap to the OS.
                Slide 12: Steps in Handling a Page Fault
                Exam weight High
                1. Handling: trap → check the reference → find a free frame → read the page from disk → update the table, set v → restart the instruction.
                🎯 How this can come up · 1 question
                MCQ

                After the page is loaded, the OS:

                What happens if there is no free frame?

                • Page replacement – find some page in memory, but not really in use, swap it out.
                  • algorithm
                  • performance – want an algorithm which will result in minimum number of page faults.
                • Same page may be brought into memory several times.
                13
                Exam weight Low

                  Performance of Demand Paging

                  • Demand paging affect the performance of a computer system.
                  • Page Fault Rate 0  p  1.0
                    • if p = 0 no page faults -- → EAT = MAT
                    • if p = 1, every reference is a fault
                  • Effective Access Time (EAT) EAT = (1 – p) x ma + p * page fault service time EAT = (1 – p) x memory access + p (page fault overhead + [swap page out ] + swap page in + restart overhead)
                  14
                  Exam weight High
                  1. EAT = (1 − p)·memory access + p·page-fault time. Even a tiny p hurts because a fault costs milliseconds.
                  🎯 How this can come up · 1 question
                  MCQ

                  Memory 200 ns, fault service 8 ms, p = 0.001. EAT ≈

                  Demand Paging Example

                  • Memory access time = 200 nsecs
                  • Page fault service time= 8 msecs
                  • Effective access time in nanoseconds is: EAT = (1 – p) x ma + p x page fault service time = (1 – p)x200 + p x 8000000 = 200 + 7999800 p (in nsec)
                  15
                  Exam weight Low

                    Basic Page Replacement

                    1.

                    Find the location of the desired page on disk

                    2.

                    Find a free frame:

                    • If there is a free frame, use it
                    • If there is no free frame (all memory in use), use a page replacement (find one that is not currently being used and free it) algorithm to select a victim frame
                    • Swap out victim frame to disk. Change the page and frame tables accordingly.

                    3.

                    Read the desired page into the (newly) free frame. Update the page and frame tables.

                    1. Restart the process
                    16
                    Exam weight Low
                    1. Page replacement: if no free frame, pick a victim; use a dirty (modify) bit so only modified pages are written back.
                    Slide 17: Need For Page Replacement
                    Exam weight Low

                      Basic Page Replacement (cont.)

                      • Two page transfers are required by Page replacement. This doubles the page fault service time and increases EAT.
                      • If the page to be replaced has not been modified since it was loaded from disk, there is no need to write it back to the disk.
                      • Use modify (dirty ) bit to reduce overhead of page transfers – used to indicate whether or not a page has been modified.
                      • When the page is first read into memory - dirty bit = 0.
                      • If any word or byte is written into (page has been modified)- dirty bit = 1.
                      • The dirty bit is checked when a page is selected for replacement:
                        • If the bit is set- page has been modified, must write the page to disk.
                        • If the bit is not set, need not write page to disk.
                      18
                      Exam weight Low

                        Page Replacement

                        19
                        Exam weight Low

                          Page-Replacement Algorithms

                          • They are many different Page-Replacement algorithms, but how do we select a particular replacement algorithms?
                          • Want lowest page-fault rate.
                          • Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string.
                          • The more frames that are available, the fewer page faults there will be.
                          20
                          Exam weight Low
                          1. Goal: lowest page-fault rate. Evaluate on a reference string.
                          TipPractice string: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 with 3 frames → FIFO 15 faults, OPT 9, LRU 12.
                          Slide 21: Graph of Page Faults Versus The Number of Frames
                          Exam weight Low

                            First-In-First-Out (FIFO) Algorithm

                            • Choose the oldest page in memory for replacement.
                            • Example: Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
                            • 3 frames (3 pages can be in memory at a time per process). 9 page faults
                            • 4 frames. 10 page faults
                            • Problem: Belady ’ s Anomaly
                              • more frames  less page faults
                            22
                            Exam weight Low
                            1. FIFO: replace the oldest page.
                            Slide 23: Slide 23
                            Exam weight Low
                              Slide 24: FIFO Illustrating Belady s Anomaly ’
                              Exam weight High
                              1. Belady’s anomaly: with FIFO, MORE frames can cause MORE page faults.
                              🎯 How this can come up · 1 question
                              MCQ

                              Belady’s anomaly occurs with:

                              Optimal Algorithm

                              • Replace page that will not be used for longest period of time.
                              • Better than a FIFO algorithm, but difficult to implement.
                              • Used for measuring how well your algorithm performs.
                              • Difficult to implement, as it requires future knowledge of the reference string.
                              25
                              Exam weight High
                              1. Optimal (OPT/MIN): replace the page not used for the longest time in the future. Lowest fault rate but needs the future: used as a benchmark.
                              🎯 How this can come up · 1 question
                              True / False

                              The optimal page-replacement algorithm can be implemented in a real OS.

                              Slide 26: Slide 26
                              Exam weight Low

                                Least Recently Used (LRU) Algorithm

                                • Replace the page that has not been used for the longest period of time.
                                • The optimal page replacement algorithm looking backward in time, rather than forward.
                                • Considered a good replacement algorithm.
                                • Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1. Used in 3 frame memory space What is the total No of page faults?
                                27
                                Exam weight High
                                1. LRU: replace the page not used for the longest time in the past. Implemented with counters or a stack; no Belady’s anomaly.
                                🎯 How this can come up · 1 question
                                Written

                                Reference string 1 2 3 4 1 2 5 1 2 3 4 5 with 3 frames. Count FIFO page faults.

                                Slide 28: Slide 28
                                Exam weight Low

                                  Thrashing

                                  • a process is busy swapping pages in and out than executing
                                  29
                                  Exam weight High
                                  1. Thrashing: the process spends more time paging than executing because it has too few frames; CPU utilization drops.
                                  🎯 How this can come up · 1 question
                                  MCQ

                                  Thrashing is when a process:

                                  Thrashing

                                  30
                                  Exam weight Low