0% found this document useful (0 votes)
13 views

Program 6

The document outlines a C++ program that implements the Bully Election Algorithm using message passing through procedural calls and return values. It prompts the user for the number of processes and the initiating process for the election, simulating the election process until a new coordinator is declared. The program demonstrates how processes communicate and elect a new leader when the current coordinator fails.

Uploaded by

Hardik Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Program 6

The document outlines a C++ program that implements the Bully Election Algorithm using message passing through procedural calls and return values. It prompts the user for the number of processes and the initiating process for the election, simulating the election process until a new coordinator is declared. The program demonstrates how processes communicate and elect a new leader when the current coordinator fails.

Uploaded by

Hardik Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Program - 6

Aim: Object of the assignment is to implement Bully Election Algorithm by message


passing through procedural calls and return values. Program should take as input list
of process numbers participating in election and the process that will start the
election procedure

Code:

#include<bits/stdc++.h>
using namespace std;

int main(){
int n;
set<int>queue;
cout<<"Enter count of processes: ";
cin>>n;
cout<<endl<<"Processes are: ";
for(int i = 1 ; i <= n ; i++){
cout<<i<<' ';
}
cout<<endl;
cout<<"Process "<<n<<" was the coordinator"<<endl;
cout<<"Process "<<n<<" failed!"<<endl;
cout<<endl;
cout<<"Enter process that starts the election: ";
int t; cin>>t;
cout<<"\nProcess "<<t<<" starts the election"<<endl;
queue.insert(t);
int last_call;
cout<<endl;
while(!queue.empty() && t != n){
cout<<"Process "<<t<<" sends election message to processes: ";
for(int i = t+1 ; i <= n ; i++ ){
queue.insert(i);
cout<<i<<' ';
}
cout<<endl;
queue.erase(t);
last_call = t;
t = *queue.begin();
}
cout<<endl;
cout<<"Process "<<*queue.begin()<<" doesn't respond"<<endl;
cout<<"Process "<<last_call<<" declares itself as the winner"<<endl;
cout<<"Process "<<last_call<<" is the new coordinator"<<endl;
}
Output:

You might also like