import [Link].
*;
class Node extends Thread {
int id;
List<Node> neighbors;
static int sharedMemory = 0; // write this line only for shared
memory
Node(int id) {
[Link] = id;
neighbors = new ArrayList<>();
}
public void sendMessage(String msg, Node receiver) {
[Link]("Node " + id + " sends message to Node " +
[Link] + ": " + msg);
[Link](msg, this);
}
public void receiveMessage(String msg, Node sender) {
[Link]("Processing message...");
[Link]("Node " + id + " received message from Node
" + [Link] + ": " + msg);
}
public void run() {
[Link]("Node " + id + " is running...");
}
}
// 👉 Change class name according to the topic if required
// Examples:
// IPCDemo, RPCDemo, BullyElection, ClockSync, MutualExclusion, etc.
public class DistributedSystemExam {
public static void main(String[] args) {
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
[Link]();
[Link]();
[Link]();
// =========================================================
// 👉 UNCOMMENT ONLY ONE BLOCK IN EXAM
// =========================================================
// ================================
// 1. INTERPROCESS COMMUNICATION
// ================================
/*
[Link]("=== Interprocess Communication ===");
[Link]("Sending message between processes");
[Link]("IPC Message", n2);
*/
// ================================
// 2. REMOTE PROCEDURE CALL (RPC)
// ================================
/*
[Link]("=== Remote Procedure Call ===");
[Link]("Client calling remote method on server");
[Link]("RPC Request", n2);
[Link]("Server processing request and sending
response");
[Link]("RPC Response", n1);
*/
// ================================
// 3. GROUP COMMUNICATION
// ================================
/*
[Link]("=== Group Communication ===");
[Link]("Broadcasting message to all nodes");
for(Node n : [Link](n1, n2, n3)) {
[Link]("Hello Group", n);
}
*/
// ================================
// 4. CLOCK SYNCHRONIZATION
// ================================
/*
[Link]("=== Clock Synchronization ===");
int t1 = new Random().nextInt(100);
int t2 = new Random().nextInt(100);
[Link]("Node1 time: " + t1);
[Link]("Node2 time: " + t2);
int avg = (t1 + t2) / 2;
[Link]("Synchronized time: " + avg);
*/
// ================================
// 5. BULLY ELECTION ALGORITHM
// ================================
/*
[Link]("=== Bully Election Algorithm ===");
[Link]("Node 1 initiating election");
if([Link] > [Link] && [Link] > [Link]) {
[Link]("Node " + [Link] + " becomes
coordinator");
} else if([Link] > [Link]) {
[Link]("Node " + [Link] + " becomes
coordinator");
} else {
[Link]("Node " + [Link] + " becomes
coordinator");
}
*/
// ================================
// 6. MUTUAL EXCLUSION (MAEKAWA)
// ================================
/*
[Link]("=== Mutual Exclusion (Maekawa) ===");
[Link]("Node requesting critical section");
[Link]("Permission granted");
[Link]("Node entering critical section");
[Link]("Node leaving critical section");
*/
// ================================
// 7. DEADLOCK DETECTION
// ================================
/*
[Link]("=== Deadlock Detection ===");
[Link]("Checking for circular wait...");
[Link]("Deadlock detected between Node1 and
Node2");
*/
// ================================
// 8. LOAD BALANCING
// ================================
/*
[Link]("=== Load Balancing ===");
[Link]("Distributing tasks among nodes");
[Link]("Task1 -> Node1");
[Link]("Task2 -> Node2");
[Link]("Task3 -> Node3");
*/
// ================================
// 9. SHARED MEMORY
// ================================
/*
[Link]("=== Shared Memory ===");
[Link]++;
[Link]("Shared Memory Value: " +
[Link]);
*/
// ================================
// 10. DISTRIBUTED FILE SYSTEM
// ================================
/*
[Link]("=== Distributed File System ===");
[Link]("Reading file from Node1");
[Link]("Sending file to Node2");
*/
// =========================================================
}
}