COMP 3511: Lecture 20

Date: 2024-11-12 15:01:29

Reviewed:

Topic / Chapter:

summary

❓Questions
  • ❓ is page table copied upon clone anyway? can't it be shared?
    • 👨‍🏫 as it's part of PCB, it must be copied.
    • in principle, it can be shared

Notes

Virtual Memory
  • Demand paging

    • instead of loading the entire process
      • bring a "page" into memory only when the page: needed / referenced
        • less IO needed
        • less memory needed
        • faster response
        • more users: can be ran
    • similar to: paging system w/ swapping
      • 01_paging_system
    • page: needed if reference to it is made
      • invalid reference (illegal address): abort
      • not in memory: bring to memory
    • lazy swapper: never swaps a page into memory, unless page is needed
      • pager: swapper dealing with pages (unit changed into page, instead of block)
  • VM concepts

    • pager: brings only those "needed"
    • determine the pages to be brought by: modifying MMU functionality
    • if pages needed: already memory-resident
      • no difference from non demand-paging MMU
    • if pages: not in memory
      • detect & load page into memory from a secondary storage device
        • w/o changing program behavior or code
  • Valid-invalid bit

    • w/ each table entry: exists associated valid-invalid bit
      • v: in-memory (memory resident)
      • i: not-in-memory
    • initially: all set to i
      • during address translation: if valid=invalid bit is i:
        • results in page fault
    • 02_overall_view
  • Page fault

    • if: any reference of page is made, first reference to the page: traps application into page fault
    • it is handled by:
      1. OS: look at the corresponding PTE
        • invalid reference: abort
        • else: just not in memory
      2. get empty frame, if any
        • OS: maintains free-frame list
      3. swap the page (from secondary) into frame via scheduled disk operation
      4. update the corresponding PTE
      5. reset page table to indicate: page is now in memory
        • set validation bit: v
      6. restart the instruction caused the page fault
    • 03_page_fault_handling
  • Aspects of demand paging

    • extreme cse: starts process w/ no pages in memory
      • OS: sets instruction pointer to first instruction of process, non-memory resident
        • leads to page fault
      • same for all other process page, on first access
      • aka pure demand paging
    • single instruction: could access multiple pages: resulting multiple faults
      • e.g. reading two numbers from memory and storing result in memory again
      • page faults: decreases as process runs
        • due to locality
    • to minimize initial / potential high page fault: OS pre-pace some pages into memory
      • before process execute
    • hardware support needed for demand paging
      • page table w/ valid / invalid bit as indication
      • secondary memory (swap device w/ swap space) for page in & out
      • instruction restart
  • Free frame list

    • when a page fault occurs: OS must bring desired page into memory
    • most OS: maintains a free-frame list: pool
      • kernel data structure: only accessible for OS
    • OS: typically allocate free-frames w/ zero-fill-on-demand technique
      • content of the frame: zeroed-out before re-allocated
        • i.e. writing zeros before providing it to a process
        • not doing so: leads to potential security issues
    • when system starts up: all available memory is on free-frame list
  • Performance of demand paging

    • stages in demand paging: handle page faults
      1. trap to the OS
      2. save user registers & process state (main delay)
      3. determine: whether interrupt was a page fault
      4. check if page reference was legal / determine location of page
      5. issue: a read from disk to free frame
        1. wait in que for device until read is done
        2. wait for device seek / latency time
        3. begin transfer of page to free frame
      6. allocate CPU cores to other processes while waiting
      7. receive: interrupt from dis IO (IO: completed!)
      8. save registers & process state for the other processes
      9. determine: interrupt was from the dist
      10. updateL page table & other tables to show page is not in memory
      11. wait for CPU to be allocated to process again
      12. restore: user registers, process state, new page table, resume the interrupted instruction
    • three major tasks in page-fault service time
      1. service the interrupt: requires careful coding for efficiency
      2. read the page: accessing the hard disk (lots of time)
      3. restart the process: small amount of time
    • page switch: probability close to 8 ms for typical hard disk
    • page fault rate:
      • : no page faults; : every reference is a fault
    • effective access time: EAT
  • Computation example

    • memory access time: 200 ns
    • average page-fault service time: 8 ms
    • 👨‍🏫 roughly 3-4 magnitude difference
    • if one access occurs once in a thousand
      • , 40 TIMES SLOWER!!
    • if we want performance degradation: less than 10 percent
      • less than one fault in every 400,000 memory accesses!
    • VERY VERY fortunately: memory locality usually satisfies this...
