Distributed_Lab[1] MAHBOOB
Distributed_Lab[1] MAHBOOB
SYSTEM
LAB FILE
BCE- C 661
This is to certify that I, MAHBOOB RAZA student of CSE Third Year (Session
2022-2026) has completed the enclosed program all by myself and have not
submitted these programs elsewhere for any other purpose.
This is to certify that MAHBOOB RAZA who is a student of CSE Third year
(Session 2022- 2026) has completed all the programs enclosed in this file under
my supervision and has not submitted these programs elsewhere for award of any
degree as per best of my knowledge.
4. Locking Algorithm
7. Chat Server
Steps
1. send_request()
3. enter_critical_section()
4. send_reply_to_others()
Program :---
import threading
import time
import random
class Process(threading.Thread):
super().__init__()
self.pid = pid
self.clock = 0
self.queue = []
self.processes = processes
def request_cs(self):
self.clock += 1
for p in self.processes:
if p.pid != self.pid:
p.receive_request(self.pid, self.clock)
self.queue.append((timestamp, pid))
def run(self):
time.sleep(random.uniform(1, 2)) # Simulate delay before requesting CS
self.request_cs()
# Create processes
processes = []
for i in range(3):
for p in processes:
p.processes = processes
for p in processes:
p.start()
for p in processes:
p.join()
Output :-
Program :-
class LamportClock:
def __init__(self):
self.time = 0
def tick(self):
self.time += 1
def send_event(self):
self.tick()
return self.time
# Example usage
p1 = LamportClock()
p2 = LamportClock()
t1 = p1.send_event()
p2.receive_event(t1)
output :-
3. Edge Chasing Deadlock Detection
Steps
if receives_own_probe:
deadlock detected
Program :-
wait_for = {
0: 1,
1: 2,
def detect_deadlock(initiator):
visited = set()
def forward(pid):
if pid in visited:
return
visited.add(pid)
if wait_for.get(pid) is None:
return
next_pid = wait_for[pid]
if next_pid == initiator:
print("Deadlock detected!")
else:
forward(next_pid)
forward(initiator)
detect_deadlock(0)
Output :-
4. Locking Algorithm
Steps
# Simple Lock
lock = False
if not lock:
lock = True
# critical section
lock = False
Program :-
import threading
lock = threading.Lock()
def critical_section(pid):
with lock:
for t in threads:
t.start()
for t in threads:
t.join()
Output :-
Steps
Install pyro5
Program :-
RMI SERVER
# rmi_server.py
import Pyro5.api
@Pyro5.api.expose
class Calculator:
return a + b
daemon.requestLoop()
RMI CLIENT
# rmi_client.py
import Pyro5.api
uri = input("Enter the URI of the remote object: ") # Paste the printed URI from the server
OUTPUT:
Steps
Program :-
RPC SERVER
# rpc_server.py
return x + y
server.register_function(add, "add")
server.serve_forever()
RPC CLIENT
# rpc_client.py
import xmlrpc.client
proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
OUTPUT:
7. Chat Server
Steps
# Server
socket.bind()
socket.listen()
conn.send("Hello client".encode())
# Client
socket.connect()
msg = socket.recv(1024).decode()
Program :-
# server.py
import socket
import threading
clients = []
def handle_client(client):
while True:
msg = client.recv(1024).decode()
for c in clients:
if c != client:
c.send(msg.encode())
server = socket.socket()
server.bind(('localhost', 5555))
server.listen()
while True:
clients.append(client)
threading.Thread(target=handle_client, args=(client,)).start()
# client.py
import socket
import threading
client = socket.socket()
client.connect(('localhost', 5555))
def listen():
while True:
msg = client.recv(1024).decode()
print("Received:", msg)
threading.Thread(target=listen).start()
while True:
msg = input()
client.send(msg.encode())
Ouput :-
8. Termination Detection (Diffusion)
Program :-
class Node:
self.pid = pid
self.children = []
self.ack_received = 0
def send_work(self):
child.send_work()
self.receive_ack()
def receive_ack(self):
self.ack_received += 1
# Create nodes
root = Node(0)
child1 = Node(1)
child2 = Node(2)
# Start
root.send_work()
Output :-