0% found this document useful (0 votes)
2 views6 pages

DS (Ex 2)

The document describes the implementation of the Bully and Ring algorithms for process election in distributed systems. The Bully Algorithm elects a leader by having a process send election messages to higher-ID processes, while the Ring Algorithm uses a circular communication method to elect a leader based on the highest ID. Both algorithms are demonstrated with code examples and their execution results.

Uploaded by

nikhildeutsch03
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)
2 views6 pages

DS (Ex 2)

The document describes the implementation of the Bully and Ring algorithms for process election in distributed systems. The Bully Algorithm elects a leader by having a process send election messages to higher-ID processes, while the Ring Algorithm uses a circular communication method to elect a leader based on the highest ID. Both algorithms are demonstrated with code examples and their execution results.

Uploaded by

nikhildeutsch03
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/ 6

JEYANTH.

S
22IZ016

EXERCISE-2

AIM:
To implement bully algorithm and ring algorithm for process election.

1 ) BULLY ALGORITHM:

In a distributed system, multiple processes run on different nodes, and


coordination among them often requires a leader or coordinator. The Bully
Algorithm Garcia-Molina provides an efficient way to elect a leader when the
current leader fails or when a new process with a higher priority joins the system.

Algorithm:

1.​ Detect Failure: A process detects that the leader is unresponsive.

2.​ Start Election: The detecting process (P) sends an election message to
all processes with higher IDs.

3.​ Wait for Response:


○​ If no higher-ID process responds, P declares itself the leader and
informs all other processes.
○​ If a higher-ID process responds, it takes over and initiates its own
election.

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:

The Ring Algorithm is a decentralized approach where processes communicate


in a circular order to elect a leader. This method is particularly useful in systems
where process identifiers are arranged logically in a ring structure.

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.

3.​ Message Passing:


○​ The receiving process forwards the highest ID received so far.
○​ If the message completes a full cycle, the highest-ID process is
elected as the leader.

4.​ Leader Announcement: The elected leader sends a coordinator message


around the ring to inform all processes.
Code:

#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.

You might also like