Linux是否为应用程序提供单调增加的时钟 [英] Does Linux provide a monotonically increasing clock to applications

查看:39
本文介绍了Linux是否为应用程序提供单调增加的时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux/Unix/Posix是否向用户空间应用程序提供API来访问单调递增的时钟,且精度在毫秒到毫秒之间?

Does Linux/Unix/Posix provide an API to user-space applications to access a monotonically increasing clock, with centisecond to millisecond accuracy?

在Linux上,/proc/uptime提供了基于字符串的浮点数表示形式,表示系统已启动的秒数.

On Linux, /proc/uptime provides a string-based representation of a floating point number of the number of seconds the system has been up.

gettimeofday(2)不能提供单调递增的时钟.

gettimeofday(2) does not provide a monotonically increasing clock.

我可以在ITIMER_REAL时域中使用getitimer(2),将计时器设置为最大(取决于平台)启动,并忽略生成的信号,但是根据手册页,计时器可以运行的最长时间约为100天,比我的预期运行时间短.

I could use getitimer(2) in the ITIMER_REAL time domain, set the timer to start at the (platform dependent) maximum and ignore the signal generated, but according to the man page the longest the timer can run for is approximately 100 days, which is shorter than my expected run time.

推荐答案

使用POSIX clock_gettime()函数和 CLOCK_MONOTONIC .有关详细信息,请参见手册页.

Use the POSIX clock_gettime() function with CLOCK_MONOTONIC. See the man page for details.

这篇关于Linux是否为应用程序提供单调增加的时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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