Philosophers

Eat, sleep, spaghetti, repeat

The dining philosophers in C: one thread per diner, one lock per fork, and one last thread that does nothing but watch, able to catch a death to the millisecond. The two classic traps: deadlock, where everyone waits on their neighbour fork, and starvation.

Area
  • 42
  • system
  • threads
Stack
C, Threads
Grade
100%
Date
2022

The problem

Philosophers sit around a table with one fork between each pair of neighbours. Eating takes two, so a philosopher can only eat while both neighbours are doing something else. Each one runs the same cycle: take both forks, eat, put them down, sleep, think. Go too long without eating and you die, and everything stops.

It is a synchronisation problem dressed up as a dinner scene. The forks are shared resources, the philosophers concurrent processes, and both classic traps are there: deadlock, where everyone waits on everyone else forever, and starvation, where someone never gets a turn.

One thread per philosopher

./philo <philosophers> <time_to_die> <time_to_eat> <time_to_sleep> [meals], the durations in milliseconds. The program starts one thread per philosopher and one lock per fork, plus a last thread that does nothing but watch.

A philosopher’s two neighbours are their two locks, picked by position around the table. Taking a fork means locking; putting it down means unlocking. Nothing more, and that is already the whole problem.

Not deadlocking

If all five philosophers grab their left fork at the same instant, each holds one and waits for their neighbour’s, who is waiting for theirs. Nobody moves again. Deadlock is not a rare accident here, it is what the simplest code you could write does naturally.

Two measures head it off. Even and odd philosophers do not pick their forks up in the same order, which breaks the circular wait: there is always someone for whom the fork they want is free. And the odd ones begin by waiting out one meal’s duration, which staggers the two groups instead of starting them together.

The timeline of a real run shows the result: two neighbours never eat at the same time, and nobody waits forever for a turn.

An execution timeline of five philosophers over three and a half seconds: the eating bands of neighbours never overlap

The lone philosopher

One special case breaks everything else: a single philosopher. They have only one fork, so they will never eat, and the subject asks that they die cleanly.

The general code cannot handle it: it would lock the same fork twice and hang on the second attempt, never dying and never giving back control. So that philosopher gets their own path, which takes the one fork, waits out the survival delay and announces the death.

The watching thread

A death cannot be noticed by the one dying: a sleeping philosopher cannot wake up to report that they should have eaten. That is the watcher thread’s job, going round the table in a loop and comparing the clock against each deadline.

The deadline is an absolute time, not a countdown: at the start of every meal, the philosopher writes down the time at which they will die unless they have eaten again by then. Writing and reading it both go through a lock of that philosopher’s own, so the watcher never reads a half-updated time.

The same thread handles the other stopping condition: once everyone has reached the requested number of meals, the simulation ends by itself, with nobody dying.

A timeline of four philosophers with too short a survival delay: two eat, then the other two, and the fourth dies at 311 milliseconds

Sleeping without missing a death

One detail decides the grade. Putting a philosopher to sleep with a single two-hundred millisecond call makes the program deaf for two hundred milliseconds: a death in the meantime would be reported late, and the philosopher could go on announcing that they are sleeping after the simulation is over.

So the sleep is cut into hundred-microsecond slices, with a check between each one for whether the simulation has stopped. And printing itself takes the write lock and the stop lock before putting out a line, so that no message can ever follow a death.

What it teaches

A data race does not show up in testing. It shows up once, hours in, on a machine that is not yours, and it will not reproduce when you go looking for it. The only defence is to know, for every shared variable, which lock protects it, and never to touch it without that lock.

The rest is a matter of milliseconds. A philosopher who dies one millisecond late is a failed project.