Page Replacement
  • Copy on write

    • traditionally: fork() works by creating a copy of parent's address space
    • copy-on-write (COW): allows both parent & child to initially share the same page in memory
      • shared pages: marked as copy-on-write pages
        • if either process writes to shared page: copy of shared page must be created
        • original, non-modified page: not left with no sharing
    • COW: allows efficient process creation
      • only modifies pages getting copied / duplicated
      • results in rapid process creation
      • w/ minimizing no. of new pages to by allocated to newly created process
        • e.g. most child process invoke exec, so no pages (except PCB) will be copied for them
    • 04_before_after_page_modification
  • What if: no free frame?

    • used up by process pages. kernel, IO buffer, etc.
    • how much memory to each memory: frame-allocation algorithm
    • page replacement: find some page in memory, and page it out
      • algorithms: terminate process? swap out the entire process image? replace the page?
      • performance: wish to result in minimum no. of page faults
      • must be transparent to process / program execution
    • loading same page multiple time: inevitable
  • Page replacement

    • prevent: over-allocation of memory by modifying page-fault service routine
      • s.t. it include page replacement
    • use modify (dirty) bit to reduce: overhead of page transfers
      • only modified pages: written back to dist
    • page replacement: completes separation between logical memory & physical memory
      • large virtual memory: can be supported on a smaller physical memory
    • 05_need_for_replacement
      • we must replace some page to load M
  • Basic page replacement

    1. find the location of desired page on dist
    2. find: a free frame
      • if it exists: use it
      • if there is no: use page replace algorithm to select a victim frame
        • write victim frame back to dist if it is "dirty"
    3. bring the desired page into (cleared) free frame; update the page table
    4. continue the process by restarting the instruction
    • potentially: two page transfers for a page fault
      • significant increase EAT
    • 06_page_replacement_diagram
  • Page and frame replacement algorithms

    • page-replacement algorithms
      • decide: which frame to replace
      • objective: minimize the page-fault rate
    • frame-allocation algorithm
      • determine: how many frames allocate to each process
        • decided by how process access the memory (locality)
  • Page and frame replacement algorithms

    • evaluate algorithms by: running particular string of memory references reference string
      • then: compute no. of page faults on that string
    • for a given page size: only consider the page number
      • as: consecutive reference to the same page never causes a page fault
      • thus: having the consecutive page number in string: doesn't make sense
    • e.g. 7,0,1,2,0,3,0,4,2,3,0,2,2,1,2,0,1,7,0,1
    • no. of page faults vs. no. of frame
      • 07_page_fault_frame_no
  • First-In First-Out (FIFO)

    • reference string: 7,0,1,2,0,3,0,4,2,3,0,2,2,1,2,0,1,7,0,1
    • 3 frames, then 15 page faults
      • 08_ref_str_diagram
    • actual result: would differ by reference string
    • adding more frame: can cause more page faults (strange)
      • Belady's Anomaly
      • e.g. 1,2,3,4,1,2,5,1,2,3,4,5
        • results in 9 faults in 3 frames, 10 in 4 frames
      • 09_belady_anomaly
    • tracking ages of pages: easy
      • use FIFO queue, or some counter, etc.
  • Optimal algorithm

    • theoretically optimal, if we know the future!
    • replace a page: not used for the longest period in the future
      • ideally: choose a page that will not be used at all in the future
    • used for: measuring how well your algorithm performs
    • 10_optimal_algorithm
    • 9 faults: optimal for 3 frames