Forth
Forth
*;
public class Ring {
int max_processes;
int coordinator;
boolean processes[];
ArrayList<Integer> pid;
void displayProcesses() {
for(int i = 0; i < max_processes; i++) {
if(processes[i])
System.out.println("P" + (i+1) + " is up.");
else
System.out.println("P" + (i+1) + " is down.");
}
System.out.println("P" + (coordinator) + " is the coordinator");
}
while(temp != process_id - 1) {
if(processes[temp]) {
pid.add(temp+1);
System.out.print("Process P" + (temp + 1) + " sending the following
list:- ");
displayArrayList(pid);
}
temp = (temp + 1) % max_processes;
}
while(true) {
System.out.println("Ring Algorithm");
System.out.println("1. Create processes");
System.out.println("2. Display processes");
System.out.println("3. Up a process");
System.out.println("4. Down a process");
System.out.println("5. Run election algorithm");
System.out.println("6. Exit Program");
System.out.print("Enter your choice:- ");
choice = sc.nextInt();
switch(choice) {
case 1:
System.out.print("Enter the total number of processes:- ");
max_processes = sc.nextInt();
ring = new Ring(max_processes);
break;
case 2:
ring.displayProcesses();
break;
case 3:
System.out.print("Enter the process to up:- ");
process_id = sc.nextInt();
ring.upProcess(process_id);
break;
case 4:
System.out.print("Enter the process to down:- ");
process_id = sc.nextInt();
ring.downProcess(process_id);
break;
case 5:
System.out.print("Enter the process which will initiate
election:- ");
process_id = sc.nextInt();
ring.initElection(process_id);
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Error in choice. Please try again.");
break;
}
}
}
}