source code for ring election algorithm in java
#1

Please send me the source code for ring election algorithm in java
Reply
#2
Any process warns that the coordinator is not working, builds an ELECTION message that contains its own process number and sends the message to its successor. If the successor is down, the sender jumps over the successor and moves to the next number along the ring, or the next one, until a running process is found. At each step, the sender adds his or her own process number to the message list.


Eventually, the message returns to the process that started it all. This process recognizes this event when it receives an incoming message that contains its own process number. At that time, the type of message is changed to COORDINATOR and is circulated once again, this time to inform everyone else who is the coordinator (designated by the list member with the highest number) and who are the members Of the new ring. When this message has been circulated once, it is deleted and everyone goes back to work.

The workbench shows four processes. There are two queues associated with each process: a queue of requests received from other processes and a queue of requests waiting for recognition of other processes. The processes are color coded as follows: red means a noncoordinating process alive, purple indicates a coordinator and gray represents a process that is down. Each non-coordinator process sends requests (circles) to the coordinator and waits for an acknowledgment before sending the next request. Acknowledgment messages are indicated by circles within rectangles. If the acknowledgment does not arrive at a certain waiting time, the process initiates an election by sending ELECTION messages (bars).

The simulation can be controlled step by step by pressing the buttons corresponding to each process. These buttons can cause individual processes to send requests, fail, or recover. The simulation can also be run in the animation mode in which the processes operate simultaneously and randomly raise and lower. The animation speed can be controlled by the "Slow Ratio" at the top of the demonstration. Moving the slide to the right will slow down the animation.

Code is given below:

Quote:#include<string.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
struct rr
{
int index;
int id;
int f;
char state[10];
}proc[10];
int i,j,k,m,n;
void main()
{
int temp;
char str[10];
cout<<"\n enter the number of process\t";
cin>>n;
for(i=0;i<n;i++)
{
proc[i].index;
cout<<"\n enter id of process\t";
cin>>proc[i].id;
strcpy(proc[i].state,"active");
proc[i].f=0;
}
// sorting
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(proc[j].id>proc[j+1].id)
{
temp=proc[j].id;
proc[j].id=proc[j+1].id;
proc[j+1].id=temp;

}
}
}
for(i=0;i<n;i++)
printf("[%d] %d\t",i,proc[i].id);
int init;
int ch;
int temp1;
int temp2;
int arr[10];
strcpy(proc[n-1].state,"inactive");
cout<<"\nprocess "<<proc[n-1].id<<" select as coordinator";
while(1)
{
cout<<"\n1)election 2)quit\n";
scanf("%d",&ch);
for(i=0;i<n;i++)
{
proc[i].f=0;
}
switch(ch)
{
case 1:
cout<<"\nenter the process Number who intialised election";
scanf("%d",&init);
 temp2=init;
 temp1=init+1;
 i=0;
 while(temp2!=temp1)
 {
 if(strcmp(proc[temp1].state,"active")==0 && proc[temp1].f==0 )
 {
 cout<<"process "<<proc[init].id<<"send message to "<<proc[temp1].id<<"\n";
 proc[temp1].f=1;
 init=temp1;
 arr[i]=proc[temp1].id;
 i++;
 }
 if(temp1==n)
 temp1=0;
 else
 temp1++;

 }

 cout<<"process "<<proc[init].id<<"send message to "<<proc[temp1].id<<"\n";
  arr[i]=proc[temp1].id;
 i++;
 int max=-1;
 for(j=0;j<i;j++)
 {
 if(max<arr[j])
 max=arr[j];

 }
 cout<<"\nprocess "<<max<<" select as coordinator";
 for(i=0;i<n;i++)
 {
 if(proc[i].id==max)
 {
 strcpy(proc[i].state,"inactive");
// cout<<"\n"<<i<<" "<<proc[i].id<<"deactivate\n";
 } }
 break;
break;
}
}
}

Output ::

 enter the number of process    7

 enter id of process    2

 enter id of process    3

 enter id of process    4

 enter id of process    7

 enter id of process    5

 enter id of process    1

 enter id of process    8
[0] 1   [1] 2   [2] 3   [3] 4   [4] 5   [5] 7   [6] 8
process 8 select as coordinator
1)election 2)quit
1

enter the process Number who intialised election 3
process 4send message to 5
process 5send message to 7
process 7send message to 1
process 1send message to 2
process 2send message to 3
process 3send message to 4

process 7 select as coordinator
1)election 2)quit

