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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:22:43  来源:igfitidea点击:

Difference between CLOCK_REALTIME and CLOCK_MONOTONIC?

linux

提问by NPE

Could you explain the difference between CLOCK_REALTIMEand CLOCK_MONOTONICclocks returned by clock_gettime()on Linux?

你能解释一下Linux 上返回的CLOCK_REALTIMECLOCK_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_REALTIMEand CLOCK_MONOTONIC?

最后,如果我有一个 NTP 守护进程定期调整系统时间,这些调整如何与每个CLOCK_REALTIME和交互CLOCK_MONOTONIC

采纳答案by caf

CLOCK_REALTIMErepresents the machine's best-guess as to the current wall-clock, time-of-day time. As Ignacioand MarkRsay, this means that CLOCK_REALTIMEcan jump forwards and backwards as the system time-of-day clock is changed, including by NTP.

CLOCK_REALTIME代表机器对当前挂钟、时间的最佳猜测。正如IgnacioMarkR所说,这意味着CLOCK_REALTIME随着系统时钟的变化(包括 NTP),它可以向前和向后跳跃。

CLOCK_MONOTONICrepresents 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_MONOTONICis the best option.

如果您想计算在一台机器上观察到的两个事件之间的经过时间而无需干预重启,这CLOCK_MONOTONIC是最好的选择。

Note that on Linux, CLOCK_MONOTONICdoes not measure time spent in suspend, although by the POSIX definition it should. You can use the Linux-specific CLOCK_BOOTTIMEfor a monotonic clock that keeps running during suspend.

请注意,在 Linux 上,CLOCK_MONOTONIC不测量挂起的时间,尽管根据 POSIX 定义它应该测量。您可以将 Linux 特定CLOCK_BOOTTIME的时钟用于在挂起期间保持运行的单调时钟。

回答by Ignacio Vazquez-Abrams

CLOCK_REALTIMEis affected by NTP, and can move forwards and backwards. CLOCK_MONOTONICis not, and advances at one tick per tick.

CLOCK_REALTIME受 NTP 影响,可以前后移动。CLOCK_MONOTONIC不是,并且以每滴答一个滴答前进。

回答by MarkR

In addition to Ignacio's answer, CLOCK_REALTIMEcan go up forward in leaps, and occasionally backwards. CLOCK_MONOTONICdoes 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_REALTIMEleaping 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_REALTIMEjumps forwards following the resume, CLOCK_MONOTONICdoes 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.

也就是说,我相信他假设进程在操作系统的同一实例上运行,因此您可能希望定期运行校准以估计漂移。