CS360 Final Exam - May 6, 2013 - James S. Plank - Page Two


Question 3

You are writing a program that has two threads, which we will call threads A and B. Both threads have access to a shared data structure D of type (Data *). Here is the basic outline of what the threads do:

Thread A:

while (1) {
  do_stuff_that_doesnt_involve_D();
  safe_modify_data(D, I)
}
Thread B:

while (1) {
  do_stuff_that_messes_with_D(D);
  block_myself_while_thread_A_modifies_D(I).
}

The variable I is of type (Info *), defined as follows:

 typedef struct {
  pthread_mutex_t lock;
  pthread_cond_t a_cond;
  pthread_cond_t b_cond;
  int b_blocked;
} Info;

It gets initialized before any threads are created with the following code:

 I = (Info *) malloc(sizeof(Info));
  pthread_mutex_init(&(I->lock), NULL);
  pthread_cond_init(&(I->a_cond), NULL);
  pthread_cond_init(&(I->b_cond), NULL);
  I->b_blocked = 0;

Your job is going to be the following. Write me a paragraph that says how the threads are going to use I so that they can accomplish their tasks.

Then write safe_modify_data() and block_myself_while_thread_A_modifies_D() so that they do the following: