50% found this document useful (2 votes)
4K views

Implementation of Election Algorithms

The document discusses two algorithms for leader election in distributed computing systems: 1. The Ring Election Algorithm elects a leader by having each node in a ring compare its unique ID to the ID in an "election message" that propagates around the ring. The node with the highest ID becomes the leader. 2. The Bully Algorithm dynamically selects a coordinator when the existing one fails. A node broadcasts an "election message" to nodes with higher IDs. If it hears nothing, it becomes leader. Otherwise, it waits to see if a higher ID node becomes leader.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
4K views

Implementation of Election Algorithms

The document discusses two algorithms for leader election in distributed computing systems: 1. The Ring Election Algorithm elects a leader by having each node in a ring compare its unique ID to the ID in an "election message" that propagates around the ring. The node with the highest ID becomes the leader. 2. The Bully Algorithm dynamically selects a coordinator when the existing one fails. A node broadcasts an "election message" to nodes with higher IDs. If it hears nothing, it becomes leader. Otherwise, it waits to see if a higher ID node becomes leader.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

9.

IMPLEMENTATION OF ELECTION ALGORITHMS

In distributed computing, leader election is the process of


designating a single process as the organizer of some task
distributed among several computers (nodes). Before the task is
begun, all network nodes are unaware which node will serve as the
"leader," or coordinator, of the task. After a leader election
algorithm has been run, however, each node throughout the
network recognizes a particular, unique node as the task leader.
The network nodes communicate among themselves in order to
decide which of them will get into the "leader" state. For that, they
need some method in order to break the symmetry among them.
For example, if each node has unique identities, then the nodes
can compare their identities, and decide that the node with the
highest identity is the leader.
The definition of this problem is often attributed to LeLann, who
formalized it as a method to create a new token in a token ring
network in which the token has been lost.
Leader election algorithms are designed to be economical in terms
of total bytes transmitted, and time. The algorithm suggested by
Gallager, Humblet, and Spira for general undirected graphs has
had a strong impact on the design of distributed algorithms in
general, and won the Dijkstra Prize for an influential paper in
distributed computing.

a. RING ELECTION ALGORITHM:


The algorithm assumes that each process has a Unique
Identification (UID) and that the processes can arrange themselves
in a unidirectional ring with a communication channel going to the
clockwise and anticlockwise neighbour. The 2 part algorithm can
be described as follows:
1. Initially each process in the ring is marked as non-
participant.
2. A process that notices a lack of leader starts an election.
It marks itself as participant and creates an election
message containing its UID. It then sends this message
clockwise to its neighbour.
3. When a process receives an election message it
compares the UID with its own, if the current process has a
larger UID it replaces the one in the election message with its
UID. The process then marks itself as participant and again
forwards the election message in a clockwise direction.
4. If the process was already marked as participant when it
receives an election message the procedure is different. In
this case it will compare the UID as before but only forward
the election message if it has needed to replace the UID.

The algorithm finishes when a process receives an election


message containing its own UID. Then the second stage of the
algorithm takes place

1. This process marks itself as non-participant and sends


an elected message to its neighbour announcing its election
and UID.
2. When a process receives an elected message it marks
itself as non-participant records the elected UID and again
forwards the elected message.
3. When the elected message reaches the newly elected
process the election is over.

Assuming there are no failures this algorithm will finish.


For Example:
Let’s assume that we have 7 nodes:
At this current moment in time, node ‘7’ is the coordinator,
because it is the highest numbered node. But then node ‘7’ leaves,
so who is now the coordinator? Lets say node ‘4’ decides it wants
to be the coordinator, then we have:

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.

When a process P determines that the current coordinator is down


because of message timeouts or failure of the coordinator to
initiate a handshake, it performs the following sequence of actions:

1. P broadcasts an election message (inquiry) to all other


processes with higher process IDs.

2. If P hears from no process with a higher process ID than it, it


wins the election and broadcasts victory.

3. If P hears from a process with a higher ID, P waits a certain


amount of time for that process to broadcast itself as the
leader. If it does not receive this message in time, it re-
broadcasts the election message.

Note that if P receives a victory message from a process with a


lower ID number, it immediately initiates a new election. This is
how the algorithm gets its name - a process with a higher ID
number will bully a lower ID process out of the coordinator position
as soon as it comes online.

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

You might also like