DS (Ex 2)
DS (Ex 2)
S
22IZ016
EXERCISE-2
AIM:
To implement bully algorithm and ring algorithm for process election.
1 ) BULLY ALGORITHM:
Algorithm:
2. Start Election: The detecting process (P) sends an election message to
all processes with higher IDs.
4. Leader Announcement: The process with the highest ID eventually wins
the election and broadcasts its leadership to all processes.
Code:
#include <stdio.h>
#include <stdbool.h>
#define MAX_NODES 10
typedef struct {
int id;
bool is_active;
} Node;
int bully_algorithm(Node nodes[], int n, int initiator_id, int *message_count) {
int coordinator_id = -1;
*message_count = 0;
printf("\nNode %d starts the election.\n", initiator_id);
for (int i = 0; i < n; i++) {
if (nodes[i].is_active && nodes[i].id > initiator_id) {
printf("Node %d sends election message to Node %d.\n", initiator_id, nodes[i].id);
(*message_count)++;
}
}
for (int i = 0; i < n; i++) {
if (nodes[i].is_active && nodes[i].id > coordinator_id) {
coordinator_id = nodes[i].id;
}
}
printf("Node %d is elected as the new coordinator.\n", coordinator_id);
return coordinator_id;
}
int main() {
Node nodes[MAX_NODES] = {
{1, true}, {2, true}, {3, true}, {4, true}, {5, false}, {6, true}, {7, true}, {8, true}, {9, true}, {10,
false}
};
int n = 10;
int message_count, coordinator_id;
printf("\n--- Best Case ---");
coordinator_id = bully_algorithm(nodes, n, 9, &message_count);
printf("Messages exchanged: %d\n", message_count);
printf("\n--- Average Case ---");
coordinator_id = bully_algorithm(nodes, n, 5, &message_count);
printf("Messages exchanged: %d\n", message_count);
printf("\n--- Worst Case ---");
coordinator_id = bully_algorithm(nodes, n, 1, &message_count);
printf("Messages exchanged: %d\n", message_count);
return 0;
}
OUTPUT:
2 ) RING ALGORITHM:
Algorithm
1. Detect Failure: A process detects that the leader has failed.
2. Start Election: The detecting process (P) sends an election message with
its ID to its immediate neighbour.
#include <stdio.h>
#include <stdbool.h>
#define MAX_NODES 10
typedef struct {
int id;
bool is_active;
} Node;
int ring_algorithm(Node nodes[], int n, int initiator_index, int *message_count) {
int coordinator_id = -1;
int current_index = initiator_index;
*message_count = 0;
printf("\nNode %d starts the election.\n", nodes[initiator_index].id);
do {
int next_index = (current_index + 1) % n; if (nodes[next_index].is_active) {
printf("Node %d passes its ID (%d) to Node %d.\n", nodes[current_index].id,
nodes[current_index].id, nodes[next_index].id);
(*message_count)++;
if (nodes[next_index].id > coordinator_id) {
coordinator_id = nodes[next_index].id;
}
} current_index = next_index;
} while (current_index != initiator_r_index); printf("Node %d is elected as the new
coordinator.\n",
coordinator_id);
return coordinator_id;
}
int main() {
Node nodes[MAX_NODES] = {
{1, true}, {2, true}, {3, true}, {4, false}, {5, true}, {6, true}, {7, true}, {8, true}, {9, true}, {10,
false}
};
int n = 10;
int message_count, coordinator_id;
printf("\n--- Best Case ---");
coordinator_id = ring_algorithm(nodes, n, 8, &message_count);
printf("Messages exchanged: %d\n", message_count);
printf("\n--- Average Case ---");
coordinator_id = ring_algorithm(nodes, n, 5, &message_count);
printf("Messages exchanged: %d\n", message_count);
printf("\n--- Worst Case ---");
coordinator_id = ring_algorithm(nodes, n, 0, &message_count); // Node 1 initiates
printf("Messages exchanged: %d\n", message_count);
return 0;
}
OUTPUT:
RESULT:
Bully algorithm and Ring algorithm for process election has been executed
successfully.