Classic Synchronization Problems

Tom Kelliher, CS 311

Mar. 26, 2012

Administrivia

Announcements

Assignment

From Last Time

Software solutions to the CS problem.

Outline

  1. Dining philosophers.

  2. Second readers/writers problem.

  3. Sleepy barber problem.

Coming Up

Deadlock.

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 2012-03-25
Tom Kelliher