CLOCK_REALTIME和CLOCK_MONOTONIC之间的区别? [英] Difference between CLOCK_REALTIME and CLOCK_MONOTONIC?
问题描述
您能解释一下Linux上clock_gettime()
返回的CLOCK_REALTIME
和CLOCK_MONOTONIC
时钟之间的区别吗?
Could you explain the difference between CLOCK_REALTIME
and CLOCK_MONOTONIC
clocks returned by clock_gettime()
on Linux?
如果我需要计算外部源产生的时间戳与当前时间之间的经过时间,哪个是更好的选择?
Which is a better choice if I need to compute elapsed time between timestamps produced by an external source and the current time?
最后,如果我有一个NTP守护程序定期调整系统时间,这些调整如何与CLOCK_REALTIME
和CLOCK_MONOTONIC
中的每一个相互作用?
Lastly, if I have an NTP daemon periodically adjusting system time, how do these adjustments interact with each of CLOCK_REALTIME
and CLOCK_MONOTONIC
?
推荐答案
CLOCK_REALTIME
代表机器对于当前壁钟时间的最佳猜测.如 Ignacio 和MarkR 说,这意味着CLOCK_REALTIME
可以在系统中前后跳转时间时钟已更改,包括通过NTP更改.
CLOCK_REALTIME
represents the machine's best-guess as to the current wall-clock, time-of-day time. As Ignacio and MarkR say, this means that CLOCK_REALTIME
can jump forwards and backwards as the system time-of-day clock is changed, including by NTP.
CLOCK_MONOTONIC
表示自过去某个固定点以来经过的绝对时间.不受系统时间时钟变化的影响.
CLOCK_MONOTONIC
represents the absolute elapsed wall-clock time since some arbitrary, fixed point in the past. It isn't affected by changes in the system time-of-day clock.
如果要计算在一台计算机上观察到的两个事件之间的间隔时间,而又不进行重新引导,那么CLOCK_MONOTONIC
是最佳选择.
If you want to compute the elapsed time between two events observed on the one machine without an intervening reboot, CLOCK_MONOTONIC
is the best option.
请注意,在Linux上,CLOCK_MONOTONIC
不会衡量挂起所花费的时间,尽管它应该由POSIX定义.您可以使用特定于Linux的CLOCK_BOOTTIME
来使单调时钟在挂起期间保持运行.
Note that on Linux, CLOCK_MONOTONIC
does not measure time spent in suspend, although by the POSIX definition it should. You can use the Linux-specific CLOCK_BOOTTIME
for a monotonic clock that keeps running during suspend.
这篇关于CLOCK_REALTIME和CLOCK_MONOTONIC之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!