Bully Algorithm code in Java
Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes, we need specific election methods like bully and ring.
The processes that serve as supervisors are chosen using election algorithms as a single process. When the selected coordinator process crashes for some reason, a new process is selected. The selection algorithms are used to decide the location where the fresh copy of the coordinator must be restarted.
It is assumed that every process in the system has a specific priority number and that the primary priority process would be selected as the new coordinator first. In the event that the current usage coordinator process fails, a new process with the main priority number is chosen. We note that priority number and give it to each running distributed system process.
The following is the Bully election algorithm:
Assume P is a coordinator-sending process that transmits messages.
- When the coordinator doesn't respond within the time interval T, it will assume that the coordinator procedure has failed.
- Process P will send the main priority number and an election message to all running processes.
- In the current method, P will choose itself as a supervisor if it doesn't hear back within the time period T.
- After choosing itself to be the coordinator, it notifies any processes with lower priority that process P has been selected as their new coordinator.
- If process P gets a response from the other processes Q within time T, then it has been chosen as the coordinator by process Q and must wait for time T to obtain another response.
- It is believed to have failed, and the process is repeated if it fails to receive a reaction within time T.
BullyAlgo.java
import java.io.*;
import java.util.Scanner;
// importing the packages
// To comprehend how bullying algorithms function, create the class BullyAlgoExample.
class BullyAlgo{
// Regarding the process and its status, declare variables and arrays.
static int noofprocess;
static int priority[] = newint[100];
static int S[] = newint[100];
static int C;
// main() method of the class
public static void main(String args[]) throws IOException
{
// Taking input for the number of processes from the user
System. out.println("Select the no.of process that are to be done:");
// Object creation for scanner class
Scanner sc = new Scanner(System.in);
noofprocess = sc.nextInt();
int i;
// Setting priorities for the process in the for loop
for(i = 0; i<noofprocess; i++)
{
System.out.println("Status for process "+(i+1)+":");
S[i] = sc.nextInt();
System.out.println("Priority of the current process "+(i+1)+":");
priority[i] = sc.nextInt();
}
System.out.println("Which process have to be elected for initation :");
int e = sc.nextInt();
sc.close();
// call electProcess() method
electProcess(e);
System.out.println("After electing process the final coordinator is "+C);
}
// create electProcess() method
static void electProcess(int e)
{
e = e - 1;
C = e + 1;
for(int i = 0; i<noofprocess; i++)
{
if(priority[e]<priority[i])
{
System.out.println("Election message is sent from "+(e+1)+" to "+(i+1));
if(S[i]==1)
electProcess(i+1);
}
}
}
}
BullyAlgo2.java
// Importing the packages
import java.util.Scanner;
class Process{
public int pid;
public StRound status;
public Process(int pid){
this.pid = pid;
this.status = "active";
}
}
public class BullyAlgo {
Scanner sc;
Process[] Process;
int num;
public BullyAlgoExample2(){
sc= new Scanner(System.in);
}
publicvopid Round(){
System.out.println("Enter total number of Process of Process");
num = sc.nextInt();
Process = new Process[n];
for(int j = 0; j< num; j++){
Process[j]= new Process(j);
}
}
publicvopidperformElection(){
try {
Thread.sleep(100);
} catch (InterruptedException ae) {
ae.printStackTrace();
}
System.out.println("Process having pid "+Process[getMaxValue()].pid+" fails");
Process[getMaxValue()].status = "Inactive";
int currentpid = 0;
boolean oversts = true;
while(oversts){
boolean high = false;
for(int i = currentpid + 1; i< n; i++){
if(Process[i].status == "active"){
System.out.println("Process "+currentpid+" Passes Election("+currentpid+") message to process" +i);
high = true;
}
}
if(high){
for(int i = currentpid + 1; i< n; i++){
if(Process[i].status == "active"){
System.out.println("Process "+i+"Passes Ok("+i+") message to process" +currentpid);
}
}
currentpid++;
}
else{
int cod = Process[getMaxValue()].pid;
System.out.println("Finally Process "+cod+" Becomes codinator");
for(int i = cod - 1; i>= 0; i--){
if(Process[i].status == "active"){
System.out.println("Process "+cod+"Passes codinator("+cod+") message to process " +i);
}
}
System. out.println("End of Election");
oversts = false;
break;
}
}
}
public int getMaxValue(){
int Maxpid = -99;
int Maxpidin = 0;
for(inti = 0; i<Process.length; i++){
if(Process[i].status == "active" && Process[i].pid >Maxpid){
Maxpid = Process[i].pid;
Maxpidin = i;
}
}
return Maxpidin;
}
publicstaticvopid main(StRound[] args) {
BullyAlgoExample2 bully = newBullyAlgoExample2();
bully.Round();
bully.performElection();
}
}
Output:
