Accurate inter process timer on Linux

c, gcc, linux, system-clock

Solution

Yes

You are using the correct interface. This is a Posix-specified HRT (High Resolution Timer) that is available in modern Loonix, and your use is the canonical standard.

For ping resolutions it probably doesn't matter, but it's possible to use `CLOCK_MONOTONIC,` especially for short intervals. Under other conditions, `CLOCK_REALTIME` may be more accurate. However, the exact meaning of these clocks is not specified by Posix, and on Linux, I believe all of them are subject to NTP adjustments. That's a good thing for long intervals, at least.

On Linux, to get access to the underlying hardware clock without NTP adjustments, you have to go off the Posix reservation and use `CLOCK_MONOTONIC_RAW.`

http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html

Problem

I would like to measure the ping time between a sender process and a consumer. Both processes run on the same physical host, Linux 64 bit. I'm currently using `clock_gettime(CLOCK_REALTIME, &cur_ts);`. I basically capture the current timestamp `cur_ts` and send it to consumer process; as soon as it's being received on the other end I then invoke `clock_gettime(CLOCK_REALTIME, &cur_ts);` again in the second process and subtract the two times. Is this procedure accurate to measure ping between two processes? Should I be using something different? Thanks

Original source