import java.io.*; import java.util.*; class testpgm { int MaxProcess=100; //max no of processes allowed int p[] = new int[MaxProcess]; //array to store status of process 1-alive 0-crashed int actualProcesses=0; //actual no of proess by user int coord=0; //stores coordinator Scanner scrinp = new Scanner(System.in); //for taking input public void performElection(int candidate ) { int flag=0; // coordinator to be elected while(flag == 0) { if(p[candidate]==1) //should be alive { int j = candidate +1 ; while( j< actualProcesses) { if(j>candidate && p[j]==1) { System.out.println("\nProcess " + j + "is in election process."); flag =1; break; } j++; } if(flag==0) { coord = candidate; System.out.println("\nNew elected coordinator Process " + coord ); flag =1; } else { flag = 0; candidate = j; } } } } void bully() { System.out.println("ENter the no. of processes (Max 100) "); actualProcesses = scrinp.nextInt(); int i=0; coord = actualProcesses-1; for(i=0; i< actualProcesses ; i++) { p[i]=1; //make alive all the processes } int choice; do { System.out.println("Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit "); choice= scrinp.nextInt(); switch(choice) { case 1: System.out.println("ENter the process to be crashed"); int pc= scrinp.nextInt(); p[pc] = 0; System.out.println("\nProcess "+ pc + " crashed "); if( pc == coord) { int crash =0; for( i=0 ; i< actualProcesses; i++) { if(p[i] == 0) crash++; } if(crash == actualProcesses) { System.out.println("\nAll processes are crashed"); break; } System.out.println("\nCoordinator crashed."); System.out.println("\nPlease enter the initiator node "); int initiator= scrinp.nextInt(); while(p[initiator]==0) { System.out.println("\nThe entered node is already crashed."); System.out.println("\nPlease enter another initiator node "); initiator= scrinp.nextInt(); } performElection(initiator ); } break; case 2: System.out.println("\nENter the process to be recovered"); int pr= scrinp.nextInt(); if(p[pr] == 0) //check if proecss is crashed or not { p[pr] =1; //make iit alive System.out.println("\nProcess "+ pr + " recovered "); if( pr > coord) { performElection(pr ); } } else { System.out.println("\nProcess is already running"); } break; case 3: System.out.println("\nThe coordinator is " + coord); break; case 4: System.out.println("\n\n Status of processes - "); for( i=0 ; i< actualProcesses; i++) System.out.println( i +"-" + p[i] + " " ); break; case 5: System.exit(0); default: System.out.println("\nInvalid Choice "); } }while(true); } public static void main(String args[])throws IOException { testpgm test1 = new testpgm(); test1.bully(); } } |
Output: // C:\Users\GRIMM\Desktop>javac election.java // C:\Users\GRIMM\Desktop>java testpgm // ENter the no. of processes (Max 100) // 4 // Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit // 3 // The coordinator is 3 // Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit // 4 // Status of processes - // 0-1 // 1-1 // 2-1 // 3-1 // Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit // 4 // Status of processes - // 0-1 // 1-1 // 2-1 // 3-1 // Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit // 1 // ENter the process to be crashed // 2 // Process 2 crashed // Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit // 4 // Status of processes - // 0-1 // 1-1 // 2-0 // 3-1 // Please enter your choice => 1. Crash a process 2. Recover a Process 3. Display the Coordinator 4. Status of Processed 5.Exit |
No comments:
Post a Comment