CMPSCI 377: Operating Systems
Homework 4: Memory Management
Solutions


  1. Consider a segmented memory system with memory allocated as shown below.
    Suppose the following actions occur:
    • Process E starts and requests 300 memory units.
    • Process A requests 400 more memory units.
    • Process B exits.
    • Process F starts and requests 800 memory units.
    • Process C exits.
    • Process G starts and requests 900 memory units.

    1. (3 pts.) Describe the contents of memory after each action using the first-fit algorithm.

      E requests 300: E is allocated in 400-700
      A requests 400 more: cannot fit because the entire process is allocated in a single continuous chunk of memory in a segmented memory system. Need to compact memory: move B to 1100-1900, move E to 2400-2800, give A additional addresses 400-800
      B exits: there is a hole between 800-1900
      F requests 800: F is allocated in 800-1600
      C exits: there is a hole between 1600-2400
      G requests 900: no hole that is big enough. Compact memory: move E to 2800-3100, G is allocated in 1600-2500

    2. (3 pts.) Describe the contents of memory after each action using the best-fit algorithm.

      E requests 300: E is allocated in 1600-1900
      A requests 400 more: this 400 is allocated in 400-800
      B exits: there is a hole between 800-1600
      F requests 800: F is allocated in 800-1600
      C exits: there is a hole between 1900-3100
      G requests 900: G is allocated in 1900-2800

    3. (3 pts.) How would worst fit allocate memory?

      E requests 300: E is allocated in 2400-2700
      A requests 400 more: this additional 400 is allocated in 400-800
      B exits: there is a hole between 800-1900
      F requests 800: F is allocated in 800-16000
      C exits: there is a hole between 1600-2400
      G requests 900: no hole big enough. Need to compact: move E to 2800-3100, give 1600-2500 to G.

      For this example, best-fit is best.

  2. Most modern operating systems support copy-on-write. Copy-on-write allows two processes to share a page in memory that contains data that can be modified but for which the processes do not want to see the changes made by each other. If a page is marked as copy-on-write and there is an attempt to write to that page, a trap occurs. The trap handler copies the page, updating the process's page table and re-executes the instruction that caused the trap.

    1. (5 pts.) What will the effect of re-execution be?

      Address translation will now map the virtual address to the new copy of the page and the write operation will now succeed.

    2. (5 pts.) Describe one situation in which you believe copy-on-write would be useful.

      When forking a process, it would be best to mark all pages as copy-on-write and allow the original and forked processes to share pages until one of them attempts to modify the page. This would be much more efficient than actually copying the address space on the fork operation.

  3. Suppose we have virtual memory containing 8 pages with 512 bytes per page and physical memory with 16 page frames.

    1. (2 pts.) How long is a virtual address?

      Virtual address bits: log2(8x512) = 12 bits.

    2. (2 pts.) How long is a physical address?

      Physical address bits: log2(16x512) = 13 bits.

    3. (1 pt.) Given the following page table, what is the physical address corresponding to the virtual address of page 4, byte 241?

      VirtualPage
      PageFrame
      00
      18
      211
      36
      43
      51
      67
      710

      binary physical address = 0 0110 1111 0001 so, physical address = 1777

  4. (10 pts.) Is it easier for processes to share code pages in a pure paging system or in a system that includes segmentation? Explain your answer.

    It is easier for processes to share code pages in a system combining segments and paging because the offset within the segment will be the same for each process that shares the segment. Whole segments can be shared by sharing the segment table.

    Without segmentation, we need to be sure that each chunk of shared pages (like the code) and each chunk of non-shared pages (like the stack) begins on a page boundary, since we cannot share just part of a page. Mapping virtual memory onto pages is done simply by dividing the virtual address space into units that are the size of the hardware page frames. To place page boundaries at the right places, the compiler would need to be know the size of the hardware page frames when generating the virtual addresses used in object code. This is obviously more difficult than generating separate segments for each chunk.

  5. Consider a paging system with the page table stored in memory.

    1. (2 pts.) If a memory reference takes 150 nanoseconds, how long does a paged memory reference take?

      2*150=300 ns

    2. (3 pts.) If we add associative registers (a TLB), what percent of page table references need to hit in the TLB to get an effective access rate of 155 nanoseconds? Assume that a TLB lookup has zero overhead.

      Let p=percent of page table references need to hit in the TLB, then

      155 = p*150 + (1-p)*300
      so,
      p=96.6%