Lec 10: Virtual Memory II

There are many benefits of virtual memory

Swapping

Page Eviction Policies

  • Which page should the OS choose to swap out to disk?

  • A strawman policy: First-in, First-out. See example in Saltzer, Kaashoek (6-349)
    Reference string: 0  1  2  3  0  1  4  0  1  2  3  4
    physical page 1 :*0  0  0 *3  3  3 *4  4  4  4  4  4 
    physical page 2 : - *1  1  1 *0  0  0  0  0 *2  2  2
    physical page 3 : -  - *2  2  2 *1  1  1  1  1 *3  3
    
    Now we add one more physical page,
    Reference string: 0  1  2  3  0  1  4  0  1  2  3  4
    physical page 1 :*0  0  0  0  0  0 *4  4  4  4 *3  3 
    physical page 2 : - *1  1  1  1  1  1 *0  0  0  0 *4
    physical page 3 : -  - *2  2  2  2  2  2 *1  1  1  1
    physical page 4 : -  -  - *3  3  3  3  3  3 *2  2  2
    
    
  • FIFO eviction policy suffers from Belady's anomaly: Increasing physical memory results in increased page faults!
  • What's the optimal policy? Evict the page which will be used in the furthest in the future.
    Reference string: 0  1  2  3  0  1  4  0  1  2  3  4
    physical page 1 :*0  0  0  0  0  0  0  0  0 *2 *3  3 
    physical page 2 : - *1  1  1  1  1  1  1  1  1  1  1
    physical page 3 : -  - *2 *3  3  3 *4  4  4  4  4  4
    
  • Why is it optimal?
  • OPT is not a practical algorithm since we cannot predict future
  • LRU (least-recently used) is a popular approximation
    Reference string: 0  1  2  3  0  1  4  0  1  2  3  4
    physical page 1 :*0  0  0 *3  3  3  3  3 *1  1  1 *4 
    physical page 2 : - *1  1  1 *0  0  0  0  0  0 *3  3
    physical page 3 : -  - *2  2  2 *2 *4  4  4 *2  2  2
    
  • Does LRU or OPT suffer from Belady's anomaly?
  • Keeping track of every memory reference in the kernel software is too expensive. How to approximate it?
  • The clock page-removal algorithm

    Demand paging

    Shared libraries

  • Most programs in the system use (link to) the C library libc, or other common libraries like KDE, X etc.
  • There's no need to store multiple copies of the same library since these libraies mostly consist of code and are rarely modified. But how to ensure applications do not change shared library's code/data?
  • Solution: store one copy of library in memory and map it to applications' address spaces as read-only.

  • What parts of the C library cannot be shared?

    Copy-on-write fork

  • In Lab1, we forked a mini-process by copying only the parent's stack to the child. This does not provide isolation of processes, since the parent and child can easily trash other other's global/heap data.
  • In Lab 3, you are asked to implement fork() again, with correct isolation this time.
  • One way to do a fork is Eager Copying, where we copy the address space immediately.

  • Eager copying is wasteful because
  • How about lazy copying? i.e. we delay copying the content until it is required for the child's execution
  • Will it work?
  • Copy-on-write fork makes the child's address space a copy of the parent's and mark both as read-only. Then kernel continues execution with both parent or child.
  • How to handle page fault?
    page_fault_handler(uintptr va, int cpl, bool write){
      vp = vpage_info(current, va);
      if(vp is copy_on_write){
        copy the page contents into a new page pa
        mark va as pointing to the new page with pgdir_set(current->pgdir, va, pa, PTE_P|PTE_W|...)
      }
    }
    
  • You can play many of the virtual memory tricks at user-level too using proper syscalls