CS360 Midterm -- May 2, 2002

Answer to Question 5

Disaster. Even though I wrote and tested the program, I didn't proofread fully. I meant for the signal statement to be:
   if (i < 4) pthread_cond_signal(c+i+1);
Two typos. Such is life. Sorry.

But I didn't, so we have to deal with the program at hand, which is an odd one. All threads can print out their 0's at any time. However, for the other digits, if a thread wants to print digit x, and there have not been three threads that have printed digit (x-1), then the thread will block. It will eventually unblock, though, because the threads that print the third, fourth and fifth digit (x-1) will be able to print digit x, and will wake up the blocking threads. There are at most two blocked threads, and these will always be awakened, so we have no deadlock problems.

This activity will go on for digits 1,2,3 and 4.

So the answers to the assertions are as follows:

Just for fun, I've executed it a bunch of times. Here's the output -- see how the assertions do.

Part 2. There is never deadlock here. Since my assertion was wrong, I didn't count anything for part 2. Again, my apologies for causing you stress.


Grading

0.75 points for each assertion. Nothing for part 2.