please give me the java code for bully algorithm
import java.util.*;
class Bully1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=0;
while(n==0)
{
System.out.println("enter number of processes");
n=sc.nextInt();
if(n==0)
System.out.println("dude, process cannot be 0 ");
}
int pid[]=new int[n];
for(int i=0;i<n;i++)
{
pid[i]=1;
}
/*for(int k:pid)
System.out.println(k);*/
int max=Max.max(pid,0);
int cord=max;
System.out.println(":cord"+(cord));
//crash-=1;
while(true)
{
System.out.println("enter option:\n1.crash a process\n2.revoke\n3.exit");
int choice =sc.nextInt();
switch(choice)
{
case 1:
System.out.println("enter process to be crash");
int crash=sc.nextInt();
try
{
if(pid[crash]==0)
{
System.out.println("process already crashed");
}
else
{
pid[crash]=0;
System.out.println("enter the process who will initiate election");
int start=sc.nextInt();
//revoke-=1;
if(pid[start]==0)
{
System.out.println("process dead");
continue;
}
else
{
System.out.println("nold coordinator :"+cord);
cord=Max.max(pid,start);
System.out.println("new coordinator :"+cord);
}
}
}
catch(ArrayIndexOutOfBoundsException d)
{
System.out.println("does not exist"+d);
}
catch(Exception e)
{
System.out.println(e);
}
break;
case 2:
try
{
System.out.println("enter process u want to revoke");
int revoke=sc.nextInt();
if(pid[revoke]==1)
{
System.out.println("dude,process already alive");
}
else
{
pid[revoke]=1;
if(cord<revoke)
cord=revoke;
System.out.println("new coordinator is "+cord);
}
}
catch(ArrayIndexOutOfBoundsException d)
{
System.out.println("does not exist"+d);
}
catch(Exception e)
{
System.out.println(e);
}
break;
case 3:
return;
default:System.out.println("wrong choice");
break;
}
}
}
}
class Max
{
public static int max(int pid[],int k)
{
int max=0;
for(int i=k;i<pid.length;i++)
{
System.out.println("message received by process"+i);
if(pid[i]==1)
{
max=i;
}
}
return max;
}
}
Posts: 8,059
Threads: 1
Joined: Mar 2014
To get full information or details of bully algorithm in java source code please have a look on the pages
http://studentbank.in/report-bully-algor...ource-code
if you again feel trouble on bully algorithm in java source code please reply in that page and ask specific fields in bully algorithm in java source code
Posts: 8,059
Threads: 1
Joined: Mar 2014
To get full information or details of bully algorithm in java source code please have a look on the pages
http://studentbank.in/report-bully-algor...ource-code
if you again feel trouble on bully algorithm in java source code please reply in that page and ask specific fields in bully algorithm in java source code