This quiz checks your understanding of the lectures and its reading assignments during the past week. You may reference the textbook, lecture slides, and your notes. Online sites (e.g., Wikipedia) may also be consulted but you must cite them in your writeup.
Each question is worth three points. Please strive for clarity and brevity in your answers. Essay questions should require no more than one or two short paragraphs.
Submit your answers by clicking the "submit" link on the lecture schedule page. Only online submissions will be accepted. Please submit plain ASCII text files.
int CAS(int *a, int old, int new) { if (*a != old) return 0; *a = new; return 1; }In the lecture, we talked about test-and-set (TAS) instruction, which has the following pseudo-code:
int TAS(int *a) { if (*a != 0) return 0; *a = 1; return 1; }Implement an atomic TAS function using the CAS function.
int TAS(int *a) { return CAS(a, 0, 1); }
Please tell whether this modification is correct or incorrect. If you think it is correct, tell us the advantages and disadvantages, comparing with the solution in the lecture. If you think it is incorrect, tell us what can go wrong by showing an example.
Suggested solution: The modification is incorrect. Consider the case where the buffer is full. Suppose a Producer runs. It holds mutex and wait on the emptyCount semaphore. A Consumer now runs. It will wait on P(mutex).
Suggested solution: Hoare proposed to run the signaled thread immediately, suspending the current thread. Hansen proposed that Signal must be the last statement of a monitor procedure. Mesa monitor allows the current thread to continue its execution after executing Signal. Mesa monitor allows Signal to wakeup more than one thread. Programming with Mesa monitor requires checking the condition again after returning from Wait, because the condition may no longer be true.