DisSysLab · Distributed algorithms

Sharing Resources

Priority by time, or by an order on resources

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.

1. The problem

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.

Specification

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.

Figure 1. One round, with no conflict. Agents are on the left, managers on the right, and a line joins each agent to each manager. A dashed blue frame marks where the last event happened. Messages to a manager travel above a line, messages to an agent below it. Requests are white labels; resources are colored cups: brown tea, white milk, dark coffee. Maya becomes thirsty for tea and milk, gets them, drinks, and returns them.

2. What can go wrong

Deadlock

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.

Figure 2. Deadlock. Maya gets tea and asks for milk; Liu gets milk and asks for tea. Each holds what the other needs, and nobody will give anything up: they wait for ever.

Starvation

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.

Figure 3. Fixed priorities starve. Maya (id 2) and Liu (id 5) both want milk, again and again. Liu becomes thirsty again as soon as he finishes, and his messages are always delivered quickly. With Priority by id, Liu wins every time: when milk does reach Maya, Liu's next request takes it back before she can drink. Maya never drinks. Section 3 explains Priority by time, with which they take turns.

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.

3. Idea 1: the agent that waited longest wins

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.

Figure 4. Clocks and stamps. Each agent's clock moves right along its own line, at its own uneven pace. A flag marks a stamp: the clock reading when the agent became thirsty. The list on the right orders the stamps, earliest first, with ties broken by id. The orange line is a fixed time T. Sooner or later every clock passes T, and after that every new stamp lies to the right of T.
The algorithm with stamps
  1. A thirsty agent sends a request, carrying its stamp, to the manager of every resource it wants, all at once.
  2. A manager keeps its requests in order of stamp, and sends a free resource to the earliest request.
  3. If a request arrives that is earlier than the stamp of the agent holding the resource, the manager asks that agent to return it.
  4. An agent asked to return a resource returns it if it is thirsty; its request goes back into the manager's queue. If it is drinking, it keeps the resource until it finishes.
  5. An agent that holds everything it wants drinks, and returns everything when it finishes.
Figure 5. An earlier stamp takes a resource back. Maya's clock reads 4 and Liu's reads 2: the clocks do not agree. Maya becomes thirsty first, for tea and milk, with stamp (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.

Safety, informally

A resource is a token: managers and agents pass it on, but never copy it. So at most one agent holds it.

Progress, informally

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.

Figure 6. The early requests run out. Three agents become thirsty with stamps (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.

4. Idea 2: an order on resources

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.

Figure 7. Ordering removes the cycle. The same agents and wishes as in Figure 2, with the same timing of messages. Any order deadlocks, as before. With Highest first, both Maya and Liu ask for tea first. Liu waits for tea without holding milk, so Maya gets milk, drinks, and returns both; then Liu drinks.
Figure 8. A round with ordered requests. Maya wants tea and milk; Liu wants milk and coffee. Maya asks for tea, Liu for milk. Maya then asks for milk and waits in the queue behind Liu. Liu holds milk and asks for coffee, which no one holds; he gets it, drinks, and returns both. Milk goes to Maya, and she drinks.
Why no one waits for ever, informally

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.

Figure 9. Ordered requests, at random. Maya, Liu and Ana become thirsty for random sets of resources, and events happen in random order. The panel lists, for each thirsty agent, what it holds and what it is waiting for. The check below the list holds at every step: every agent waits only for a resource below everything it holds. The drink counts keep growing for all three.

5. Comparing the two ideas

Priority by timeOrder on resources
Needslocal clocks that tick forward for evera fixed order on the resources
Requestsall at onceone at a time, highest first
Queuesordered by stampfirst come, first served
Taking backa manager may recall a resource from a thirsty agentnever
Costresources may travel back and forthan 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.

6. Key ideas

  1. Priority among agents in conflict. Some agents win and others lose. Winning must be fair: every agent that wants to win wins eventually.
  2. Tokens. An agent that holds a token knows that no other agent holds it. Resources are tokens.
  3. Dynamic priorities. Priorities can come from an order on resources, from stamps made of clock readings and ids, or from an acyclic graph of agents that changes as they act, as in the dining philosophers.