CS560 Midterm -- March 13, 2003

Answer all questions.


Question 1

Multiple choice: Choose the best answer to each.

A: The long-term scheduler:

B: Which of the following is not a necessary condition for deadlock:

C: Which of the following statements best compares timestamp-based transactions and two-phase commit transactions:

D: In the dining philosophers, what is the problem with a solution where odd philosophers pick up their right chopstick first, and even philosopher pick up their left chopstick first. Choose all that apply:

E: Which of the following is the definition of a safe state:

F: Why do we not want a time quantum to be too small?

G: Why do we not want a time quantum to be too big?

H: Which of the following statements is false?

I: In the kthreads implementation below, why does kt_exit() call longjmp()?

void kt_exit()
{
        JRB tmp;

        InitKThreadSystem();
   
        if (ktRunning == ktOriginal) {
          ktRunning->state = DEAD;
          tmp = jrb_find_int(ktActive, ktRunning->tid);
          jrb_delete_node(tmp);
          tmp = jrb_find_int(ktBlocked, ktRunning->tid);
          if (tmp != NULL) WakeKThread((K_t)tmp->val.v);
          KtSched();
        } else {
          longjmp(ktRunning->exitbuf,1);
        }
}


Question 2

Suppose at time x, the user presses return on his keyboard. At time x + 1 (seconds), a user process executes a read(0, &c, 1) call, which returns at time x + 2. Below are 12 events that can occur in the operating system. Choose the events that must occur in this scenario, and put them in the correct order that they will occur. Note, not all events will occur at all, and some events may occur more than once. You can only choose events from this list, and you may not make up your own.


Question 3

Here are the first few lines of kt_sched():
void KtSched()
{
        declarations...

        if(setjmp(ktRunning->jmpbuf) != 0)
        {
                FreeFinishedThreads();
                return;
        }

        ...
Explain exactly what is going on in these few lines of code.

Question 4

Suppose we execute the following code:

#define NTHREADS (10)

pthread_mutex_t *locks[NTHREADS];

void *thread(void *arg)
{
  int id, *ip;

  ip = (int *) arg;
  id = *ip;

  while(1) {
    pthread_mutex_lock(locks[(id+NTHREADS-1)%NTHREADS]);
    pthread_mutex_lock(locks[id]);
    pthread_mutex_lock(locks[(id+1)%NTHREADS]);
    sleep(random%10+1);
    pthread_mutex_unlock(locks[(id+NTHREADS-1)%NTHREADS]);
    pthread_mutex_unlock(locks[id]);
    pthread_mutex_unlock(locks[(id+1)%NTHREADS]);
  }
}

main()
{
  int i;
  pthread_t t[NTHREADS];
  pthread_attr_t attrs[NTHREADS];
  int ids[NTHREADS];

  for (i = 0; i < NTHREADS; i++) {
    locks[i] = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
    pthread_mutex_init(locks[i]);
  }

  for (i = 0; i < NTHREADS; i++) {
    ids[i] = i;
    pthread_attr_init(attr+i);
    pthread_attr_setscope(attr+i, PTHREAD_SCOPE_SYSTEM);
    pthread_create(t+i, attr+i, thread, id+i);
  }
  pthread_exit(NULL);
}

Part 1: Prove that this code can deadlock. By prove, I mean show that the book's definition of deadlock holds.

Part 2: Modify the contents of the while loop so that no deadlock can occur because one of the necessary conditions of deadlock cannot be met. You may add variables if you like -- if so, state how they are initialized. Now prove that your code cannot deadlock.

Part 3: Modify the contents of the while loop so that no deadlock can occur because a different one of the necessary conditions of deadlock cannot be met. You may add variables if you like -- if so, state how they are initialized. Now prove that your code cannot deadlock.


Extra Credit (one point -- don't waste your time on this unless you are done early)

You are declaring a bridge contract with spades as trumps. Your left-hand opponent leads a heart. Assuming trumps do not split 4-0 and your opponents do not misdefend, how do you play the hand to maximize your chances of making 11 tricks? If you duck the first round of hearts, they will lead another round.
          Dummy
          S T8
          H A62
          D JT52
          C KT72

          You
          S AKQ9742
          H 73
          D K763
          C -