You are on page 1of 14

TRIBHUVAN UNIVERSITY

INSTITUTE OF ENGINEERING
PURWANCHAL CAMPUS
DHARAN-8, SUNSARI NEPAL
Lab Report On
Implementation of Election Algorithm

Submitted by:
Name: Rajani Shrestha
Roll No. : PUR075BCT059
Class: BCT(C) Submitted to:
Department of Computer
Science and Engineering

1
Lab 1: Implementation of Election Algorithm
Aim: To Implement Election algorithm: Bully Algorithm and Ring Algorithm
Prerequisite: Operating System
Outcome: After successful completion of this experiment students will be able to implement the
suitable clock synchronization and election algorithms to manage the resources.

Theory:
Election algorithms is a technique to pick a unique coordinator
Assumption: each process has a unique ID
Goal: find the non‐crashed process with the highest ID
Assumptions: Each process knows the ID and Address of every other process
Communication is reliable. A process initiates an election if it just recovered from failure or it
notices that the coordinator has failed. Three types of messages: Election, Ok, Coordinator.
Several processes can initiate an election simultaneously for consistent result.
1. The Bully Algorithm:
This algorithm applies to system where every process can send a message to every other
process in the system
Algorithm: Suppose process P sends a message to the coordinator.
1. If coordinator does not respond to it within a time interval T, then it is assumed that
coordinator has failed.
2. Now process P sends election message to every process with high priority number.
3. It waits for responses, if no one responds for time interval T then process P elects itself as a
coordinator.
4. Then it sends a message to all lower priority number processes that it is elected as their new
coordinator.
5. However, if an answer is received within time T from any other process Q,
 (I) Process P again waits for time interval T’ to receive another message from Q that it has
been elected as coordinator.
 (II) If Q doesn’t responds within time interval T’ then it is assumed to have failed and
algorithm is restarted.

2
2. The Ring Algorithm:
This algorithm applies to systems organized as a ring(logically or physically). In this algorithm
we assume that the link between the process are unidirectional and every process can message
to the process on its right only. Data structure that this algorithm uses is active list, a list that
has priority number of all active processes in the system.

Algorithm:
1. If process P1 detects a coordinator failure, it creates new active list which is empty initially. It
sends election message to its neighbour on right and adds number 1 to its active list.
2. If process P2 receives message elect from processes on left, it responds in 3 ways:
 (I) If message received does not contain 1 in active list then P1 adds 2 to its active list and
forwards the message.
 (II) If this is the first election message it has received or sent, P1 creates new active list with
numbers 1 and 2. It then sends election message 1 followed by 2.
 (III) If Process P1 receives its own election message 1 then active list for P1 now contains
numbers of all the active processes in the system. Now Process P1 detects highest priority
number from list and elects it as the new coordinator.

Source Code:
#include<iostream>
using namespace std;
#include <cstdlib>

#define MAX 20

int list[MAX], n, coordinator;

