Agents often need exclusive use of several resources at once: a set of files, a set of locks, a set of devices. This class gives two simple ideas for sharing resources fairly. The first decides conflicts by time: the agent that started waiting earliest wins. The second puts the resources in order and makes every agent ask for them in that order. As in the dining philosophers, the arguments are informal, and the animations show why they hold.
Our example has three resources, which we call tea, milk and coffee; think of them as three files. Each resource has a manager, an agent that hands it out and keeps a queue of requests for it. The other agents, here Maya and Liu, go round a cycle of three states, which Chandy and Misra called
A thirsty agent sends a request to the manager of each resource it wants; a manager sends a resource to an agent, and the agent later sends it back. Messages take an arbitrary but finite time. Each resource is a token: it is always in exactly one place.
Safety. A resource is held by at most one agent at a time.
Progress. Every thirsty agent drinks eventually.
The dining philosophers are the special case in which every agent always wants the same resources, the forks it shares with its neighbors.
Suppose agents ask for their resources one at a time, in any order, and managers serve requests first come, first served. Maya wants tea and then milk; Liu wants milk and then tea.
Deadlock comes from waiting in a cycle. A priority breaks the cycle: a manager can take a resource back from a lower-priority agent that is still thirsty, and give it to a higher-priority one. Give each agent a fixed number, its id, and let the higher id win. Will that work? No: an agent with a high id can win every conflict, for ever.
The priority of an agent that loses a conflict must rise relative to the winner. The two ideas that follow do this in different ways.
Each agent has a local clock. The clocks need not agree; all we need is
that every clock ticks forward for ever, so that it eventually passes
any given time. When an agent becomes thirsty it reads its clock and
takes a stamp (t, id). Stamps are compared first by
t, and when the clock readings are equal, by id: the
smaller id wins the tie. So any two stamps can be compared, and no two
are equal. The earlier stamp has the higher priority.
T.
Sooner or later every clock passes T, and after that
every new stamp lies to the right of T.
(4, Maya). Liu becomes thirsty a little later, for milk and
coffee, but his stamp (2, Liu) is earlier. When his
request reaches the milk manager, Maya has not started drinking, so
the manager asks her to return milk. Liu drinks; then milk goes back
to Maya, and she drinks.
Go back to Figure 3 and choose Priority by time. Each time Liu becomes thirsty again, his new stamp is later than Maya's, so the manager no longer takes milk away from her: she drinks, and they take turns.
A resource is a token: managers and agents pass it on, but never copy it. So at most one agent holds it.
Take a thirsty agent with stamp (T, id). The argument
has two parts.
Part 1: every clock passes T, and stays past it (Figure 4).
Clocks tick forward and never go back. So after a while every new
request has a stamp later than T, and only finitely many
requests have stamps up to T.
Part 2: the requests with stamps up to T run out (Figure 6). Look at the earliest of them that is still waiting. Every manager it asks puts it first, and takes the resource back from any later agent that is only thirsty; an agent that is drinking gives the resource back when it finishes. So the earliest request gets everything it wants, and drinks. Then the next earliest does, and so on, until our agent drinks.
(3, Maya),
(4, Ana) and (5, Liu), and they want
overlapping sets. Take T = 5, and start when every clock
has already passed T, so every later request has a later
stamp. Events then happen at random. The panel counts the requests
with stamps up to T that are still waiting, and marks
the earliest one, with a thick outline. That one is never kept waiting
for good, and the count falls to zero.
The second idea needs no clocks. Give the resources a fixed order, here tea > milk > coffee. An agent asks for the resources it wants one at a time, highest first, and asks for the next only when it holds all the higher ones. For example, an agent that wants coffee and tea asks for tea, and asks for coffee only once it has tea. Managers serve requests first come, first served, and never take a resource back.
A thirsty agent waiting for a resource r holds only
resources above r, because it asks highest first. So an
agent never waits for a resource higher than one it holds, and a cycle
of agents each waiting for the next is impossible.
Go up the order, starting from the lowest resource, coffee. An agent holding coffee waits for nothing below it, so it has everything it wants: it drinks and returns coffee, and since the queue is first come, first served, everyone waiting for coffee gets it in turn. An agent holding milk waits at most for coffee, which it gets; so it drinks and returns milk, and everyone in the milk queue gets milk in turn. Then the same for tea. So every thirsty agent drinks.
| Priority by time | Order on resources | |
|---|---|---|
| Needs | local clocks that tick forward for ever | a fixed order on the resources |
| Requests | all at once | one at a time, highest first |
| Queues | ordered by stamp | first come, first served |
| Taking back | a manager may recall a resource from a thirsty agent | never |
| Cost | resources may travel back and forth | an agent may hold resources while it waits for lower ones |
The dining philosophers algorithm is a third way: there the priority between two neighbors is stored in the fork they share, and changes when one of them eats.