0% found this document useful (0 votes)
9 views3 pages

Exp 7

The document outlines the implementation of the Ring Election Algorithm, which is used to elect a coordinator among processes arranged in a ring structure. When a process detects a failure in the coordinator, it initiates an election by sending an ELECTION message to its successor, collecting process numbers along the way. The process with the highest number is then declared the new coordinator and informs all other processes of the change.

Uploaded by

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

Exp 7

The document outlines the implementation of the Ring Election Algorithm, which is used to elect a coordinator among processes arranged in a ring structure. When a process detects a failure in the coordinator, it initiates an election by sending an ELECTION message to its successor, collecting process numbers along the way. The process with the highest number is then declared the new coordinator and informs all other processes of the change.

Uploaded by

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

EXPERIMENT 7

AIM:
Write a program to implement Ring Election Algorithm.

Introduction and Theory :


Another election algorithm is based on the use of a ring, but without a token. We assume that the
processes are physically or logically ordered, so that each process knows who its successor is. When any
process notices that the coordinator is not functioning, it builds an ELECTION message containing its
own process number and sends the message to its successor. If the successor is down, the sender skips
over the successor and goes to the next member along the ring, or the one after that, until a running
process is located. At each step, the sender adds its own process number to the list in the message.
Eventually, the message gets back to the process that started it all. That process recognizes this event
when it receives an incoming message containing its own process number. At that point, the message
type is changed to COORDINATOR and circulated once again, this time to inform everyone else who the
coordinator is (the list member with the highest number) and who the members of the new ring are.
When this message has circulated once, it is removed and everyone goes back to work.

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

int main() {
int n;
cout << "Enter No of Processes : ";
cin >> n;
cout << "Order of Processes in Ring : ";
vector<int> a(n), b(n);
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
cout << "\n";
cout << "Process " << n << " is the Coordinator\n";
cout << "Process " << n << " Fails\n";
vector<int> active_list;
int temp;
for (int i = 0; i < n; i++) {
if (b[i] == n) {
active_list.push_back(a[i]);
temp = i;
cout << "Process " << a[i] << " Starts Election\n";
cout << "Process " << a[i] << " added to Active List\n";

break;
}
}
int count = 0;
int f = 0;
while (count < n) {
temp++;
if (temp >= n)
temp = 0;
for (int i = 0; i < active_list.size(); i++) {
if (b[temp] == active_list[i]) {
int max = 0;
for (int i = 0; i < active_list.size(); i++) {
if (max < active_list[i])
max = active_list[i];
}
cout << "Process " << max << " is new Coordinator\n";
f = 1;
}
if (f == 1)
break;
}
if (f == 1)
break;
active_list.push_back(b[temp]);
cout << "Process " << b[temp] << " added to Active List\n";
count++;
}

return 0;
}

OUTPUT:

You might also like