Using clock_getres - Linux C newbie

I am trying to determine the granularity of the timers in my Linux box. According to the man pages for clock_getres, I have to use this snippet:

#include <time.h> #include <stdio.h> int main( int argc, char** argv ) { clockid_t types[] = { CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID, (clockid_t) - 1 }; struct timespec spec; int i = 0; for ( i; types[i] != (clockid_t) - 1; i++ ) { if ( clock_getres( types[i], &spec ) != 0 ) { printf( "Timer %d not supported.\n", types[i] ); } else { printf( "Timer: %d, Seconds: %ld Nanos: %ld\n", i, spec.tv_sec, spec.tv_nsec ); } } } 

I am trying to build like this: gcc -o timertest timertest.c

This works fine on Solaris, but on Linux I get the error:

 /tmp/ccuqfrCK.o: In function `main ':
 timertest.c :(. text + 0x49): undefined reference to `clock_getres'
 collect2: ld returned 1 exit status

I tried passing -lc to gcc, obviously clock_getres is defined in libc, but that doesn't make any difference. I need to skip something simple - any ideas?

Thanks,

Russ

+7
c linux
source share
2 answers

You need to link to the RT library ( -lrt )

+14
source share

Unfortunately, clock_getres() is a POSIX function (optional part of "realtime" - check REALTIME on the POSIX page http://pubs.opengroup.org/onlinepubs/009695399/functions/clock_getres.html ) does not report the timers on Linux in detail. It can return only two predefined results: either 1 / HZ for a low-resolution clock (where HZ is the value of the CONFIG_HZ macro used to configure the Linux kernel, typical values ​​are 100 300 1000), or 1 ns for a high-resolution clock (hrtimers).

In the linux/include/linux/hrtimer.h , there is a comment in the kernel about this level of detail and the meaning of the clock_getres() result http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux /include/linux/hrtimer.h

 269 /* 270 * The resolution of the clocks. The resolution value is returned in 271 * the clock_getres() system call to give application programmers an 272 * idea of the (in)accuracy of timers. Timer values are rounded up to 273 * this resolution values. 274 */ 

Thus, even if the timer source is registered as “hrtimer” (high resolution timer), not every nanosecond (ns) can work. And the value returned from clock_getres() will only say that this timer has not been rounded (since timespec struct has nanosecond precision).

The Linux POSIX API often implements glibc (or its derivatives, such as eglibc), which by default are associated with all programs (linker -lc option). Glibc with versions less than 2.17 shared some additional POSIX parts into additional libraries, such as -lpthread or -lrt . clock_getres() was defined in -lrt .

-lrt option is not required for glibc 2.17 and newer, according to Linux clock_getres() page clock_getres() and clock_gettime() functions, http://man7.org/linux/man-pages/man2/clock_gettime.2.html

Link to -lrt (glibc versions prior to 2.17 only).

The change was also registered in the compatibility tracker: http://upstream.rosalinux.ru/compat_reports/glibc/2.16.0_to_2.17/abi_compat_report.html

Symbols added ... libc-2.17.so ... clock_getres

+2
source share

All Articles