Name: Bhushan Manojkumar Bhujbal
Roll No: 507
Practical: TokenRing
//
***********************************************************************************
***************//
import java.util.Scanner;
public class TokenRing {
private static int n; // Number of processes
private static int token; // Index of the current process holding the token
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
n = sc.nextInt();
System.out.println("Ring formed is as below: ");
for (int i = 0; i < n; i++) {
System.out.print(i + " ");
}
System.out.println("0");
token = 0; // Initialize token holder to process 0
while (true) {
System.out.println("Process " + token + " has the token.");
System.out.println("Does process " + token + " want to access the
shared resource? (yes/no)");
String choice = sc.next();
if (choice.equalsIgnoreCase("yes")) {
// Process wants to access the shared resource
System.out.println("Process " + token + " is in the critical
section.");
// Simulating critical section
System.out.println("Process " + token + " is accessing the shared
resource.");
// Release the token
System.out.println("Process " + token + " releases the token.");
// Pass the token to the next process
token = (token + 1) % n;
} else {
// Process doesn't want to access the shared resource, just pass
the token
System.out.println("Process " + token + " doesn't want to access
the shared resource.");
// Pass the token to the next process
token = (token + 1) % n;
}
// Simulate some delay for token passing
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//
***********************************************************************************
*******************//
Output:
it@it-HP-EliteDesk-800-G2-SFF:~/Desktop/Bhushan/TokenRing$ javac TokenRing.java
it@it-HP-EliteDesk-800-G2-SFF:~/Desktop/Bhushan/TokenRing$ java TokenRing
Enter the number of processes: 10
Ring formed is as below:
0 1 2 3 4 5 6 7 8 9 0
Process 0 has the token.
Does process 0 want to access the shared resource? (yes/no)
yes
Process 0 is in the critical section.
Process 0 is accessing the shared resource.
Process 0 releases the token.
Process 1 has the token.
Does process 1 want to access the shared resource? (yes/no)
no
Process 1 doesn't want to access the shared resource.
Process 2 has the token.
Does process 2 want to access the shared resource? (yes/no)
yes
Process 2 is in the critical section.
Process 2 is accessing the shared resource.
Process 2 releases the token.
noProcess 3 has the token.
Does process 3 want to access the shared resource? (yes/no)
Process 3 doesn't want to access the shared resource.
yProcess 4 has the token.
Does process 4 want to access the shared resource? (yes/no)
no
Process 4 doesn't want to access the shared resource.
Process 5 has the token.
Does process 5 want to access the shared resource? (yes/no)
yes
Process 5 is in the critical section.
Process 5 is accessing the shared resource.
Process 5 releases the token.
Process 6 has the token.
Does process 6 want to access the shared resource? (yes/no)
yes
Process 6 is in the critical section.
Process 6 is accessing the shared resource.
Process 6 releases the token.
Process 7 has the token.
Does process 7 want to access the shared resource? (yes/no)
no
Process 7 doesn't want to access the shared resource.
Process 8 has the token.
Does process 8 want to access the shared resource? (yes/no)
no
Process 8 doesn't want to access the shared resource.
Process 9 has the token.
Does process 9 want to access the shared resource? (yes/no)
yes
Process 9 is in the critical section.
Process 9 is accessing the shared resource.
Process 9 releases the token.
Process 0 has the token.
Does process 0 want to access the shared resource? (yes/no)