0% found this document useful (0 votes)
10 views5 pages

EXPT No 6

The document outlines an experiment to study the Berkeley Clock synchronization algorithm, which synchronizes the time across multiple nodes without accurate time sources. It includes a Java code implementation that calculates time differences, averages them, and adjusts the nodes' clocks accordingly. The output demonstrates the synchronization process with example input from two nodes.

Uploaded by

os9268839
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)
10 views5 pages

EXPT No 6

The document outlines an experiment to study the Berkeley Clock synchronization algorithm, which synchronizes the time across multiple nodes without accurate time sources. It includes a Java code implementation that calculates time differences, averages them, and adjusts the nodes' clocks accordingly. The output demonstrates the synchronization process with example input from two nodes.

Uploaded by

os9268839
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/ 5

EXPERIMENT NO.

6
Name : Class : BE.COMP

Roll No. : SUB :DC

Date of Conductance : Date of Performance:

Title: WAP to study Berkeley Clock synchronization algorithm

Theory:
●​ Assumes none of the node have an accurate time source
•​ It averages the time and synchronizes all . Works in master slave configuration.
•​ Server polls each machine periodically , asking for their time. When all results
are in , the master computes the average time(including its own time in
calculation)
•​ Average cancels out the individual clock tendencies to run fast or slow.
•​ Instead sending updated time back slaves , which would introduce further
uncertainty due to network delays, it sends each machine the offset by which
clocks need adjusted. Then slave clock adjusted accordingly.
•​

•​
•​

Code :-

import java.io.*;
import java.util.*;

public class Berkley {

// Function to calculate the time difference between Time Server and Nodes
float diff(int h, int m, int s, int nh, int nm, int ns) {
int dh = h - nh;
int dm = m - nm;
int ds = s - ns;
int diff = (dh * 60 * 60) + (dm * 60) + ds;
return diff;
}

// Function to calculate the average of all time differences


float average(float diff[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += diff[i];
}
float average = (float) sum / (n + 1);
System.out.println("The average of all time differences is " + average);
return average;
}

// Function to synchronize the clocks based on the average time difference


void sync(float diff[], int n, int h, int m, int s, int nh[], int nm[], int ns[], float average)
{
for (int i = 0; i < n; i++) {
diff[i] += average;

int dh = (int) diff[i] / (60 * 60);


diff[i] %= (60 * 60);
int dm = (int) diff[i] / 60;
diff[i] %= 60;
int ds = (int) diff[i];

nh[i] += dh;
if (nh[i] > 23) {
nh[i] %= 24;
}

nm[i] += dm;
if (nm[i] > 59) {
nh[i]++;
nm[i] %= 60;
}

ns[i] += ds;
if (ns[i] > 59) {
nm[i]++;
ns[i] %= 60;
}

if (ns[i] < 0) {
nm[i]--;
ns[i] += 60;
}
}

// Adjust Time Server clock


h += (int) (average / (60 * 60));
if (h > 23) {
h %= 24;
}
m += (int) (average / (60 * 60));
if (m > 59) {
h++;
m %= 60;
}

s += (int) (average % (60 * 60));


if (s > 59) {
m++;
s %= 60;
}

if (s < 0) {
m--;
s += 60;
}

// Print synchronized clocks


System.out.println("The synchronized clocks are:");
System.out.println("Time Server -> " + h + ":" + m + ":" + s);
for (int i = 0; i < n; i++) {
System.out.println("Node " + (i + 1) + " -> " + nh[i] + ":" + nm[i] + ":" + ns[i]);
}
}

public static void main(String[] args) throws IOException {


Berkley b = new Berkley();
Date date = new Date();
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));

System.out.println("Enter number of nodes:");


int n = Integer.parseInt(obj.readLine());

int h = date.getHours();
int m = date.getMinutes();
int s = date.getSeconds();

int nh[] = new int[n];


int nm[] = new int[n];
int ns[] = new int[n];

// Input times for each node


for (int i = 0; i < n; i++) {
System.out.println("Enter time for node " + (i + 1));
System.out.print("Hours: ");
nh[i] = Integer.parseInt(obj.readLine());
System.out.print("Minutes: ");
nm[i] = Integer.parseInt(obj.readLine());
System.out.print("Seconds: ");
ns[i] = Integer.parseInt(obj.readLine());
}

// Send the Time Server's time to the nodes and calculate the differences
System.out.println("Time Server sent time " + h + ":" + m + ":" + s + " to
nodes.");
float diff[] = new float[n];
for (int i = 0; i < n; i++) {
diff[i] = b.diff(h, m, s, nh[i], nm[i], ns[i]);
System.out.println("Node " + (i + 1) + " sent time difference of " + (int) diff[i] +
" to Time Server.");
}

// Calculate the average time difference and synchronize clocks


float average = b.average(diff, n);
b.sync(diff, n, h, m, s, nh, nm, ns, average);
}
}

OUTPUT :-

Enter number of nodes:


2
Enter time for node 1
Hours: 11
Minutes: 48
Seconds: 45

Enter time for node 2


Hours: 11
Minutes: 49
Seconds: 41

Time Server sent time 4:52:12 to nodes.


Node 1 sent time difference of -24993 to Time Server.
Node 2 sent time difference of -25049 to Time Server.
The average of all time differences is -16680.666
The synchronized clocks are:
Time Server -> 0:47:-2208
Node 1 -> 0:14:12
Node 2 -> 0:14:12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy