Skip to content
Related Articles
Open in App
Not now

Related Articles

Election algorithm and distributed processing

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 24 Jan, 2023
Improve Article
Save Article

Distributed Algorithm is an algorithm that runs on a distributed system. Distributed system is a collection of independent computers that do not share their memory. Each processor has its own memory and they communicate via communication networks. Communication in networks is implemented in a process on one machine communicating with a process on another machine. Many algorithms used in the distributed system require a coordinator that performs functions needed by other processes in the system. 

Election algorithms are designed to choose a coordinator. 

Election Algorithms: Election algorithms choose a process from a group of processors to act as a coordinator. If the coordinator process crashes due to some reasons, then a new coordinator is elected on other processor. Election algorithm basically determines where a new copy of the coordinator should be restarted. Election algorithm assumes that every active process in the system has a unique priority number. The process with highest priority will be chosen as a new coordinator. Hence, when a coordinator fails, this algorithm elects that active process which has highest priority number.Then this number is send to every active process in the distributed system. We have two election algorithms for two different configurations of a distributed system. 

1. The Bully Algorithm – This algorithm applies to system where every process can send a message to every other process in the system. Algorithm – Suppose process P sends a message to the coordinator.

  1. If the coordinator does not respond to it within a time interval T, then it is assumed that coordinator has failed.
  2. Now process P sends an election messages to every process with high priority number.
  3. It waits for responses, if no one responds for time interval T then process P elects itself as a coordinator.
  4. Then it sends a message to all lower priority number processes that it is elected as their new coordinator.
  5. However, if an answer is received within time T from any other process Q,
    • (I) Process P again waits for time interval T’ to receive another message from Q that it has been elected as coordinator.
    • (II) If Q doesn’t responds within time interval T’ then it is assumed to have failed and algorithm is restarted.

2. The Ring Algorithm – This algorithm applies to systems organized as a ring(logically or physically). In this algorithm we assume that the link between the process are unidirectional and every process can message to the process on its right only. Data structure that this algorithm uses is active list, a list that has a priority number of all active processes in the system. 

Algorithm –

  1. If process P1 detects a coordinator failure, it creates new active list which is empty initially. It sends election message to its neighbour on right and adds number 1 to its active list.
  2. If process P2 receives message elect from processes on left, it responds in 3 ways:
    • (I) If message received does not contain 1 in active list then P1 adds 2 to its active list and forwards the message.
    • (II) If this is the first election message it has received or sent, P1 creates new active list with numbers 1 and 2. It then sends election message 1 followed by 2.
    • (III) If Process P1 receives its own election message 1 then active list for P1 now contains numbers of all the active processes in the system. Now Process P1 detects highest priority number from list and elects it as the new coordinator.

Java




import java.util.Scanner;
 
public class GFG {
   
    class Pro {
        int id;
        boolean act;
        Pro(int id)
        {
            this.id = id;
            act = true;
        }
    }
    int TotalProcess;
    Pro[] process;
    public GFG() { }
    public void initialiseGFG()
    {
        System.out.println("No of processes 5");
        TotalProcess = 5;
        process = new Pro[TotalProcess];
        int i = 0;
        while (i < process.length) {
            process[i] = new Pro(i);
            i++;
        }
    }
    public void Election()
    {
        System.out.println("Process no "
                           + process[FetchMaximum()].id
                           + " fails");
        process[FetchMaximum()].act = false;
        System.out.println("Election Initiated by 2");
        int initializedProcess = 2;
 
        int old = initializedProcess;
        int newer = old + 1;
 
        while (true) {
            if (process[newer].act) {
                System.out.println(
                    "Process " + process[old].id
                    + " pass Election(" + process[old].id
                    + ") to" + process[newer].id);
                old = newer;
            }
 
            newer = (newer + 1) % TotalProcess;
            if (newer == initializedProcess) {
                break;
            }
        }
 
        System.out.println("Process "
                           + process[FetchMaximum()].id
                           + " becomes coordinator");
        int coord = process[FetchMaximum()].id;
 
        old = coord;
        newer = (old + 1) % TotalProcess;
 
        while (true) {
 
            if (process[newer].act) {
                System.out.println(
                    "Process " + process[old].id
                    + " pass Coordinator(" + coord
                    + ") message to process "
                    + process[newer].id);
                old = newer;
            }
            newer = (newer + 1) % TotalProcess;
            if (newer == coord) {
                System.out.println("End Of Election ");
                break;
            }
        }
    }
    public int FetchMaximum()
    {
        int Ind = 0;
        int maxId = -9999;
        int i = 0;
        while (i < process.length) {
            if (process[i].act && process[i].id > maxId) {
                maxId = process[i].id;
                Ind = i;
            }
            i++;
        }
        return Ind;
    }
 
    public static void main(String arg[])
    {
        GFG object = new GFG();
        object.initialiseGFG();
        object.Election();
    }
}

C++




#include <iostream>
#include <vector>
#include <algorithm>
 
struct Pro {
  int id;
  bool act;
  Pro(int id) {
    this->id = id;
    act = true;
  }
};
 
class GFG {
 public:
  int TotalProcess;
  std::vector<Pro> process;
  GFG() {}
  void initialiseGFG() {
    std::cout << "No of processes 5" << std::endl;
    TotalProcess = 5;
    process.reserve(TotalProcess);
    for (int i = 0; i < process.capacity(); i++) {
      process.emplace_back(i);
    }
  }
  void Election() {
    std::cout << "Process no " << process[FetchMaximum()].id << " fails" << std::endl;
    process[FetchMaximum()].act = false;
    std::cout << "Election Initiated by 2" << std::endl;
    int initializedProcess = 2;
 
    int old = initializedProcess;
    int newer = old + 1;
 
    while (true) {
      if (process[newer].act) {
        std::cout << "Process " << process[old].id << " pass Election(" << process[old].id << ") to" << process[newer].id << std::endl;
        old = newer;
      }
 
      newer = (newer + 1) % TotalProcess;
      if (newer == initializedProcess) {
        break;
      }
    }
 
    std::cout << "Process " << process[FetchMaximum()].id << " becomes coordinator" << std::endl;
    int coord = process[FetchMaximum()].id;
 
    old = coord;
    newer = (old + 1) % TotalProcess;
 
    while (true) {
 
      if (process[newer].act) {
        std::cout << "Process " << process[old].id << " pass Coordinator(" << coord << ") message to process " << process[newer].id << std::endl;
        old = newer;
      }
      newer = (newer + 1) % TotalProcess;
      if (newer == coord) {
        std::cout << "End Of Election " << std::endl;
        break;
      }
    }
  }
  int FetchMaximum() {
    int Ind = 0;
    int maxId = -9999;
    for (int i = 0; i < process.size(); i++) {
      if (process[i].act && process[i].id > maxId) {
        maxId = process[i].id;
        Ind = i;
      }
    }
    return Ind;
  }
};
 
int main() {
  GFG object;
  object.initialiseGFG();
  object.Election();
  return 0;
}

Output

No of processes 5
Process no 4 fails
Election Initiated by 2
Process 2 pass Election(2) to3
Process 3 pass Election(3) to0
Process 0 pass Election(0) to1
Process 3 becomes coordinator
Process 3 pass Coordinator(3) message to process 0
Process 0 pass Coordinator(3) message to process 1
Process 1 pass Coordinator(3) message to process 2
End Of Election 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!