CS 26
October 4, 1996
Yes. Semaphore wait() corresponds to a mailbox receive(),
assuming that the
receive() blocks on an empty mailbox. Semaphore
signal() corresponds to a mailbox send() of an empty message.
To initialize a semaphore to n, send n empty messages to the
mailbox.
Spin-loops waste CPU cycles unless the I/O wait is shorter than the overhead involved in interrupt handling (not usually the case). Interrupt-driven I/O permits the CPU to be doing useful work while the I/O takes place.
A critical section is any section of a process' code in which shared variables are accessed. The three characteristics are:
Calculate the average turnaround time for each of the three scheduling disciplines listed:
global: int flag[2] = { 0, 0 }; mutexbegin: do flag[i] = 1 - flag[1-i]; while (flag[1-i] != 0); mutexend: flag[i] = 0;Does this ``solution'' satisfy the three requirements of a solution to the critical section problem? Justify your answer.
No, because mutual exclusion can be violated. Suppose both processes are trying to enter critical sections. Each process can read the other process' flag (0) and set their own to 1. This causes them each to repeat the do-while. On the next iteration through the loop, they can each again read the other process' flag (1) and reset their own to 0. The do-while condition is now violated and both processes enter their respective critical sections, violating mutual exclusion.
The big hint on this problem was calling the algorithm a ``solution.''