CSE 120 Spring 2002 Homework 2

Solutions

Problem 1

Consider a problem in which there is a producer p and two consumers c1 and c2. The producer produces pairs of values <a, b>. Consumer c1 consumes the a values of these pairs and c2 consumes the b values of these pairs.

More precisely, let P be the sequence of values produced by p, let A be the sequence of values consumed by c1 and let B be the sequence of values consumed by c2. Let Pa and P.b be the sequence of a and of b values in P respectively. Then, for some value of max (which is the number of buffers),

Write a Hoare-style monitor for this problem. It should have three entry methods: void Put(int a, b) that p would use to produce values, int GetA(void) that c1 would use to consume values, and int GetB(void) that c2 would use to consume values.
monitor Split {
  private:
    int in = 0, outA = 0, outB = 0;
    int inA = 0, inB = 0;
    int max;
    int *av, *bv;
    // Invariant 0 <= inA <= max && 0 <= inB <= max
    condition canPut;  // inA < max && inB < max
    condition canGetA; // inA > 0
    condition canGetB; // inB > 0
  public:
    Split(int i) { max = i; av = new int[max]; bv = new int[max]; };

    entry void Put(int a, int b) {
      if (inA == max || inB == max) canPut.wait();
      av[in] = a;
      bv[in] = b;
      in = (in + 1) % max;
      inA++;
      canGetA.signal();
      inB++;
      canGetB.signal();
      };

    entry int GetA(void) {
      int v;
      if (inA == 0) canGetA.wait();
      v = av[outA];
      outA = (outA + 1) % max;
      inA--;
      if (inB < max) canPut.signal();
      return v;
      };

    entry int GetB(void) {
      int v;
      if (inB == 0) canGetB.wait();
      v = bv[outB];
      outB = (outB + 1) % max;
      inB--;
      if (inA < max) canPut.signal();
      return v;
      };
  };


Problem 2

Give a semaphore-based implementation of Java monitors. You should give the code that precedes and follows the invocation of an entry (i.e., synchronized) method, and give the semaphore code for wait, notify, and notifyAll.

This solution uses an urgent queue for the threads that have been notified (you could instead have them wait on the monitor lock). Note that the value ccount and ucount are updated only by the thread that (implicitly) has the monitor lock. It is import to do this, since otherwise there could be two threads that simultaneously update the variable. This would be a problem since increment is not generally an atomic operation.

  1. Instantiate the following structure for each monitor:

  2. struct mon {
      semaphore lock = 1;
      semaphore csem = 0, usem = 0;
      int ccount = 0, ucount = 0;
      };
     

  3. Wrap each invocation of an entry procedure as follows (where m is the struct for the monitor):

  4. P(m.lock);
    entry procedure invocation
    if (m.ucount == 0) V(m.lock);  // release monitor lock
    else V(m.usem);  // pass monitor lock to urgent process
     

  5. Implement wait() as follows:

  6. m.ccount++;
    if (m.ucount == 0) V(m.lock);  // release monitor lock
    else V(m.usem);  // pass monitor lock to urgent process
    P(m.csem);
    P(m.usem);  // reacquire monitor lock
    m.ucount--;
     

  7. Implement notify() as follows:

  8. if (m.ccount > 0) {
      m.ccount--;
      m.ucount++;
      V(m.csem);
     

  9. Implement notifyAll() as follows:

  10. while (m.ccount > 0) {
      m.ccount--;
      m.ucount++;
      V(m.csem);
      }


Problem 3

Annabelle, Bertrand, Chloe and Dag are working on their term papers in CSE 120, which is a 10,000 word essay on My All-Time Favorite Race Conditions. To help them work on their papers, they have one dictionary, two copies of Roget's Thesaurus, and two coffee cups. Consider the following state:
  1. Is the system deadlocked in this state? Explain using a resouce allocation graph.

  2. The resource allocation graph, shown below, can be fully reduced. Betrand is not blocked. Erasing the edges incident on Bertrand unblocks Chloe and Dag. Erasing their edges unblocks Annabelle.

  3. Is this state reachable if the four people allocated and released their resources using the Banker's algorithm? Explain.

  4. Yes. The resource allocation graph is also the maximum claims graph. Since it can be fully reduced, the state is attainable using the Banker's algorithm.


last edited 6 May 2002 by kam