BULLY ALGORITHM ::
Program ::
#include<stdio.h>
#include<string.h>
#include<iostream.h>
#include<stdlib.h>
struct rr
{
char name[10];
int prior;
char state[10];
}proc[10];
int i,j,k,l,m,n;
void main()
{
cout<<"\n enter the number of proceess \t";
scanf("%d",&n);
for(i=0;i<n;i++)
{
cout<<"\nenter the name of process\t";
cin>>proc[i].name;
cout<<"\nenter the priority of process\t";
cin>>proc[i].prior;
strcpy(proc[i].state,"active");
}


for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(proc[j].prior<proc[j+1].prior)
{
char ch[5];
int t=proc[j].prior;
proc[j].prior= proc[j+1].prior;
proc[j+1].prior=t;
strcpy(ch,proc[j].name);
strcpy(proc[j].name,proc[j+1].name);
strcpy(proc[j+1].name,ch);
}
}
}
int min=0;
for(i=0;i<n;i++)
cout<<"\n"<<proc[i].name<<"\t"<<proc[i].prior;
for(i=0;i<n;i++)
{
for(i=0;i<n;i++)
{
if(min<proc[i].prior)
min=proc[i].prior;
}
}
for(i=0;i<n;i++)
{
if(proc[i].prior==min)
{
cout<<"\nprocess "<<proc[i].name<<" select aas coordinator";
strcpy(proc[i].state,"iactive");
break;
}
}
int pr;
while(1)
{
int ch;
cout<<"\n1)election\t";
cout<<"\n 2) exit  \t";
cin>>ch;
int max=0;
int ar[20];
k=0;
int fl=0;
switch(ch)
{
case 1: char str[5];
 cout<<"\n 1)intialise election\t";
 cin>>str;
      fl=0;
 l1: for(i=0;i<n;i++)
  {
   if(strcmp(str,proc[i].name)==0)
  {
   pr=proc[i].prior;
  }
   }
 //cout<<"\n"<<pr;
 for(i=0;i<n;i++)
  {
   if(pr<proc[i].prior)
  {
   cout<<"\nprocess "<<str<<" send message to "<<proc[i].name;
  }
  }
 for(i=0;i<n;i++)
  {
   if(pr<proc[i].prior && strcmp(proc[i].state,"active")==0 )
  {
  if(fl==0)
  {
   ar[k]= proc[i].prior;
   k++;
  }
   cout<<"\nprocess "<<proc[i].name<<" send OK message to "<<str;
   if(proc[i].prior>max)
   max=proc[i].prior;


   }
 }
  fl=1;



  if(k!=0)
  {
  k=k-1;
  for(i=0;i<n;i++)
  {
  if(ar[k]==proc[i].prior)
  strcpy(str,proc[i].name);
  }


  goto l1;
  }
  m=0;
   for(j=0;j<n;j++)
  {
  if(proc[j].prior>m && strcmp(proc[j].state,"active")==0 )
  {
  cout<<"\nprocess "<<proc[j].name <<" is select as new coordinator";
  strcpy(proc[j].state,"inactive");
  break;
  }
  }


   for(i=0;i<n;i++)
 {
 if(strcmp(proc[i].state,"active")==0 && proc[j].prior>proc[i].prior)
 {
 cout<<"\nprocess "<<proc[j].name<<" send alert message to "<<proc[i].name;
 }
  }


 break;
case 2:exit(1);

}
}
}

Output ::

 enter the number of proceess   4

enter the name of process       p1

enter the priority of process   4

enter the name of process       p2

enter the priority of process   3

enter the name of process       p3

enter the priority of process   6

enter the name of process       p4

enter the priority of process   1

p3      6
p1      4
p2      3
p4      1
process p3 select aas coordinator
1)election
 2) exit        1

 1)intialise election   p2

process p2 send message to p3
process p2 send message to p1
process p1 send OK message to p2
process p1 send message to p3
process p1 is select as new coordinator
process p1 send alert message to p2
process p1 send alert message to p4
1)election
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page
Popular Searches: election project in java, election management system ppt java, implementation of bully election algorithm in java, peterson leader election algorithm, program for bully election algorithm in java, java code for ring algorithm, election algorithm program in java,

[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  voice vased email system source code 0 3,118 20-04-2021, 07:59 AM
Last Post:
  ns2 source codes free download for hello flood attack 0 2,824 31-10-2018, 02:42 PM
Last Post: Guest
  voice based email for blinds source code 0 704 22-10-2018, 05:12 PM
Last Post: Guest
  source code for blood group detection in matlab 0 6,363 22-10-2018, 10:59 AM
Last Post: Guest
  voice based email for blinds source code 0 692 21-10-2018, 07:58 PM
Last Post: Guest
  voice based email for blinds source code 0 631 21-10-2018, 07:43 PM
Last Post: Guest
  source code for hall ticket generation in php 0 2,025 08-10-2018, 10:00 PM
Last Post: Guest
  program for ticket reservation using multithreading in java 0 1,055 08-10-2018, 10:00 AM
Last Post: Guest
  algorithm of railway reservation system 0 667 02-10-2018, 10:50 PM
Last Post: Guest
  voice based email for blinds source code 0 644 28-09-2018, 02:42 PM
Last Post: Guest

Forum Jump: