Dining Philosophers Algorithm: Preventing Deadlock and Starvation, Lecture notes of Advanced Algorithms

The Dining Philosophers problem, where five philosophers must eat spaghetti using five forks without causing deadlock or starvation. several attempts to solve the problem and introduces the concept of binary semaphores (mutex) to protect obtaining forks. It also suggests an alternative solution with left-handed philosophers.

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

danmarino
danmarino 🇺🇸

4.2

(11)

267 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Dining Philosophers
Five philosophers sit around a table
with five forks and spaghetti to eat.
Philosophers think for a while and they
want to eat, only spaghetti, for a while.
To eat a philosopher requires two forks,
one from the left and one from right.
Assume a philo. can only pick up one
fork at a time.
After eating, forks are placed down and
philo. goes back to thinking.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Dining Philosophers Algorithm: Preventing Deadlock and Starvation and more Lecture notes Advanced Algorithms in PDF only on Docsity!

Dining Philosophers

Five philosophers sit around a table with five forks and spaghetti to eat. Philosophers think for a while and they want to eat, only spaghetti, for a while. To eat a philosopher requires two forks, one from the left and one from right. Assume a philo. can only pick up one fork at a time. After eating, forks are placed down and philo. goes back to thinking.

Dining Philosophers

Devise an algorithm that will allow philosophers to eat. Must satisfy mutual exclusion

  • no two philos can use the same fork at the same time. Avoid deadlock and starvation!

Dining Philosophers

One solution is to protect obtaining forks with a binary semaphore (mutex)

  • but only one philo can eat at a time rather than two. Following code uses one semaphore per philo and states of eating, thinking, and hungry. A philo can advance to eating if neither neighbor is eating.

Dining Philosophers

#define N 5 /* number of philos / #define LEFT (i+N-1)%N / # of i's left / #define RIGHT (i+1)%N / # of i's right / #define THINKING 0 #define HUNGRY 1 #define EATING 2 int state[N]; / keep track of state / semaphore mutex = 1; semaphore s[N]; / semaphore per philo */

Dining Philosophers

void take_forks(int i) { wait(mutex); state[i] = HUNGRY; test(i); // try getting 2 forks signal(mutex); if state[i] != EATING wait(s[i]); // block if no forks acquired }

Dining Philosophers

void test (int i) { if ( state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING ) { state[i] = EATING; signal(s[i]); } }

Dining Philosophers

Another solution is to have some philos be left handed first. Others:

  • get more forks, learn to eat with one fork. (changes the problem)
  • only let four at a time to the table

Readers/Writers

Models access to a database. Some processes want to read the DB others write it. Many readers can read at the same time. But when writing mutual exclusion is needed. Following code could shut out writers if enough readers.

Readers/Writers

void reader () { while (TRUE) { wait(mutex); rc = rc + 1; if ( rc == 1 ) wait(db); signal(mutex); read_data_base(); wait(mutex); rc = rc – 1; if ( rc == 0 ) signal(db); signal(mutex); use_data_read(); } }

Sleeping Barber

Barber shop with one barber, one barber chair and N chairs to wait in. When no customers the barber goes to sleep in barber chair and must be woken when a customer comes in. When barber is cutting hair new customers take empty seats to wait, or leave if no vacancy. Program barber and customers so no race condition exists.

Sleeping Barber

#define CHAIRS 5 /* # of chairs */ semaphore customers = 0; semaphore barbers = 0; semaphore mutex = 1; int waiting = 0;

Sleeping Barber

void barber () { while (TRUE) { wait(customers); wait(mutex); waiting = waiting – 1; signal(barbers); signal(mutex); cut_hair(); } }