[英]Create a free running timer in real time linux
I am studying real time linux and want to create a free running timer. 我正在研究实时Linux,并希望创建一个免费的运行计时器。 But I am not able to find information regarding the real time linux.
但是我找不到有关实时linux的信息。 Could someone suggests me some document or could someone tell me how to create a free running high precision timer ??
有人可以向我建议一些文档,还是可以告诉我如何创建一个自由运行的高精度计时器?
Achievement : After creating a free running timer, So that I can get the time stamp value. 成就:创建一个自由运行的计时器后,我可以获取时间戳值。
The entire point of using real-time Linux is that it is Linux - all the existing Linux and POSIX services are available, and you should avoid using proprietary non-POSIX OS services if portability is an issue - otherwise you may as well be using a true RTOS. 使用实时Linux的全部要点是Linux-所有现有的Linux和POSIX服务都可用,并且如果可移植性成为问题,则应避免使用专有的非POSIX OS服务-否则,您最好使用真正的实时操作系统。
The standard function clock()
has a CLOCKS_PER_SEC
of 1000000 (ie microseconds) on POSIX. 在POSIX上,标准函数
clock()
的CLOCKS_PER_SEC
为1000000(即微秒)。 The actual granularity is implementation dependent however. 但是,实际的粒度取决于实现。 You can check this for your system with:
您可以使用以下方法为您的系统进行检查:
#include <time.h>
#include <stdio.h>
int main()
{
clock_t t0 = clock() ;
clock_t t1 = t0 ;
while( t0 == t1 )
{
t1 = clock() ;
}
printf( "Clock granularity = %lf seconds\n", (double)(t1 - t0)/(double)(CLOCKS_PER_SEC) ) ;
return 0 ;
}
On ideone.com, this gave the following result: 在ideone.com上,得出以下结果:
Clock granularity = 0.010000 seconds
ie 10ms. 即10毫秒。
If that is not sufficient, a number of clock sources are defined for use with clock_gettime()
and the resolution of each can be determined by clock_getres()
. 如果这还不够,那么可以定义多个时钟源供
clock_gettime()
使用,并且每个时钟源的分辨率都可以由clock_getres()
确定。 The clock ID CLOCK_PROCESS_CPUTIME_ID
is likely to be appropriate. 时钟ID
CLOCK_PROCESS_CPUTIME_ID
可能是合适的。 Note that resolution and granularity are not the same here either. 请注意,此处的分辨率和粒度也不相同。 I performed the following test on ideone.com:
我在ideone.com上执行了以下测试:
#include <time.h>
#include <stdio.h>
int main()
{
struct timespec t0 ;
struct timespec t1 ;
clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &t0) ;
t1 = t0 ;
while( t0.tv_nsec == t1.tv_nsec )
{
clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &t1) ;
}
printf( "Clock granularity = %lf seconds\n", (double)(t1.tv_nsec - t0.tv_nsec)/1e9 ) ;
struct timespec res ;
clock_getres( CLOCK_PROCESS_CPUTIME_ID, &res ) ;
printf( "CPUTIME resolution = %.15lf seconds\n", e, (double)(res.tv_nsec)/1e9) ;
return 0 ;
}
With results: 结果:
Clock granularity = 0.000001 seconds
CPUTIME resolution = 0.000000001000000 seconds
So the granularity is 1 microsecond. 因此,粒度为1微秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.