Midterm Review

February 7 2007

Lecture Review

  1. Scheduling and Deadlock
  2. Midterm Review

Midterm Review

Anything you guys want to go over in particular? We can talk about homework problems or the midterm review posted on the class webpage if you like.

More Problems

  1. (leftover from lecture) Suppose we have the following code
    int x = 0;
    
    void AddToX() {
      for(i=0; i < 100; i++) {
        x++;
      }
    }
    
    void SubFromX() {
      for(j=0; j < 100; j++) {
        x--;
      }
    }
    

    and we have one thread running AddToX() while another thread runs SubFromX(). What is the range of possible final values for x?

  2. (leftover from lecture) Event Synchronization: Event::Wait() blocks if and only if the Event is unsignaled. Event::Signal() marks the Event as signaled, and wakes up blocked threads. Once signaled, an Event remains signaled. Define Event::Wait() and Event::Signal() with a monitor, then with semaphores.
  3. Restaurant: We have a restaurant with N chairs and no tables. Customers come and go, but they arrive in parties of M customers. A party must always be seated at the same time, so if there are 4 chairs available, and a party of 5 arrives, the party must wait. On the other hand, if a party of 3 arrives, and 4 chairs are available, they can pull together 3 chairs and sit.

    After the party is seated, they eat for a while, and then they leave, allowing more customers to enter. Clearly, there can never be more than N seated customers at any time, because the restaurant only has N chairs.

    Each party of M customers is represented by a thread that executes the following code:

    SitDown(M)
    // eat
    StandUp(M)
    

    SitDown(M) indicates that a party of size M wants to sit down. A party will start eating as soon as SitDown returns, so SitDown must block until M seats are available. StandUp(M) indicates that a party of size M wants to stand up, freeing M chairs for waiting customers.

    Define SitDown and StandUp first using monitors, then using semaphores.

    Does your solution guarantee that parties of the same size are served in FIFO order? That is, if a party of 2 arrives, then later another party of 2 arrives, can you guarantee that the first party of 2 will always sit first? If not, how would you enforce this policy?