Berkeley's Algorithm


Berkeley's Algorithm is a distributed algorithm for computing the correct time in a network of computers. The algorithm is designed to work in a network where clocks may be running at slightly different rates, and some computers may experience intermittent communication failures.

The basic idea behind Berkeley's Algorithm is that each computer in the network periodically sends its local time to a designated "master" computer, which then computes the correct time for the network based on the received timestamps. The master computer then sends the correct time back to all the computers in the network, and each computer sets its clock to the received time.

There are several variations of Berkeley's Algorithm that have been proposed, but a common version of the algorithm is as follows −

  • Each computer starts with its own local time, and periodically sends its time to the master computer.

  • The master computer receives timestamps from all the computers in the network.

  • The master computer computes the average time of all the timestamps it has received and sends the average time back to all the computers in the network.

  • Each computer sets its clock to the time it receives from the master computer.

  • The process is repeated periodically, so that over time, the clocks of all the computers in the network will converge to the correct time.

One benefit of Berkeley's Algorithm is that it is relatively simple to implement and understand. However, It has a limitation that the time computed by the algorithm is based on the network conditions and time of sending and receiving timestamps which can't be very accurate and also it has a requirement of a master computer which if failed can cause the algorithm to stop working.

There is other more advance algorithm such as the Network Time Protocol (NTP) which use a more complex algorithm and also consider the network delay and clock drift to get a more accurate time.

Scope of Improvement

There are several areas where Berkeley's Algorithm can be improved −

  • Accuracy − The algorithm calculates the average time based on the timestamps received from all the computers in the network, which can lead to inaccuracies, especially if the network has a high degree of jitter or delay.

  • Robustness − The algorithm requires a master computer, which if it fails, can cause the algorithm to stop working. If the master computer is a single point of failure, it can make the algorithm less robust.

  • Synchronization Quality − The algorithm assumes that all the clocks in the network are running at the same rate, but in practice, clocks may drift due to temperature, aging, or other factors. The algorithm doesn't consider this drift and may fail to achieve a high degree of synchronization between the clocks in the network.

  • Security − There is no security measures in the algorithm to prevent malicious computers from tampering with the timestamps they send to the master computer, which can skew the results of the algorithm.

  • Adaptability − The algorithm doesn't adapt well to changes in the network, for example, if a new computer is added to the network or if the network topology changes.

As you see, Berkeley algorithm, despite its simplicity, has some limitations. Network Time Protocol (NTP) is widely used in the industry to overcome some of the limitations of Berkeley's Algorithm. NTP uses a more complex algorithm and also considers the network delay and clock drift to get a more accurate time. Additionally, NTP uses a hierarchical structure, which makes it more robust and adaptable to changes in the network. NTP also includes cryptographic mechanisms to authenticate the time messages and protect against malicious attacks.

How to use Berkeley's Algorithm

To use Berkeley's Algorithm, you would need to implement the algorithm on each computer in a network of computers. Here is a general overview of the steps you would need to take to implement the algorithm −

  • Designate one computer in the network as the master computer. This computer will be responsible for receiving timestamps from all the other computers in the network and computing the correct time.

  • On each computer in the network, set up a timer to periodically send the computer's local time to the master computer.

  • On the master computer, set up a mechanism for receiving timestamps from all the computers in the network.

  • On the master computer, implement the logic for calculating the average time based on the received timestamps.

  • On the master computer, set up a mechanism for sending the calculated average time back to all the computers in the network.

  • On each computer in the network, set up a mechanism for receiving the time from the master computer and setting the computer's clock to that time.

  • Repeat the process periodically, for example, every 30 seconds or 1 minute, to ensure that the clocks in the network stay synchronized.

It's worth noting that this is a high-level description of the steps to implement the algorithm and in practice, many implementation details will depend on the programming language, operating system, and network infrastructure you are using. Additionally, as explained before, Berkeley Algorithm have some limitations, If you need to have a more accurate and robust solution, you may consider using other time synchronization protocols like NTP which have been designed to overcome some of the limitations of Berkeley's algorithm.

Example

Here's an example of how you might implement Berkeley's Algorithm in Python. This example is for a network of computers, where one computer is designated as the master and the others are clients.

On the Master Computer

import time

def compute_average_time(timestamps):
   return sum(timestamps) / len(timestamps)

# This function runs on the master computer
def master_main():
   timestamps = []
   while True:
      # Wait for client timestamps
      timestamp = receive_timestamp_from_client()
      timestamps.append(timestamp)
        
      # Compute average time
      average_time = compute_average_time(timestamps)
        
      # Send the correct time to all clients
      send_time_to_clients(average_time)
        
      # Clear the list of timestamps
      timestamps.clear()
        
      # Wait for a period of time before starting again
      time.sleep(30) # example for 30 sec 

On the Client Computers

import time

# This function runs on the client computers
def client_main():
   while True:
      # Get the local time
      local_time = time.time()
        
      # Send the local time to the master computer
      send_timestamp_to_master(local_time)
        
      # Receive the correct time from the master computer
      correct_time = receive_time_from_master()
        
      # Set the local clock to the correct time
      set_local_clock(correct_time)
        
      # Wait for a period of time before starting again
      time.sleep(30) # example for 30 sec 

Please note that this is a very basic example, it doesn't include any error handling, and it is not intended to be runnable as is, but to give you an idea of how Berkeley's Algorithm can be implemented in Python. In practice, you would need to fill in the details for sending and receiving timestamps and times, as well as handling cases where a client or the master might not be reachable or is malfunctioning.

It's also worth to mention that, while Berkeley's Algorithm can be useful in certain scenarios, it may not be the best choice for all cases, and you may consider using other time synchronization protocols, like NTP, which is widely used in industry to achieve accurate and robust time synchronization.

Updated on: 08-Feb-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements