CS 372 Operating Systems

Homework 2: Synchronization & Memory

Due: Thursday October 29, 2009, beginning of class
  1. Synchronization. Making Water. You need two hydrogen atoms (H), and one oxygen (O) to make water. Write solutions that generate water as soon as the atoms are available.  Use monitors.

  2. Here is a code template.  MakeH, and MakeO will get called infinitely often, but all methods are called at unpredictable times.  make_hydrogen and make_oxygen actually "make" the atoms, and make_water actually "makes" the water molecule.  Your code coordinates these processes.  The math is correct in this code sample, but there is not enough synchronization code.  Please write that code.

    public class Water {
          private int freeH;
          private int freeO;
          private static final ReentrantLock lock = new ReentrantLock();
       // You can have additional state if you like
       Water() {
          freeH = freeO = 0;
       }
       private void MakeH() {
          make_hydrogen();
          freeH++;
       }
       private void MakeO() {
          make_oxygen();
          freeO++;
       }
       public void MakeWater() {
          make_water();
          freeO--
          freeH = freeH - 2; 
       }
    }

  3. Monitors. Here is a  long term scheduling problem. A machine comes with a fixed size of memory M. Each process, i, in the system takes up some initial amount of memory space, K less than M. The OS only lets new processes become active in the system if the system will still use less than M amount of memory. When a process terminates, it releases all its memory. While a process is running it can request that the OS give it additional memory (increase K by A, K+A less than M). The OS satisfies this request if it can, otherwise it makes the process release all of its memory until the OS can satisfy the request. Insure your solution does not result in deadlock.  Don't worry about the contiguity of the memory regions---if two chunks of size 2KB and 5KB become available, you can give a process 7KB.

    You need to write three methods: Start(int i, int K), AddMemory(int i, int A) and Terminate(int i). You can assume each process id i is unique. Use an array memory[i] to keep track of the current memory in use by process i. To keep your solution as simple as possible, do not allocate or bounds check it. You can also assume all requests satisfy the constraints above (i.e., your code should not check that the process always requests less than M amount of memory).

  4. Deadlock. What does a resource allocation graph show? What does a cycle in the the resource graph mean?
  5. Deadlock. Consider the following Java program. Can it deadlock? Does it correctly transfer money?  If the program is broken fix it so that it does not deadlock and correctly transfers money.
    public class BankAccounts{
    private int accounts;

    public class void Account () {
    private int total;
    public int account_number;
    ...
    Account(...) {
    ...
    }
    public void transfer(Account withdraw, Account deposit, int amount) {
    synchronized(withdraw) {
    synchronized(deposit) {
    if(amount > 0) {
    withdraw.transaction(-amount);
    deposit.transaction(amount);
    }
    }
    }
    }
    public void transaction(int amount) {
    if (amount > 0) total += amount;
    else if (total < -amount)
    throw new InsufficientFundException()
    else total -= amount;
    }
    }
  6. Paging. Consider a process with a logical address space of 4 pages of 1024 bytes per page, mapped onto a physical memory of 64 frames.
    1. How many bits are there in the logical address?
    2. How many bits are there in the physical address?
    3. Given the following page table map: page 0 is mapped to frame 3, page 1 is mapped to frame 14, page 2 to frame 6, and page 3 to frame 33, what is the physical address of page 2 byte 256?

  7. More Paging. Consider a paging system with a single-level page table stored in memory, and no processor cache.
    1. If a memory reference takes 60 nanoseconds, how long does a paged memory reference take?
    2. If we add associative registers (a TLB), what percent of page table references need to hit in the TLB to get an effective access time of 65 nanoseconds?

  8. Page Replacement Algorithms. Consider the following page reference stream and 3 page frames:
    0 1 2 3 2 4 3 1 1 5 2 4 6 3 3 4 6 3 4 7
    For each algorithm, show the contents of the page frame after each reference, and then compute the total number of page faults, divided in to cold misses and other misses.  A cold miss is the first miss to a page.  It cannot be eliminated by a replacement policy.
    1. MIN (optimal)
    2. FIFO
    3. LRU
    4. Clock with one bit. Show the clock bits states separately.
    5. For 4 page frames, what is the minimum number of misses possible for this stream?