void bully();
void ring();
void display();
int main()
{
int i, j, fchoice;
cout<<"Enter number of process: ";
cin>>n;

for (i=1;i<=n;i++)

{
cout<<"Enter process "<<i<<" is alive or not(0/1)" ;
cin>>list[i];
3
if(list[i])
coordinator = i;
/* code */
}

display();
do{
cout<<"1.Bully Algorithm\n2.Ring Algorithm\n3.Display\n4.Exit\n";
cout<<"Enter your choice ";
cin>>fchoice;

switch (fchoice)
{
case 1:
bully();
break;

case 2:
ring();
break;

case 3:
display();
break;

case 4:
exit(1);
break;

}
}
while(fchoice !=4);
return 0;

void display()
{
int i;
cout<<"Processes ";
for(i=1;i<=n;i++)

4
cout<<"\t"<<i;
cout<<endl<<"Alive ";
for(i=1;i<=n;i++)
cout<<"\t"<<list[i];
cout<<endl<<"COORDINATOR IS: "<<coordinator<<endl;

void bully()
{
int schoice, crash, actiavte, i, gid, flag, subcoordinator;

do
{
cout<<"1.CRASH\n2.ACTIVATE\n3.DISPLAY\n4.EXIT\n";

cout<<"Enter your choice ";

cin>>schoice;
switch(schoice)

{
case 1:
cout<<"Enter process to crash ";
cin>>crash;

if(list[crash])
list[crash]=0;
else
cout<<"Process"<<crash<<"Is already dead!"<<endl;
do
{

cout<<"Enter election generator id ";


cin>>gid;

if(gid==coordinator)
cout<<"Enter a valid gebnerator id"<<endl;
}
while(gid == coordinator);
flag = 0;

5
if (crash == coordinator)
{
for(i=gid+1;i<=n;i++)
{
cout<<"Message is sent from "<<gid<<" to "<<i<<endl;
if (list[i])
{
subcoordinator = i;
cout<<"Response is sent from "<<i<<" to "<<gid<<endl;
flag = 1;

}
}

if (flag==1)
coordinator = subcoordinator;
else
coordinator=gid;

display();
break;

case 2:
cout<<"Enter prosess ID to be activated " ;
cin>>actiavte;

if(!list[actiavte])
{
list[actiavte] = 1;
}
else
{
cout<<"Process "<<actiavte<<"is already alive!"<<endl;
break;
}

if(actiavte == n)
{
coordinator=n;
6
break;
}

flag=0;

for(i=actiavte+1;i<=n;i++)
{
cout<<"Message is sent from "<<actiavte<<" to "<<i<<endl;

if(list[i])
{
subcoordinator = i;
cout<<"Response is sent from "<<i<<" to "<<actiavte;
flag=1;
}
}

if(flag == 1)
coordinator = subcoordinator;

else
coordinator=actiavte;
display();
break;

case 3:
display();
break;

case 4:
break;
}
}

while(schoice!=4);
}

void ring()
{

7
int message[20],ring_n,k,i;
int ch,crash,activate,gid,flag,subcoordinator;
do
{
cout<<"\n1.Crash\n2.Activate\n3.Display\n4.Exit\nEnter You choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter Process no. to Crash: ";
cin>>crash;
if(list[crash])
list[crash]=0;
else
{
cout<<"\nProcess is alreaady dead!!";
break;
}
do
{
cout<<"\nEnter election generator id: ";
cin>>gid;
if(gid==coordinator)
{
cout<<"\nenter a valid generator id: ";
}
}while(gid==crash);
flag=0;
k=1;
if(crash==coordinator)
{
message[k++]=gid;
for(i=(gid+1)%n;i!=gid;i=(i+1)%n)
{
if(list[i])
{
cout<<"Message is sent to "<<i<<endl;
message[k++]=i;

}
}
subcoordinator=0;
for(i=1;i<k;i++)
{

8
cout<<"message: "<<message[i]<<endl;
if(subcoordinator<message[i])
{
subcoordinator=message[i];
}
}
coordinator=subcoordinator;
}
display();
break;

case 2:
//activate
cout<<"\nEnter Process no. to Activated: ";
cin>>activate;
if(!list[activate])
list[activate]=1;
else
{
cout<<"\nProcess is alreaady alive!!";
break;
}
//
if(activate==n)
{
coordinator=n;
break;
}
for(i=activate+1;i<=n;i++)
{
cout<<"\nmessage is sent from " <<activate<<" to "<<i<<endl;
if(list[i])
{
subcoordinator=i;
cout<<"Response is sent from "<<i<<" to " <<activate<<endl;
flag=1;
}

9
}
if(flag==1)
{
coordinator=subcoordinator;
}
else
{
coordinator=activate;
}

display();
break;

case 3:
display();
break;

case 4:
break;
}
}while(ch!=4);

Output:
Enter number of process: 5
Enter process 1 is alive or not(0/1)1
Enter process 2 is alive or not(0/1)1
Enter process 3 is alive or not(0/1)1
Enter process 4 is alive or not(0/1)0
Enter process 5 is alive or not(0/1)1
Processes 1 2 3 4 5
Alive 1 1 1 0 1
COORDINATOR IS: 5
1.Bully Algorithm
2.Ring Algorithm

10
3.Display
4.Exit
Enter your choice 1
1.CRASH
2.ACTIVATE
3.DISPLAY
4.EXIT
Enter your choice 1
Enter process to crash 5
Enter election generator id 1
Message is sent from 1 to 2
Response is sent from 2 to 1
Message is sent from 1 to 3
Response is sent from 3 to 1
Message is sent from 1 to 4
Message is sent from 1 to 5
Processes 1 2 3 4 5
Alive 1 1 1 0 0
COORDINATOR IS: 3
1.CRASH
2.ACTIVATE
3.DISPLAY
4.EXIT
Enter your choice 3
Processes 1 2 3 4 5
Alive 1 1 1 0 0
COORDINATOR IS: 3
1.CRASH
2.ACTIVATE
3.DISPLAY
4.EXIT
Enter your choice 2

11
Enter prosess ID to be activated 4
Message is sent from 4 to 5
Processes 1 2 3 4 5
Alive 1 1 1 1 0
COORDINATOR IS: 4
1.CRASH
2.ACTIVATE
3.DISPLAY
4.EXIT
Enter your choice 4
1.Bully Algorithm
2.Ring Algorithm
3.Display
4.Exit
Enter your choice 2

1.Crash
2.Activate
3.Display
4.Exit
Enter You choice: 1

Enter Process no. to Crash: 4

Enter election generator id: 1


Message is sent to 2
Message is sent to 3
message: 1
message: 2
message: 3
Processes 1 2 3 4 5
Alive 1 1 1 0 0

12
COORDINATOR IS: 3

1.Crash
2.Activate
3.Display
4.Exit
Enter You choice: 2

Enter Process no. to Activated: 5

1.Crash
2.Activate
3.Display
4.Exit
Enter You choice: 3
Processes 1 2 3 4 5
Alive 1 1 1 0 1
COORDINATOR IS: 5

1.Crash
2.Activate
3.Display
4.Exit
Enter You choice: 4
1.Bully ALgorithm
2.Ring Algorithm
3.Display
4.Exit
Enter your choice 4
PS C:\Users\RAJANI\Desktop\distributedlab>

13
Analysis:
Initially, among the five processes, process with ID:5 is the one with the highest ID, so it
is selected as a leader. After that, the process with ID:5 crashes and since process with ID:4 is
dead, process with ID:3 is selected as leader. After some time, the process with ID:4 activates
and calls for election. Since, process with ID:5 is dead, the process with ID:4 is selected as
leader.

Conclusion:
Hence, in this lab, we successfully simulated the bully algorithm and ring algorithm for
leader election in distributed system.

14

You might also like