CLOCK_REALTIME 和 CLOCK_MONOTONIC 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3523442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between CLOCK_REALTIME and CLOCK_MONOTONIC?
提问by NPE
Could you explain the difference between CLOCK_REALTIME
and CLOCK_MONOTONIC
clocks returned by clock_gettime()
on Linux?
你能解释一下Linux 上返回的CLOCK_REALTIME
和CLOCK_MONOTONIC
时钟之间的区别clock_gettime()
吗?
Which is a better choice if I need to compute elapsed time between timestamps produced by an external source and the current time?
如果我需要计算外部源生成的时间戳与当前时间之间的经过时间,哪个是更好的选择?
Lastly, if I have an NTP daemon periodically adjusting system time, how do these adjustments interact with each of CLOCK_REALTIME
and CLOCK_MONOTONIC
?
最后,如果我有一个 NTP 守护进程定期调整系统时间,这些调整如何与每个CLOCK_REALTIME
和交互CLOCK_MONOTONIC
?
采纳答案by caf
CLOCK_REALTIME
represents the machine's best-guess as to the current wall-clock, time-of-day time. As Ignacioand MarkRsay, this means that CLOCK_REALTIME
can jump forwards and backwards as the system time-of-day clock is changed, including by NTP.
CLOCK_REALTIME
代表机器对当前挂钟、时间的最佳猜测。正如Ignacio和MarkR所说,这意味着CLOCK_REALTIME
随着系统时钟的变化(包括 NTP),它可以向前和向后跳跃。
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.
如果您想计算在一台机器上观察到的两个事件之间的经过时间而无需干预重启,这CLOCK_MONOTONIC
是最好的选择。
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.
请注意,在 Linux 上,CLOCK_MONOTONIC
不测量挂起的时间,尽管根据 POSIX 定义它应该测量。您可以将 Linux 特定CLOCK_BOOTTIME
的时钟用于在挂起期间保持运行的单调时钟。
回答by Ignacio Vazquez-Abrams
CLOCK_REALTIME
is affected by NTP, and can move forwards and backwards. CLOCK_MONOTONIC
is not, and advances at one tick per tick.
CLOCK_REALTIME
受 NTP 影响,可以前后移动。CLOCK_MONOTONIC
不是,并且以每滴答一个滴答前进。
回答by MarkR
In addition to Ignacio's answer, CLOCK_REALTIME
can go up forward in leaps, and occasionally backwards. CLOCK_MONOTONIC
does neither; it just keeps going forwards (although it probably resets at reboot).
除了伊格纳西奥的回答,CLOCK_REALTIME
还能向前跳跃,偶尔向后。CLOCK_MONOTONIC
两者都没有;它只是继续前进(尽管它可能会在重新启动时重置)。
A robust app needs to be able to tolerate CLOCK_REALTIME
leaping forwards occasionally (and perhaps backwards very slightly very occasionally, although that is more of an edge-case).
一个健壮的应用程序需要能够容忍CLOCK_REALTIME
偶尔向前跳跃(也许偶尔会非常轻微地向后跳跃,尽管这更像是一种边缘情况)。
Imagine what happens when you suspend your laptop - CLOCK_REALTIME
jumps forwards following the resume, CLOCK_MONOTONIC
does not. Try it on a VM.
想象一下当你暂停你的笔记本电脑时会发生什么 -CLOCK_REALTIME
在恢复后向前跳跃,CLOCK_MONOTONIC
而不是。在虚拟机上试试。
回答by user2548100
Robert Love's book LINUX System Programming 2nd Edition, specifically addresses your question at the beginning of Chapter 11, pg 363:
Robert Love 的书LINUX System Programming 2nd Edition,在第 11 章第 363 页的开头专门解决了您的问题:
The important aspect of a monotonic time source is NOT the current value, but the guarantee that the time source is strictly linearly increasing, and thus useful for calculating the difference in time between two samplings
单调时间源的重要方面不是当前值,而是保证时间源严格线性增加,因此可用于计算两次采样之间的时间差
That said, I believe he is assuming the processes are running on the same instance of an OS, so you might want to have a periodic calibration running to be able to estimate drift.
也就是说,我相信他假设进程在操作系统的同一实例上运行,因此您可能希望定期运行校准以估计漂移。