DS - LabFile Chirag
DS - LabFile Chirag
(KCS751A)
Simulation of Distributed
mutual exclusion in java.
3
Implementation of CORBA
(Common Object Request
4 Broker Architecture)
mechanism.
Implementation of Path
Pushing Algorithm
5
Implementation of Edge
Chasing Algorithm
6
Implementation of Commit
Protocol Algorithm
7
Implementation of Voting
Protocol Algorithm
8
EXPERIMENT 1
#include <stdio.h>
#include <stdlib.h>
logical_clock[0] = 1;
int main() {
int n;
lamport_logical_clock(events, n);
return 0;
}
Output:
EXPERIMENT 2
#include <stdio.h>
#define MAX 10
int main() {
int n, vc[MAX][MAX], choice, p1, p2;
Output:
EXPERIMENT 3
import java.util.Scanner;
if (canEnter) {
enterCriticalSection(process);
} else {
System.out.println("Process " + process + " is waiting for
replies...");
}
}
while (true) {
System.out.print("\n1. Request Critical Section\n2. Exit\nEnter
your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter process ID (0 to " +
(totalProcesses - 1) + "): ");
processId = scanner.nextInt();
if (processId < 0 || processId >= totalProcesses) {
System.out.println("Invalid process ID.");
} else if (criticalSection) {
System.out.println("Critical section is currently
occupied. Try again later.");
} else {
requestCriticalSection(processId);
}
break;
case 2:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try
again.");
}
}
}
}
EXPERIMENT 4
Step 1: Hello.idl
module HelloApp {
interface Hello {
string sayHello();
};
};
Step 2: HelloServer.java
import HelloApp.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
} catch (Exception e) {
e.printStackTrace();
}
}
}
Step 3: HelloClient.java
import HelloApp.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
} catch (Exception e) {
e.printStackTrace();
}
}
}
EXPERIMENT 5
class Graph:
def __init__(self, vertices):
self.vertices = vertices
self.edges = []
if __name__ == "__main__":
vertices = int(input("Enter the number of vertices: "))
edges = int(input("Enter the number of edges: "))
graph = Graph(vertices)
Output:
EXPERIMENT 6
class Process:
def __init__(self, pid):
self.pid = pid
self.waiting_for = [] # Processes this process is waiting for
visited.add(self.pid)
visited.remove(self.pid) # Backtrack
return False
if __name__ == "__main__":
# Create processes
num_processes = int(input("Enter the number of processes: "))
processes = {i: Process(i) for i in range(num_processes)}
# Add dependencies
print("Enter dependencies (process waiting for another process). Enter
-1 -1 to stop:")
while True:
p1, p2 = map(int, input().split())
if p1 == -1 and p2 == -1:
break
processes[p1].add_dependency(processes[p2])
Output:
EXPERIMENT 7
class Participant:
def __init__(self, pid):
self.pid = pid
self.vote = None # 'commit' or 'abort'
def decide_vote(self):
import random
self.vote = random.choice(['commit', 'abort'])
print(f"Participant {self.pid} votes {self.vote}")
return self.vote
class Coordinator:
def __init__(self, participants):
self.participants = participants
def two_phase_commit(self):
print("\nPhase 1: Voting Phase")
votes = []
for participant in self.participants:
votes.append(participant.decide_vote())
if 'abort' in votes:
print("\nPhase 2: Abort Phase")
print("Coordinator instructs all participants to ROLLBACK.")
self.rollback()
else:
print("\nPhase 2: Commit Phase")
print("Coordinator instructs all participants to COMMIT.")
self.commit()
def commit(self):
for participant in self.participants:
print(f"Participant {participant.pid} commits transaction.")
def rollback(self):
for participant in self.participants:
print(f"Participant {participant.pid} rolls back transaction.")
if __name__ == "__main__":
num_participants = int(input("Enter the number of participants: "))
participants = [Participant(pid) for pid in range(num_participants)]
coordinator = Coordinator(participants)
coordinator.two_phase_commit()
Output:
EXPERIMENT 8
class Participant:
def __init__(self, pid):
self.pid = pid
self.vote = None # 'yes' or 'no'
def cast_vote(self):
import random
self.vote = random.choice(['yes', 'no'])
print(f"Participant {self.pid} votes {self.vote}")
return self.vote
class Coordinator:
def __init__(self, participants):
self.participants = participants
def execute_voting_protocol(self):
print("\nVoting Phase:")
votes = []
for participant in self.participants:
votes.append(participant.cast_vote())
# Decision Phase
print("\nDecision Phase:")
if 'no' in votes:
print("Consensus not achieved. The proposal is REJECTED.")
else:
print("Consensus achieved. The proposal is ACCEPTED.")
if __name__ == "__main__":
num_participants = int(input("Enter the number of participants: "))
participants = [Participant(pid) for pid in range(num_participants)]
coordinator = Coordinator(participants)
coordinator.execute_voting_protocol()
Output: