This code implements an election algorithm using the bully algorithm. It allows the user to simulate failure, election, and recovery of coordinator processes. The code defines an array to track the status of processes, initializes the coordinator, then enters a loop to accept user input for different events. When election is selected, it sends messages to higher numbered processes, finds the highest alive process to elect as new coordinator.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
66 views
Bully Algo
This code implements an election algorithm using the bully algorithm. It allows the user to simulate failure, election, and recovery of coordinator processes. The code defines an array to track the status of processes, initializes the coordinator, then enters a loop to accept user input for different events. When election is selected, it sends messages to higher numbered processes, finds the highest alive process to elect as new coordinator.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
Experiment No:08
Election Algorithm using Bully
Code: #include<conio.h> #include<stdio.h> int main() { int a[10]; int n; int elect,coord; printf("enter the no of processes \n "); scanf("%d",&n); int c=1; coord=n; for(int i=1;i<=n;i++) { a[i]=1; } while(c>0) { printf("enter the choice: 1.failure 2.election 3.recover \n"); scanf("%d",&c); printf("%d",c); switch(c) { case 2: { a[n]=0; printf("enter the the process who starts the election \n"); scanf("%d",&elect); for(int j=elect;elect<n;elect++) { for(int i=j;i<=n;i++) { printf("message sent to node %d \n",i); } for(int i=elect+1;i<n;i++) { if(a[i]==1) { printf("reply ok %d \n",i); } } } coord=coord-1; printf("new coordinator is %d \n",coord); break; } case 1: {
a[n]=0; printf("coordinator no %d failed \n",n); break; } case 3: { a[n]=1; coord=a[n]; printf("coordinator is %d \n",n); break; } case 4: { c=0; } } } } Output: