LMU ☀️ CMSI 673
CONCURRENT AND DISTRIBUTED PROGRAMMING
Quiz 1

The test is open-everything with the sole limitation that you neither solicit nor give help while the exam is in progress.

  1. The Java Language Specification states, in Section 17.8.1, entitled "Wait", the following: "If thread t is interrupted, an InterruptedException is thrown and t's interruption status is set to false." In Section 17.8.3 entitled "Interruptions" it states: "Let t be the thread invoking u.interrupt, for some thread u, where t and u may be the same. This action causes u's interruption status to be set to true." One says the status is set to false while the other says its set to true. Explain why this is not inconsistent.
  2. Here's a piece of a solar system simulation. It's a class for planets that revolve around a sun. Each planet is a thread whose run() method continuously updates its current position along a circular path of radius r. A separate thread runs along periodically drawing all of the planets by calling getPosition() for each. There's something wrong here. Explain what could happen here and also show how to fix the problem.
        class Planet extends Thread {
            private double r, u, x, y, du;
            public Planet(double r, double du) {this.r=r; this.du=du; x=r; y=u=0;}
            public void run() {while (true) {u+=du; x=r*cos(u); y=r*sin(u);}}
            public Point getPosition() {return new Point(x, y);}
        }
    
  3. Prove or disprove that a Dining Philosophers solution that permits only four philosophers to be seated at any given time is deadlock-free. Assume philosophers pick up their right chopstick first and then their left, blocking indefinitely to obtain each one.
  4. What's wrong with the following attempt to enforce mutual exclusion, besides the fact that it polls? After all, it doesn't deadlock and starvation is impossible. Here "turn" is a global variable initialized to 1.
        thread1:                           thread2:
          while (true) {                     while (true) {
            NON_CRITICAL_SECTION_1             NON_CRITICAL_SECTION_2
            while (turn != 1);                 while (turn != 2);
            CRITICAL_SECTION_1                 CRITICAL_SECTION_2
            turn = 2;                          turn = 1;
          }                                  }
    
  5. Show how to implement a trivial count down latch with a Semaphore in Java. That is, fill in the body of this class:
    public class MySemaphoreBasedCountDown {
        Semaphore s;
        ...
        public MySemaphoreBasedCountDown(int count) {...}
        public void await() throws InterruptedException {...}
        public void countDown() {...}
    }
    

    Make objects of your class work like Java CountDownLatches, meaning you can only use them once.