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

10.election Algorithm

The document describes the bully algorithm for electing a coordinator in a distributed system. The bully algorithm selects the process with the highest identifier as the coordinator. When a process wants to become coordinator, it sends an election message to all processes with higher IDs. If none of those processes respond, it becomes coordinator. Otherwise, the responding process with the highest ID becomes coordinator. The algorithm ensures exactly one process acts as coordinator at any given time through peer-to-peer communication between processes.

Uploaded by

Shashank Gosavi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
228 views

10.election Algorithm

The document describes the bully algorithm for electing a coordinator in a distributed system. The bully algorithm selects the process with the highest identifier as the coordinator. When a process wants to become coordinator, it sends an election message to all processes with higher IDs. If none of those processes respond, it becomes coordinator. Otherwise, the responding process with the highest ID becomes coordinator. The algorithm ensures exactly one process acts as coordinator at any given time through peer-to-peer communication between processes.

Uploaded by

Shashank Gosavi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Aim: To implement Election Algorithm. Theory: Election Algorithm: We often need one process to act as a coordinator.

It may not matter which process does this, but there should be group agreement on only one. An assumption in election algorithms is that all processes are exactly the same with no distinguishing characteristics. Each process can obtain a unique identifier (for example, a machine address and process ID) and each process knows of every other process but does not know which is up and which is down. The coordinator election problem is to choose a process from among a group of processes on different processors in a distributed system to act as the central coordinator. An election algorithm is an algorithm for solving the coordinator election problem. By the nature of the coordinator election problem, any election algorithm must be a distributed algorithm.

a group of processes on different machines need to choose a coordinator Peer to peer communication: every process can send messages to every other process. Assume that processes have unique IDs, such that one is highest Assume that the priority of process Pi is i

Bully algorithm The bully algorithm selects the process with the largest identifier as the coordinator. It works as follows: 1. When a process p detects that the coordinator is not responding to requests, it initiates an Election: a. p sends an election message to all processes with higher numbers. b. If nobody responds, then p wins and takes over. c. If one of the processes answers, then p's job is done. 2. If a process receives an election message from a lower-numbered process at any time, it: a. sends an OK message back. b. holds an election (unless its already holding one). 3. A process announces its victory by sending all processes a message telling them that it is the New coordinator. 4. If a process that has been down recovers, it holds an election.

Process 4 holds an election Process 5 and 6 respond, telling 4 to stop Now 5 and 6 each hold an election Conclusion: Thus, we have successfully implement Election Algorithm.

#include<iostream.h> #include<conio.h>

int n,i,co,p,q,r,max,id[10],res[10];

void input() { cout<<"Enter the number of Processes.\n"; cin>>n; cout<<"\nEnter Process ID.\n"; for(i=0;i<n;i++) { cout<<"Enter Process ID for Process "<<(i+1)<<":"; cin>>id[i]; } }

void current() { co=id[0]; for(i=1;i<n;i++) if(co<id[i]) co=id[i]; cout<<"\nCurrent co-ordinator: "<<co<<"\n"; }

void send(int p)

{ cout<<"\n"; for(i=0;i<n;i++) if(id[i]>p) cout<<"Process "<<p<<" sends message to Process "<<id[i]<<"\n"; }

int response() { i=0; cout<<"\n"; while(1) { cout<<"Enter ID of the Process which responds to the message.(0-if no responses)\n"; cin>>r; res[i++]=r; if(r==0) break; } if(res[0]==0) return 0; else { max=res[0]; r=0; i=1; while(res[i]!=0)

if(max<res[i]) { r=i; max=res[i++]; } return res[r]; } }

void main() { clrscr();

input(); current();

cout<<"\nEnter the ID of the process which sends ELECTION message."; cin>>p;

send(p); q=response();

while(1) { if((q==0)||(q==co)) break; else if(q<co)

{ cout<<"\nNow Process "<<q<<" takes over.\n"; p=q; send(p); q=response(); } }

cout<<"\n"; if(q==0) cout<<"Current co-ordinator: "<<p; else cout<<"Current co-ordinator: "<<q;

getch(); }

/*OUTPUT: Enter the number of Processes. 5

Enter Process ID. Enter Process ID for Process 1:1 Enter Process ID for Process 2:2 Enter Process ID for Process 3:3 Enter Process ID for Process 4:4 Enter Process ID for Process 5:5

Current co-ordinator: 5

Enter the ID of the process which sends ELECTION message.2

Process 2 sends message to Process 3 Process 2 sends message to Process 4 Process 2 sends message to Process 5

Enter ID of the Process which responds to the message.(0-if no responses) 3 Enter ID of the Process which responds to the message.(0-if no responses) 0

Now Process 3 takes over.

Process 3 sends message to Process 4 Process 3 sends message to Process 5

Enter ID of the Process which responds to the message.(0-if no responses) 4 Enter ID of the Process which responds to the message.(0-if no responses) 0

Now Process 4 takes over.

Process 4 sends message to Process 5

Enter ID of the Process which responds to the message.(0-if no responses) 0

Current co-ordinator: 4 */

You might also like