Classic Synchronization Problems

Tom Kelliher, CS 318

Feb. 23, 1998

Announcements:

From last time:

Outline:

Assignment:

Lamport's Bakery Algorithm

See last set of notes.

Classic Synchronization Problems

Dining Philosophers

Five philosophers, five chopsticks, five chairs, a table, a bowl of noodles.

Some ``solutions:''

while (1)
{
   think;
   pick up left chopstick;
   pick up right chopstick;
   eat;
   put down left chopstick;
   put down right chopstick;
}

while (1)
{
   think;
   while (haven't obtained both chopsticks)
   {
      pick up left chopstick;
      if (right chopstick is available)
         pick up right chopstick;
      else
         put down left chopstick;
   }
   eat;
   put down left chopstick;
   put down right chopstick;
}

while (1)
{
   think;
   while (haven't obtained both chopsticks)
   {
      if (both chopsticks available)
      {
         pick up left chopstick;
         pick up right chopstick;
      }
   }
   eat;
   put down left chopstick;
   put down right chopstick;
}

The real solution: put a total order on the chopsticks.

while (1)
{
   think;
   pick up lower-numbered chopstick;
   pick up higher-numbered chopstick;
   eat;
   put down lower-numbered chopstick;
   put down higher-numbered chopstick;
}

Second Readers-Writers Problem

Writers have priority.

The Sleeping Barber Problem

A barbershop consists of a waiting room with N chairs, and the barber room containing the barber chair. If there are no customers to be served the barber goes to sleep. If a customer enters the barbershop and all chairs are busy, then the customer leaves the shop. If the barber is busy, then the customer sits in one of the available free chairs. If the barber is asleep, the customer wakes the barber up. Write a program to coordinate the barber and the customers.

What needs to be modeled?

  1. Barber: awake/asleep.

  2. The barber chair.

  3. The customer chairs.

  4. Waiting customers.

  5. Access to shared data.



Thomas P. Kelliher
Sun Feb 22 18:32:23 EST 1998
Tom Kelliher