Implementation of Election Algorithms
Implementation of Election Algorithms
Here, node ‘4’ has sent a ELECTION to nodes ‘5’, ‘6’ and ‘7’, this is
because none of the nodes know that node ‘7’ has left yet. In this
case, node ‘7’ cannot reply, yet, nodes ‘5’, and ‘6’ are still here, so
they reply to ‘4’ that they are still here:
Now we have nodes ‘5’ and ‘6’ sending out elections. In this case,
node ‘5’ sends an election to node ‘6’ and ‘7’. Whereas node ‘6’
just sends an election to node ‘7’:
Now, since node ‘6’ is still here, then node ‘6’ tells node ‘5’ that it
is still here:
Finally, since node ‘7’ has not replied to node ‘6’, this implies that
node ‘7’ no longer exists, meaning that node ‘6’ is the new
coordinator. So node ‘6’ tells all the other nodes:
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int main()
{ clrscr();
int node_state[6]={1,1,1,1,1,1};
int ring_nodes[6]={9};
int cord = 6;
int req_node;
int new_req;
int new_del;
int new_act_node;
int fin_req=0;
int oneTimeFlag=0;
int i, k, x=99, m=0, n_req=0;
//destroying the coordinator
node_state[5]=0;
cord=9;
printf("The node 6 has broke down, Have to relect a new coordinator using
ring algorithm.......\n");
getch();
while(fin_req!=999)
{
if(cord==9)
{
//getting request for node
srand ( time(NULL) );
req_node = (rand() % 7);
if(req_node==0)
req_node=1;
if(node_state[req_node-1]==0)
{
//take the node to the right or if node6 is there then take 5
if(req_node==6)
req_node=5;
else
req_node+=1;
}
printf("\nThe random node requesting permission is, Node :
%d",req_node);
getch();
//electing the new coordinator
cord=req_node;
printf("\nThe cordinator node is : %d",cord);
getch();
ring_nodes[0]=req_node;
printf("\nThe ring nodes is : %d", ring_nodes[0]);
getch();
k=1;
n_req = (req_node+1);
while(x!=req_node)
{
printf("\nEntered loop with x : %d and n_req : %d",x, n_req);
getch();
if(n_req==7)
{
n_req=1;
printf("\nEntered node 6 loop with n_req is : %d",cord);
getch();
}
if(node_state[n_req-1]==1)
{
printf("\nEntered node stated checking and ring_nodes
setting loop, k : %d",k);
getch();
ring_nodes[k]=n_req;
}
else
{
n_req++;
continue;
}
x=n_req;
n_req++;
k++;
}
for(m=0;m<5;m++)
{
if(ring_nodes[m+1]>ring_nodes[m])
{
cord=m+1;
}
}
printf("\nThe new relected coordinator is, Node : %d", cord);
//bringing up a new node
srand ( time(NULL) );
new_req = (rand() % 7);
if(new_req==0)
new_req=1;
node_state[new_req-1]=1;
printf("\nThe node : %d is up and running....\n", new_req);
getch();
if(new_req>cord)
{
printf("\nThe subsequent coordinator is, Node : %d",new_req);
getch();
cord=new_req;
}
}
//else
//{
//bringing up a new node
srand ( time(NULL) );
new_req = (rand() % 7);
if(new_req==0)
new_req=1;
node_state[new_req-1]=1;
printf("\nThe node : %d is up and running....\n", new_req);
getch();
if(new_req>cord)
{
printf("\nThe subsequent coordinator is, Node : %d",new_req);
getch();
cord=new_req;
}
else
{
printf("\nThe coordinator for this process is, Node : %d",cord);
getch();
}
//}
//Destroying a new node
srand ( time(NULL) );
new_del = (rand() % 7);
if(new_del==0)
new_del=1;
printf("\nThe node : %d has broke down......", new_del);
node_state[new_del-1]=0;
if(new_del==cord)
{
printf("\nSubsequently, the coordinator is also down and a new
coordinator must be elected\n");
cord=9;
getch();
}
printf("\nPress 999 to exit or any other key to continue.....");
scanf("%d",&fin_req);
}
getch();
return 0;
}
OUTPUT
b. BULLY ELECTION ALGORITHM:
The bully algorithm is a method in distributed computing for
dynamically selecting a coordinator by process ID number.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int node_state[6]={1,1,1,1,1,1};
int cord = 6;
int req_node;
int new_req;
int new_del;
int new_act_node;
int fin_req=0;
int oneTimeFlag=0;
int i;
//destroying the coordinator
node_state[5]=0;
cord=9;
printf("The node 6 has broke down, Have to relect a new coordinator
.......\n");
getch();
while(fin_req!=999)
{
if(cord==9)
{
//getting request for node
srand ( time(NULL) );
req_node = (rand() % 7);
if(req_node==0)
req_node=1;
if(node_state[req_node-1]==0)
{
//take the node to the right or if node6 is there then take 5
if(req_node==6)
req_node=5;
else
req_node+=1;
}
printf("\nThe random node requesting permission is, Node :
%d",req_node);
getch();
//electing the new coordinator
cord=req_node;
for(i=req_node; i<6; i++)
{
//check state
if(node_state[i]==1)
cord=i+1;
else
continue;
}
printf("\nThe new relected coordinator is, Node : %d", cord);
//bringing up a new node
srand ( time(NULL) );
new_req = (rand() % 7);
if(new_req==0)
new_req=1;
node_state[new_req-1]=1;
printf("\nThe node : %d is up and running....\n", new_req);
getch();
if(new_req>cord)
{
printf("\nThe subsequent coordinator is, Node : %d",new_req);
getch();
cord=new_req;
}
}
//else
//{
//bringing up a new node
srand ( time(NULL) );
new_req = (rand() % 7);
if(new_req==0)
new_req=1;
node_state[new_req-1]=1;
printf("\nThe node : %d is up and running....\n", new_req);
getch();
if(new_req>cord)
{
printf("\nThe subsequent coordinator is, Node : %d",new_req);
getch();
cord=new_req;
}
else
{
printf("\nThe coordinator for this process is, Node : %d",cord);
getch();
}
//}
//Destroying a new node
srand ( time(NULL) );
new_del = (rand() % 7);
if(new_del==0)
new_del=1;
printf("\nThe node : %d has broke down......", new_del);
node_state[new_del-1]=0;
if(new_del==cord)
{
printf("\nSubsequently, the coordinator is also down and a new
coordinator must be elected\n");
cord=9;
getch();
}
printf("\nPress 999 to exit or any other key to continue.....");
scanf("%d",&fin_req);
}
getch();
return 0;
}
OUTPUT