Agents that share resources must sometimes wait for each other. This class builds, step by step, an algorithm by which agents that compete for shared resources take turns fairly, with no central coordinator. Each step is shown by an animation, and the arguments that the algorithm is correct are informal: the animations show why each claim holds.
The agents are the vertices of an undirected graph. Two agents joined by an edge are neighbors: they share a resource that only one of them can use at a time, such as a printer, a file or a lock. Each agent goes round a cycle of three states. Following Dijkstra, we call them thinking, hungry and eating, and we call the agents philosophers.
A philosopher decides by itself when to become hungry and when to stop eating. The algorithm decides only when a hungry philosopher may start eating.
Safety. Neighbors never eat at the same time.
Progress. Every hungry philosopher eats eventually.
Without an algorithm, safety fails as soon as two neighbors are hungry at about the same time. Figure 1 lets every hungry philosopher eat whenever it likes.
u, v, w,
x, y are used throughout this class; an edge
joins neighbors. White is thinking, amber is hungry, green is eating.
At each step one philosopher moves to the next state of its cycle,
shown on the right. Nothing stops two neighbors from eating together:
the edge between them turns red.
Put one fork on each edge: an object that cannot be copied or
destroyed. The fork shared by u and v is
always at u, at v, or in a message on its way
from one to the other. A philosopher eats only while it holds all the
forks it shares with its neighbors.
If u is eating, it holds the fork it shares with
v. There is only one such fork, so v does not
hold it, and so v is not eating.
Forks are an example of tokens: an agent that holds a token knows that no other agent holds it. Forks settle safety, but not who should get a fork when both neighbors want it. That is the real problem.
When several philosophers want the same forks, some must win and others must lose. A simple rule, applied the same way by everyone, can fail. Take three philosophers in a ring, each hungry and each holding one of the two forks it needs.
Whatever the rule, if every philosopher is in the same situation and follows the same rule, the situation can stay symmetric for ever. So the algorithm keeps an asymmetric structure: a priority between each pair of neighbors.
Direct each edge from the neighbor with higher priority to the neighbor with lower priority. The algorithm keeps this directed graph acyclic.
In an acyclic graph, follow edges backwards, to higher and higher priority: the walk must stop, at a philosopher that no neighbor outranks. A cycle of priorities would be the ring of Figure 2 again, with no one on top.
Fixed priorities are not fair: a philosopher at the bottom could lose
every conflict. So when a philosopher eats, its priority should drop.
How should the edges at v change when v eats?
v? This can create a cycle.v towards v,
so that v has lower priority than all its neighbors.
This keeps the graph acyclic.y first: the cycle u → w → y → u
appears in red.
Only the edges at v change, so a new cycle would have to
pass through v. But after v eats, every edge
at v points into v: a path that reaches
v cannot leave it. So there is no new cycle.
No agent can see the whole priority graph. The algorithm stores each edge's priority in its fork. A fork is clean or dirty. Eating makes all of a philosopher's forks dirty. A fork is cleaned when it is sent, so a fork in a message is clean.
Sending a fork does not change the priority: a dirty fork held by
u and the same fork, clean, on its way to v
both say that v is higher. Priority changes only when
clean forks become dirty, and that happens only when a philosopher eats:
all its edges then point towards it, which is the rule of section 4. So
the priority graph stays acyclic, although no one computes it.
Each edge carries one request, like a second token: the philosopher without the fork can send it once, and the fork comes back in reply. Figure 4 runs the algorithm with every event chosen at random, and checks three properties after every step.
The checks in Figure 4 are the facts behind the algorithm. Safety holds because of the forks. The priority graph stays acyclic because only eating changes it. And a thinking philosopher holds only dirty forks: it can hold a clean fork only after asking for it while hungry, and it stops being hungry only by eating, which dirties every fork it holds. That is why a thinking philosopher always gives a fork when asked.
Progress is harder. Could a group of philosophers take turns eating, one after the other, so that some hungry neighbor never eats? Could hungry philosophers hold some of their forks for ever, each waiting for the others? The argument below shows that neither can happen.
Fix a hungry philosopher v. Look at the philosophers
above v: those with a path to v in the
priority graph. Count how many of them are thinking,
nT, and how many are hungry, nH. We show that
the pair (nT, nH) goes down, comparing first
nT, then nH, until v eats.
v. The only change to
the priority graph points the edges of an eater towards it. That
creates no new path to v while v is hungry.
So nT never goes up, and nH goes up only
when a thinking philosopher above v becomes hungry, and
then nT goes down.v through hungry philosophers: since the graph is acyclic,
this ends at a hungry philosopher y with no hungry
philosopher above it (perhaps v itself). A neighbor above
y is thinking: it is not hungry, by the choice of
y, and it is not eating, since an eater's forks are dirty
and so an eater is below its neighbors.y
their forks when asked. A neighbor below y holds a dirty
fork, which it gives up when asked, at the latest when it finishes
eating. So y gets all its forks and eats, unless, before
that, a thinking neighbor above it becomes hungry, which makes
nT go down.y eats, all its edges
point towards it, so it is no longer above v:
nH goes down and nT stays the same.
So (nT, nH) keeps going down. A pair of natural numbers
cannot go down for ever, so eventually no hungry philosopher is above
v; then v is on top, and by step 3 it eats.
v are ringed in purple. The hungry
philosopher on top is marked top. The one on top eats:
it eats, its edges turn towards it, and it thinks again. A thinker
above v becomes hungry: the other way the pair can change. Watch
(nT, nH) on the right: it goes down at every step, in
either case. With The one on top eats alone, the pair goes
(1, 3), (1, 2), (1, 1),
(0, 0), and then v eats.
There is a simpler way to decide who gets a fork: the philosopher that started waiting earliest wins. This needs clocks instead of clean and dirty forks.
Each philosopher has a local clock. The clocks need not agree with each
other; all we need is that every clock ticks forward for ever, so that
it eventually passes any given time. Clocks kept roughly in step by a
time server, and the logical clocks of an earlier class, both qualify.
A time at philosopher x is a pair
(t, x), where t is the reading of
x's clock. Two times are compared first by
t, and if the readings are equal, by the names of the
philosophers. So no two philosophers ever have the same time, and any
two times can be compared.
Safety holds for the same reason as before: the forks. Priorities need no special care, because times are totally ordered: the earlier timestamp always wins, so there is no cycle of priorities and no symmetric state as in Figure 2.
Among all hungry philosophers, take the one with the earliest
timestamp. Its thinking neighbors give it their forks, its hungry
neighbors have later timestamps and also give, and its eating
neighbors give when they finish. So it eats. Now fix a hungry
philosopher v. Its timestamp does not change while it
waits, and every clock eventually passes it, so after a while every
philosopher that becomes hungry has a later timestamp than
v. Only finitely many hungry philosophers are ahead of
v, each of them eats in turn, and then v has
the earliest timestamp and eats.
The price is the clocks. The fork algorithm of sections 5 and 6 needs no clocks at all: the priority between two neighbors is stored in the fork they share.