One agent starts a computation by sending messages. The agents that receive them do some work and send more messages, and so on: the computation spreads, or diffuses, through the system. How does the agent that started it find out that it has ended? This class develops the answer that Dijkstra and Scholten gave in 1980. As before, the arguments are informal, and the animations show why they hold.
Agents send messages to each other along channels. Each agent is active or idle.
The computation has terminated when every agent, the initiator included, is idle, and every channel is empty.
Once terminated, the computation stays terminated: no agent is active to send a message, and no message is left to wake an agent up. Note that every agent idle is not enough.
u, so the
computation has not terminated. When the message arrives,
u becomes active again.
The initiator has a variable claim, initially false. Devise
an algorithm, running alongside the computation, that sets
claim to true when the computation has terminated.
Safety. If claim is true, the computation has
terminated.
Progress. If the computation terminates, claim
eventually becomes true.
Keep a structure that contains everything that could still keep the
computation going: every active agent, and every agent that has sent a
message still in transit. If the structure shrinks to just the initiator,
and the initiator is idle, nothing is left to keep the computation going.
Dijkstra and Scholten used a tree rooted at the initiator. Each
agent v has a variable v.parent: null if
v is not on the tree, and otherwise its parent on the tree.
w, then
w is on the tree.An idle agent may stay on the tree, and an idle agent may have active descendants. The tree changes in only two ways:
How does an agent know that it has no children and no messages in transit? Every message is acknowledged: its receiver eventually sends back an ack. Each agent counts its messages that have not yet been acknowledged.
claim to true.
The held ack is what makes the counts tell the truth. While
v is a child of w, v has not acked
the message that made it join, so w has at least one
unacknowledged message. And a message in transit has not been acked
either. So when all of an agent's messages are acknowledged, it has no
children and no messages in transit.
r receive a
second message, from s: it is already on the tree, so it
acks at once. Then the tree shrinks from its leaves, each leaving agent
sending its held ack to its parent, until the initiator claims
termination.
Suppose an agent acked every message at once, including the one that made it join. Then its parent's count could reach zero while it is still a child, and the parent could leave the tree, or the initiator could claim termination, while the child is still active.
p, and p sends
one to r. With Ack at once, both acks come straight
back, so when p becomes idle it leaves the tree while its
child r is still active, and when the initiator becomes idle
it claims termination. The checks on the right turn red. With Hold the
ack, the initiator must wait until r and then
p have left the tree.
Each step keeps the invariant. A receiver becomes active and is on the tree, either already or by joining under the sender, which is on the tree. A sender is active, so it is on the tree. An agent that leaves is idle and has no unacknowledged messages, so it has no children and no messages in transit: removing it breaks nothing. Now, when the initiator claims termination, it is idle and has no unacknowledged messages, so it has no children: the tree is just the initiator. By the invariant, no agent is active and no message is in transit. So the computation has terminated.
Suppose the computation has terminated. Then no agent sends another message, and every message still in transit is an ack. Acks arrive. Every leaf of the tree is idle and, once its acks have arrived, has no unacknowledged messages, so it leaves, sending an ack to its parent. The tree loses an agent each time, so it shrinks to the initiator, which then claims termination.
claim becomes true only after the computation has
terminated. Press Reset for a different run.
The algorithm is due to E. W. Dijkstra and C. S. Scholten, “Termination detection for diffusing computations” (Information Processing Letters, 1980). They call an ack a signal, and an agent's number of unacknowledged messages its deficit. The cost is one ack for every message. The tree is another example of a distributed data structure maintained by the actions of all the agents, none of which sees all of it.