Course Material
Slides used in class is available in the
common directory
with filename
07-Cpp-TM.pdf.
Sample programs are in
07-Cpp-TM.tar.gz
for Windows.
The set of semaphores slides is
08-Semaphores.pdf.
Study
Multithreaded Programming with
ThreadMentor: A Tutorial
If you wish to print
these slides, print them double-sided and print as many slides as
possible on the same page. Let us save a tree!
Programming Material
Do
Programming Assignment III.
Homework Assignment
Answer the following questions:
Semaphore S = 1;
Process i (i = 1, 2, 3, ..., n >= 2)
while (1) {
...
Wait(S);
// in critical section
Signal(S);
...
}
Process 1 Process 2 Process 3
while (1) { while (1) { while (1) {
... ... ...
cout << "1"; cout << "2"; cout << "3";
... ... ...
} } }
Add semaphores and
cout if necessary to the above processes so that
the three processes will print out the following sequence
1 2 3 2 1 2 3 2 1 2 3 2 1 ...
The following solution, due to Morris (1979), uses three semaphores. Semaphores FirstBarrier and SecondBarrier are used to setup two barriers, and semaphore Lock is used to protect counter FirstCount for mutual exclusion purpose. Two counters are used: FirstCount counts the number of processes ready to cross the first barrier represented by semaphore FirstBarrier, and SecondCount counts the number of processes having already crossed the first barrier but not barrier SecondBarrier.
Semaphore: FirstBarrier = 1,
SecondBarrier = 0,
Lock = 1;
int FirstCount = 0,
SecondCount = 0;
The following shows the enter and exit protocol:
// Enter Protocol
Wait(Lock); // I am ready and prepared to cross the 1st barrier
FirstCount++; // increase the counter
Signal(Lock);
Wait(FirstBarrier); // wait on the 1st barrier
SecondCount++; // I have successfully crossed the 1st barrier
Wait(Lock); // Have to lock the counter to decrease
FirstCount--; // Now I am about to cross the 2nd barrier
if (FirstCount == 0) { // if I was the only one crossing the 1st barrier
Signal(Lock); // then release the lock
Signal(SecondBarrier); // and signal the 2nd barrier so that I could cross
}
else { // if I am not the only one, I have to wait; but
Signal(Lock); // I have to release the lock, and
Signal(FirstBarrier); // also open the 1st barrier
}
Wait(SecondBarrier); // wait on the second barrier
SecondCount--; // I am no more waiting to cross the 2nd barrier
// in critical section
// Exit protocol
if (SecondCount == 0) // if there is no one wait on the 2nd barrier
Signal(FirstBarrier); // open the 1st barrier
else // otherwise,
Signal(SecondBarrier); // open the 2nd barrier
Try to understand the above protocol, and convince yourself that mutual exclusion and starvation freedom are satisfied. How about the progress and bounded waiting conditions?
| We do not collect your practice work; but, similar problems will appear in quizzes and exams in the future. Note that I will not make any announcement in class for these short quizzes. In other word, short quizzes may take place at any time as long as I see it is appropriate. |