Program:
[Link]
import [Link];
import [Link];
import [Link];
public class Bully {
static Scanner sc = new Scanner([Link]);
public static void main(String[] args) {
[Link]("Enter No. of Processes:- ");
int NoofPro = [Link]();
ArrayList<Integer> ReadyPro = new ArrayList<>();
ArrayList<Integer> CrashedPro = new ArrayList<>();
for (int i = 1; i <= NoofPro; i++) {
[Link](i);
}
[Link](ReadyPro);
while (true) {
[Link]("Choose the below");
[Link]("1. Current Co-ordinator");
[Link]("2. Crash Process");
[Link]("3. Revoke Process");
[Link]("4. Exit");
[Link]("Enter the Number:");
int value = [Link]();
switch (value) {
case 1:
NewCo(ReadyPro);
break;
case 2:
CrashPro(ReadyPro, CrashedPro);
break;
case 3:
RevokePro(ReadyPro, CrashedPro);
break;
case 4:
[Link]();
[Link](0);
break;
default:
[Link]("Invalid input. Please enter a valid option.");
break;
}
}
}
public static void NewCo(ArrayList<Integer> ReadyPro) {
[Link]("The New Co-ordinator is " + [Link](ReadyPro));
}
public static void CrashPro(ArrayList<Integer> ReadyPro, ArrayList<Integer> CrashedPro) {
[Link]("Ready Processes :- " + ReadyPro);
[Link]("Enter No. of Process to Crash:- ");
int NoCrashPro = [Link]();
[Link](NoCrashPro);
[Link]([Link](NoCrashPro));
[Link](NoCrashPro);
NewCo(ReadyPro);
}
public static void RevokePro(ArrayList<Integer> ReadyPro, ArrayList<Integer> CrashedPro) {
[Link]("Crashed Processes :- " + CrashedPro);
[Link]("Enter No. of Process to Revoke :- ");
int NoRevokePro = [Link]();
[Link]([Link](NoRevokePro));
[Link](NoRevokePro);
NewCo(ReadyPro);
}
}
Output:
E:\7th SEM\DC\PRACTICAL\DC-P6(EA)>javac [Link]
E:\7th SEM\DC\PRACTICAL\DC-P6(EA)>java Bully
Enter No. of Processes:- 4
[1, 2, 3, 4]
Choose the below
1. Current Co-ordinator
2. Crash Process
3. Revoke Process
4. Exit
Enter the Number:
1
The New Co-ordinator is 4
Choose the below
1. Current Co-ordinator
2. Crash Process
3. Revoke Process
4. Exit
Enter the Number:
2
Ready Processes :- [1, 2, 3, 4]
Enter No. of Process to Crash:- 2
2
The New Co-ordinator is 4
Choose the below
1. Current Co-ordinator
2. Crash Process
3. Revoke Process
4. Exit
Enter the Number:
3
Crashed Processes :- [2]
Enter No. of Process to Revoke :- 2
The New Co-ordinator is 4
Choose the below
1. Current Co-ordinator
2. Crash Process
3. Revoke Process
4. Exit
Enter the Number:
4