DisSysLab · Distributed algorithms

Distributed Dining Philosophers

Conflict resolution with forks and priorities

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.

1. The problem

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.

Specification

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.

Figure 1. Philosophers without an algorithm. The five philosophers 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.

2. Forks make safety easy

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.

Safety, informally

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.

3. Conflict resolution needs asymmetry

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.

Figure 2. A symmetric state can last for ever. Each philosopher holds the fork it shares with one neighbor and asks the other neighbor for the second fork. If every hungry philosopher gives a requested fork, the forks rotate and the state is the mirror image of the one before: nobody ever eats. If every hungry philosopher keeps its fork, nobody ever eats either.

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.

The priority graph

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.

4. Winners must yield

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?

Figure 3. How priorities change when a philosopher eats. An arrow points from higher to lower priority. Click a philosopher to let it eat. With Edges point to the eater, the graph stays acyclic however you click. With Flip the eater's edges, click y first: the cycle u → w → y → u appears in red.
Why the graph stays acyclic, informally

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.

5. Forks carry the priorities

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.

The algorithm
  1. A hungry philosopher asks for each fork it does not have, by sending its neighbor a request.
  2. A philosopher that holds a requested fork gives it, cleaned, if the fork is dirty and the philosopher is not eating. So a thinking philosopher always gives; a hungry philosopher gives a dirty fork but keeps a clean one, because then it has priority; an eating philosopher gives the fork when it finishes.
  3. A hungry philosopher that holds all its forks eats, unless it owes a neighbor a dirty fork. Eating makes all its forks dirty.

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.

Figure 4. The algorithm. White is thinking, amber is hungry, green is eating. Forks are dots on the edges: blue is clean, red is dirty. A fork next to a philosopher is held by it; a fork halfway along an edge is in a message, with a small arrow showing where it is going. Grey squares are requests. The arrows on the edges are the priorities, read off the forks. At each step one event happens, chosen at random; the panel on the right says which. The three checks hold at every step, and every philosopher keeps getting meals. Press Everyone hungry to make the competition fierce.

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.

6. Every hungry philosopher eats

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.

  1. Nothing new gets above 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.
  2. Someone is on top. Follow edges backwards from 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.
  3. The one on top eats. Thinking neighbors give 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.
  4. The eater drops out. When 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.

Figure 5. Why v eats. Philosophers above 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.

7. A simpler idea: the one that waits longest wins

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.

The algorithm with times
  1. When a philosopher becomes hungry, it records the time: its timestamp. While it is hungry it asks for every fork it does not have, and each request carries its timestamp.
  2. A thinking philosopher that gets a request gives the fork.
  3. A hungry philosopher that gets a request compares timestamps. If its own is earlier, it keeps the fork. If the request's is earlier, it gives the fork, and asks for it back later.
  4. An eating philosopher gives the fork when it finishes eating.
  5. A hungry philosopher that holds all its forks eats.

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.

Progress, informally

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.

8. Key ideas

  1. Conflict resolution in distributed systems. When agents want the same resource at the same time, some win and some lose.
  2. Priority among agents in conflict. Winning must be fair: every agent that wants to win wins eventually. A winner yields priority to the losers.
  3. Tokens. An agent that holds a token knows that no other agent holds it. Forks give safety; their cleanliness records priority.
  4. Dynamic data structures. The acyclic priority graph is maintained by the actions of all the agents, and no agent ever sees all of it.
  5. Time as priority. With clocks that tick forward for ever, “the agent that started waiting earliest wins” is a fair priority.