-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathToken_Ring.java
More file actions
106 lines (89 loc) · 4.56 KB
/
Token_Ring.java
File metadata and controls
106 lines (89 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Name: Amey Mahendra Thakur
* Course: Distributed Computing Lab (CSL802)
* Roll No: 50 | Batch: B3
* Date of Experiment: February 18, 2022
* Repository: https://github.com/Amey-Thakur/DISTRIBUTED-COMPUTING-AND-DISTRIBUTED-COMPUTING-LAB
* Description: Experiment 6 - Implementation of Token Ring Mutual Exclusion Algorithm for Distributed Systems.
*/
import java.util.Scanner;
import java.util.InputMismatchException;
public class Token_Ring {
/**
* The Token Ring algorithm ensures mutual exclusion by passing a 'token'
* around a logical ring of processes. Only the process holding the token
* can access the critical section or send data.
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--- [TOKEN RING MUTUAL EXCLUSION SIMULATOR] ---");
try {
// 1. Initialize the logical ring
System.out.print("[?] Enter the total number of nodes in the ring: ");
int numNodes = scanner.nextInt();
System.out.println("[*] Initializing Ring Topology:");
System.out.print(" Ring: ");
for (int i = 0; i < numNodes; i++) {
System.out.print(i + " -> ");
}
System.out.println("0 (Circular)");
int currentTokenPosition = 0; // Initial token holder
int userChoice = 0;
// 2. Main execution loop for data transmission
do {
System.out.println("\n" + "-".repeat(30));
System.out.print("[?] Enter Sender Node ID: ");
int sender = scanner.nextInt();
System.out.print("[?] Enter Receiver Node ID: ");
int receiver = scanner.nextInt();
System.out.print("[?] Enter Data Packet (Integer): ");
int data = scanner.nextInt();
// Validation: Ensure nodes exist
if (sender >= numNodes || receiver >= numNodes || sender < 0 || receiver < 0) {
System.out.println("[!] Error: Invalid Node ID. Please enter IDs between 0 and " + (numNodes - 1));
userChoice = 1; // Prompt retry
continue;
}
// 3. Token Passing Phase: Passing token from current holder to the desired sender
System.out.print("\n[*] Token Passing sequence: ");
int tempPosition = currentTokenPosition;
while (tempPosition != sender) {
System.out.print(tempPosition + " -> ");
tempPosition = (tempPosition + 1) % numNodes;
}
System.out.println(sender + " [TOKEN ACQUIRED]");
// 4. Data Transmission Phase: Forwarding data through the ring to the receiver
System.out.println("[*] Node " + sender + " is transmitting data: " + data);
int forwarder = (sender + 1) % numNodes;
while (forwarder != receiver) {
System.out.println(" -> Data forwarded by Node " + forwarder);
forwarder = (forwarder + 1) % numNodes;
}
System.out.println("[+] Node " + receiver + " successfully received data: " + data);
// Update token position to the last sender (or specific protocol logic)
currentTokenPosition = sender;
// 5. Continuation Prompt
boolean validMenuInput = false;
while (!validMenuInput) {
System.out.print("\n[?] Do you want to perform another transmission? (1: Yes, 0: No): ");
try {
userChoice = scanner.nextInt();
if (userChoice == 0 || userChoice == 1) {
validMenuInput = true;
} else {
System.out.println("[!] Please enter 1 for Yes or 0 for No.");
}
} catch (InputMismatchException e) {
System.out.println("[!] Invalid input type. Please enter a number.");
scanner.next(); // Clear buffer
}
}
} while (userChoice == 1);
System.out.println("\n[*] Terminating simulator. Protocol sequence finalized.");
} catch (InputMismatchException e) {
System.out.println("[!] Critical Error: Invalid input type recorded.");
} finally {
scanner.close();
}
}
}