Does Linux provide a monotonically increasing clock to applications

人走茶凉 提交于 2019-12-12 10:31:47

问题


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

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) does not provide a monotonically increasing clock.

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.


回答1:


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




回答2:


There is clock_gettime:

struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);


来源:https://stackoverflow.com/questions/211055/does-linux-provide-a-monotonically-increasing-clock-to-applications

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!