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:
- Assertion 1: Always True. Any thread trying to
print a '1' before three zeros are printed will block. Therefore
all strings start with at least three zeros.
- Assertion 2: Always True. See above.
- Assertion 3: True part of the time. It's certainly
possible that each thread writes its 0 before any thread writes
its one. However, it's also possible that three threads write zeros
and then one write a one.
- Assertion 4: True part of the time.. See above.
- Assertion 5: Always True. Each thread ends by printing
a four.
- Assertion 6:
True part of the time. It's possible for
one thread to start after all the others are finished. Actually, it's
possible for two threads to start after all the others are finished.
Therefore, it's possible for the string 01234 to end the string. That's
just one four. However, it is possible for the threads to all take
turns strictly, resulting in the string 0000011111222223333344444.
So it is possible for the string to end with any number of fours, from
one to five.
- Assertion 7:
True part of the time. See above.
- Assertion 8: Always false.
For the string to end with 33434, there must be three 4's written
before the third 3. That cannot happen.
- Assertion 9:
True part of the time. Perfectly legal.
- Assertion 10: Always true -- straight from the description
above.
been printed before it.
- Assertion 11: Always false. Again, three 2's must be
printed before the first three.
- Assertion 12:
True part of the time. See assertion 6 above.
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.