EXPT No 6
EXPT No 6
6
Name : Class : BE.COMP
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.*;
// 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;
}
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;
}
}
if (s < 0) {
m--;
s += 60;
}
int h = date.getHours();
int m = date.getMinutes();
int s = date.getSeconds();
// 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.");
}
OUTPUT :-