Event Synchronization Using NTP

2k Views Asked by At

I would like to synchronize two events between two (or more) wire networked Linux machines. Can I use NTP to do this?

NTP seems to be mostly focused on synchronizing to a time server, where I need two machines to be synchronized to each other. There is a subtle difference there. For example, if one machine is located half as many hops away as a second machine from the time server, I might be able to get better synchronization if I try to synchronize the two machines to each other directly instead of synchronizing both to a time server.

A slightly different question: If I were to use NTP, what would be the best way to schedule events? A cronjob or at script? Could I get better (sub second) synchronization if I were to use a library like this one.

Finally, does anyone know of any time synchronization software packages that are suited to synchronizing two (or more) machines together, not necessarily synchronizing to a time server.

Thanks for any help.

2

There are 2 best solutions below

2
On

You might try delegating one machine as the master, and the remaining machines as slaves. When the synchronized events should occur, the master triggers the slaves to commence.

The synchronization would be limited only by the latency (ping) between the machines, and you wouldn't need to worry about system clocks consistency.

0
On

What is ping latency variation between your hosts? What kind of start time discrepancy between coordinated processes is ok for you? How are going to start processes? Cron is very imprecise, and startup time for processes needs to be accounted for, too.

If ping times to different hosts vary significantly, I'd do something like this.

  • Use a reliable public NTP server to synchronize clocks on all coordinated hosts a few minutes before the event. With frequent events, 3-4 synchronizations a day should be plenty enough, though.

  • Using a low-precision scheduler like cron, 2-3 minutes ahead of time, start a simple wrapper shell script that will wait() until e.g. 15 seconds before event. Then the wrapper script starts the tagert app, with higher-than-normal priority.

  • The app loads (disk access and dynamic linking is slow), reads whatever data files needed, makes all time-consuming calculations, etc. Then it waits until the start moment with subsecond precision using usleep() and ftime() or gettimeofday(), put right before the fireLasersAtTheMoon() or what happens to be your target action.

Obviously, it makes little sense to synchronize so precisely actions that are naturally imprecise, like network communication. If your network has predictable latency, you just measure it using round-trip times of ping and make a master process on one host to send a start command via ssh to other host(s) taking latency to account.