The Bully Algorithm is a classic election algorithm used in distributed systems to elect a coordinator (leader) among a group of processes. When a process notices that the current coordinator is no longer responding, it initiates an election. The process with the highest priority among all active (running) processes wins the election and becomes the new coordinator.
This Java program simulates the Bully Algorithm with user-defined processes, each having a status (active or inactive) and a priority value.
Java Program: Bully Algorithm
import java.io.*;
import java.util.Scanner;
class BullyAlgorithm {
// Total number of processes
static int totalProcesses;
// Array storing the priority of each process (1-indexed)
static int[] priority = new int[100];
// Array storing the status of each process: 1 = active, 0 = inactive
static int[] status = new int[100];
// Stores the current coordinator process number
static int coordinator;
public static void main(String args[]) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of processes:");
totalProcesses = scanner.nextInt();
// Read status and priority for each process
for (int processIdx = 0; processIdx < totalProcesses; processIdx++) {
System.out.println("For process " + (processIdx + 1) + ":");
System.out.println("Status (1=active, 0=inactive):");
status[processIdx] = scanner.nextInt();
System.out.println("Priority:");
priority[processIdx] = scanner.nextInt();
}
System.out.println("Which process will initiate the election?");
int initiator = scanner.nextInt();
// Start the election from the initiating process
elect(initiator);
System.out.println("Final coordinator is process " + coordinator);
}
// Recursive election method: process 'initiator' challenges all higher-priority processes
static void elect(int initiator) {
// Convert to 0-indexed
int initiatorIdx = initiator - 1;
// Tentatively set this process as coordinator
coordinator = initiator;
for (int i = 0; i < totalProcesses; i++) {
// Send election message only to processes with higher priority
if (priority[initiatorIdx] < priority[i]) {
System.out.println("Election message sent from process " + initiator + " to process " + (i + 1));
// If that higher-priority process is active, it takes over
if (status[i] == 1) {
elect(i + 1);
}
}
}
}
}
How the Code Works
- Input: The user provides the number of processes. For each process, they enter its status (1 = active, 0 = inactive) and its priority number.
- Election Initiation: The user specifies which process initiates the election. The
elect()method is called with that process number. - Challenge Phase: Inside
elect(), the initiating process sends an election message to every process with a higher priority. The current process tentatively claims the coordinator role. - Takeover: If a higher-priority process is active (
status[i] == 1), it takes over and recursively callselect()itself — challenging processes with even higher priority. - Winner: The process that runs
elect()last without finding any active higher-priority challenger becomes the final coordinator. This is the highest-priority active process in the system.
Sample Input / Output
Enter the number of processes:
7
For process 1:
Status (1=active, 0=inactive):
1
Priority:
1
For process 2:
Status (1=active, 0=inactive):
1
Priority:
2
For process 3:
Status (1=active, 0=inactive):
1
Priority:
3
For process 4:
Status (1=active, 0=inactive):
1
Priority:
4
For process 5:
Status (1=active, 0=inactive):
1
Priority:
5
For process 6:
Status (1=active, 0=inactive):
1
Priority:
6
For process 7:
Status (1=active, 0=inactive):
0
Priority:
7
Which process will initiate the election?
4
Election message sent from process 4 to process 5
Election message sent from process 5 to process 6
Election message sent from process 6 to process 7
Election message sent from process 5 to process 7
Election message sent from process 4 to process 6
Election message sent from process 6 to process 7
Election message sent from process 4 to process 7
Final coordinator is process 6
Output Explanation
- Process 4 initiates the election and challenges processes 5, 6, and 7 (all have higher priority).
- Process 5 is active, so it takes over and challenges processes 6 and 7.
- Process 6 is active, so it challenges process 7.
- Process 7 has the highest priority but is inactive (status = 0), so it does not take over.
- Process 6 — the highest-priority active process — becomes the final coordinator.
See Also
- Demonstrating Deadlock with Resource Allocation — another classic Distributed Computing concept
Conclusion
The Bully Algorithm elegantly solves the leader election problem in distributed systems. By having each process challenge all higher-priority peers and recursively propagating the election upward, the algorithm guarantees that the highest-priority active process always wins. Understanding this algorithm is fundamental to studying fault tolerance and coordination in distributed computing.
thank q so much nice performance sir