Create a solution to detect deadlocks in a distributed system using the Chandy-Misra-Haas algorithm. Messages are passed between processes to determine if a cycle exists in the resource dependency graph.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The solution outlines the distributed deadlock detection using probe messages carrying (initiator, sender, receiver). It simulates processes waiting on others in a wait-for graph, forwarding probes to detect cycles. When a probe returns to its initiator, a deadlock is identified. The provided Python example models processes, message passing, and cycle detection via this mechanism.
One of the simplest distributed deadlock–detection schemes is the probe–based algorithm due to Chandy, Misra and Haas. It works on the wait–for graph (WFG): a directed edge P→Q means that process P is currently blocked waiting for Q to release some resource. A deadlock exists iff there is a directed cycle in this graph.
The CMH algorithm never centralises the graph. Instead, blocked processes inject small “probe” messages into the network. A probe carries three fields:
initiator: the process that started the detection,
sender: the process that forwarded the probe,
receiver: the next process along a wait–for edge.
The algorithm in its simplest form (AND‑model, single‑instance resources) is:
code
procedure initiate_detection(P):
if P is blocked:
for each Q in WaitFor[P]:
send PROBE(initiator=P, sender=P, receiver=Q) to Q
on receipt of PROBE(i,j,k) at process P (where P==k):
if P is not blocked: discard the probe
else if i == P:
// the probe has come back to the initiator along a chain of waits
report deadlock involving P
else:
for each Q in WaitFor[P]:
send PROBE(initiator=i, sender=P, receiver=Q) to Q
Each process executes these rules locally; no global state is needed. If a probe ever returns to its initiator, a cycle exists in the WFG rooted at that initiator.
The following Python script simulates this behaviour for a small set of processes:
python
from collections import deque, defaultdict
class Probe:
def __init__(self, initiator, sender, receiver):
self.initiator = initiator
self.sender = sender
self.receiver = receiver
def __repr__(self):
return f"<Probe init={self.initiator} sent={self.sender}->{self.receiver}>"
class Process:
def __init__(self, pid, waits_for=None):
self.pid = pid
self.waits_for = waits_for or []
self.blocked = bool(self.waits_for)
def initiate_detection(self, send_func):
if self.blocked:
for q in self.waits_for:
send_func(Probe(self.pid, self.pid, q))
def on_probe(self, probe, send_func):
if not self.blocked:
return # ignore probes if not waiting on anyone
if probe.initiator == self.pid:
print(f"Deadlock detected involving {self.pid}")
return
for q in self.waits_for:
send_func(Probe(probe.initiator, self.pid, q))
# build a wait-for graph: 1→2, 2→3, 3→1 forms a cycle
waits = {
1: [2],
2: [3],
3: [1],
4: [] # not blocked
}
processes = {pid: Process(pid, waits_for=deps) for pid, deps in waits.items()}
# simple message queue to simulate asynchronous sends
msg_queue = deque()
def send(probe):
msg_queue.append(probe)
# initiate detection from each blocked process
for p in processes.values():
p.initiate_detection(send)
# main loop: deliver probes until queue is empty
while msg_queue:
probe = msg_queue.popleft()
receiver = processes[probe.receiver]
receiver.on_probe(probe, send)
When you run this, you will see:
code
Deadlock detected involving 1
because the probe initiated by process 1 is eventually forwarded 1→2→3→1 and returns to its initiator. If you change the waits dictionary to remove the cycle, the probes will be discarded because the receiver processes are not blocked or the probes never return to the initiator.
In a real distributed system, the same logic is embedded in each process. Processes maintain their own waits_for sets based on their outstanding resource requests, call initiate_detection when they block, and call on_probe whenever a PROBE message arrives. The only messages exchanged are the constant‑sized PROBE tuples, and a deadlock is detected locally when an initiator sees one of its own probes return.