Process Synchronization or Coordination Seeks To Make
Process Synchronization or Coordination Seeks To Make
Process Synchronization or Coordination Seeks To Make
Solaris preemptive
IRIX preemptive
“Classic” Solutions
• The critical-section problem dates back to…1965!
Peterson’s Solution
• G. L. Peterson’s algorithm provably possesses all three
critical-section solution requirements
/* Entry section for process i. */
flag[i] = true; // Process i wants in...
turn = j; // ...but lets process j go first.
while (flag[j] && (turn == j)); // Wait until process j isn’t interested.
/* Critical section. */
Alternatively, the turn variable may be called
/* Exit section. */
victim or loser, and is set to i instead of j, with the
flag[i] = false;
loop checking for victim == i …equivalent, right?
/* Remainder section */
Atomic Instructions
• Key idea is to accomplish certain operations atomically
— as a single, uninterruptible unit of work
• Main components:
wait(S) {
while (s <= 0);
S--;
A shared integer variable S — the semaphore
}
An atomic wait() operation (originally P() in Dutch) signal(S) {
S++;
An atomic signal() operation (originally V() in Dutch)
}
Upon x.signal(), the signaler may either suspend itself immediately (“signal-and-wait”), or
keep going until the monitor function ends (“signal-and-continue”) — current monitor
implementations favor signal-and-continue
The monitor ensures that only one process at a time can be running its declared
operations (i.e., mutual exclusion)
Processes use x.wait() and x.signal() to synchronize activities — for example, in a monitor-
based dining philosophers solution, if no chopsticks are available for philosopher/process i
(meaning i’s seatmates are eating), then that process must wait() until those seatmates put
down their chopsticks and issue a signal()
Monitor Implementation
• Semaphores are frequently used to implement
monitors; monitor-capable programming languages try
to make the implementation transparent to the coder
For example, in Java, the synchronized keyword actually indicates a segment of code that
must belong to some object’s monitor