Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class BullyAlgorithm {
- static Scanner sc = new Scanner(System.in);
- static boolean isCrashed[];
- static void holdElection(int elector) {
- System.out.println("Enter the process to start election: ");
- int flag = -1;
- for (int i = elector; i < isCrashed.length; i++) {
- if (!isCrashed[i]) {
- System.out.println("OK message from process " + i);
- flag = i;
- }
- }
- if (flag == -1) {
- System.out.println("The new Co Ordinator is process " + elector);
- } else {
- System.out.println("The new Co Ordinator is process " + flag);
- }
- }
- public static void main(String args[]) {
- int choice;
- System.out.println("Enter the number of processes: ");
- isCrashed = new boolean[sc.nextInt()];
- do {
- System.out.println("***Bully Algorithm***");
- System.out.println("1. Crash Process\n2. Recover Process\n3. Exit");
- choice = sc.nextInt();
- switch (choice) {
- case 1:
- System.out.println("Enter the process to crash: ");
- isCrashed[sc.nextInt()] = true;
- System.out.println("Enter the process to start election");
- holdElection(sc.nextInt());
- break;
- case 2:
- System.out.println("Enter the process to recover");
- int rec = sc.nextInt();
- System.out.println("The recovered process " + rec + " will start election..");
- holdElection(rec);
- break;
- }
- } while (choice != 3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment