Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Linux provide a monotonically increasing clock to applications

Tags:

linux

unix

timer

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.

like image 901
camh Avatar asked Oct 17 '08 04:10

camh


2 Answers

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

like image 172
Robert Gamble Avatar answered Nov 10 '22 03:11

Robert Gamble


There is clock_gettime:

struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
like image 30
DGentry Avatar answered Nov 10 '22 02:11

DGentry