CLOCK_REALTIME 和 CLOCK_MONOTONIC 的区别? [英] Difference between CLOCK_REALTIME and CLOCK_MONOTONIC?

查看:59
本文介绍了CLOCK_REALTIME 和 CLOCK_MONOTONIC 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释一下 CLOCK_REALTIMECLOCK_MONOTONIC 在 Linux 上由 clock_gettime() 返回的时钟之间的区别吗?

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_REALTIMECLOCK_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 代表机器对当前挂钟、时间的最佳猜测.正如 IgnacioMarkR 说,这意味着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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