0% found this document useful (0 votes)
2 views3 pages

dc exp 7

The document outlines a Python program implementing the Chandy-Misra-Haas algorithm for distributed deadlock management using threading and UDP sockets. It simulates multiple processes that request resources and send probe messages to detect deadlocks. The program includes classes for processes and a server to handle incoming probe messages, demonstrating a scenario where processes create a deadlock situation.

Uploaded by

Ganesh Panigrahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

dc exp 7

The document outlines a Python program implementing the Chandy-Misra-Haas algorithm for distributed deadlock management using threading and UDP sockets. It simulates multiple processes that request resources and send probe messages to detect deadlocks. The program includes classes for processes and a server to handle incoming probe messages, demonstrating a scenario where processes create a deadlock situation.

Uploaded by

Ganesh Panigrahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment No: 07

Aim: Program on Distriuted Deadlock Management

Source Code:

Chandy-Misra-Haas algorithm:

import threading
import time
import random
import pickle
import socket

class Process(threading.Thread):
def __init__(self, process_id, neighbors, resources_needed,
coordinator_address):
threading.Thread.__init__(self)
self.process_id = process_id
self.neighbors = neighbors # List of processes this process
can send probes to
self.resources_needed = resources_needed # Resource ids this
process is waiting for
self.coordinator_address = coordinator_address
self.state = 'IDLE' # States: IDLE, WAITING, PROBING
self.received_probes = set()

def run(self):
""" Start the process by requesting resources and sending
probes. """
self.request_resources()

def request_resources(self):
""" Simulate requesting resources. If resources are
unavailable, start waiting. """
print(f"Process {self.process_id} requesting resources:
{self.resources_needed}")

# Simulate the waiting process (in reality, this would be


waiting for resource availability)
if self.resources_needed:
self.state = 'WAITING'
print(f"Process {self.process_id} is waiting for
resources {self.resources_needed}.")
self.send_probe_messages()

def send_probe_messages(self):
""" Send probe messages to the processes that the process is
waiting on. """
for neighbor in self.neighbors:
self.send_probe(neighbor)

def send_probe(self, target_process_id):


""" Send a probe message to the target process. """
probe_message = {'process_id': self.process_id, 'probe_id':
self.process_id}
try:
# Send probe message via UDP to target process
target_address = ('localhost', 10000 + target_process_id)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(pickle.dumps(probe_message), target_address)
sock.close()
except Exception as e:
print(f"Error sending probe from {self.process_id} to
{target_process_id}: {e}")

def receive_probe(self, message, address):


""" Handle receiving a probe message. """
probe_message = pickle.loads(message)
sender_id = probe_message['process_id']
probe_id = probe_message['probe_id']

# Detect deadlock (cycle detection)


if sender_id == self.process_id:
print(f"Process {self.process_id} detected a deadlock!
Cycle detected.")
return # Deadlock detected

if probe_id not in self.received_probes:


# If the probe has not been seen before, forward it to
neighbors
print(f"Process {self.process_id} forwarding probe to
neighbors (except {sender_id}).")
self.received_probes.add(probe_id)
self.send_probe_messages()

class Server(threading.Thread):
def __init__(self, process_id, neighbors):
threading.Thread.__init__(self)
self.process_id = process_id
self.neighbors = neighbors

def run(self):
""" Start listening for probes from other processes. """
server_address = ('localhost', 10000 + self.process_id)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(server_address)

print(f"Process {self.process_id} server listening at


{server_address}")

while True:
message, address = sock.recvfrom(4096)
# Handle received probe message
process = processes[self.process_id]
process.receive_probe(message, address)

# Simulate multiple processes


def main():
num_processes = 3 # Number of processes
processes.clear() # Clear any existing processes

# Define the neighbors and resources each process needs


neighbors = [
[1], # Process 0 waits for Process 1
[2], # Process 1 waits for Process 2
[0], # Process 2 waits for Process 0 (this creates a
deadlock)
]

resources_needed = [
['A'], # Process 0 needs resource A
['B'], # Process 1 needs resource B
['A'], # Process 2 needs resource A (creates a cycle)
]

# Start servers (each process has a server to listen for probes)


for i in range(num_processes):
server = Server(i, neighbors[i])
server.start()

# Start processes (clients)


for i in range(num_processes):
process = Process(i, neighbors[i], resources_needed[i],
('localhost', 10000 + i))
processes.append(process)
process.start()

# Wait for processes to finish (not necessary, just for


simulation)
for process in processes:
process.join()

if __name__ == "__main__":
processes = []
main()

Output:

You might also like