Linux时间控制精度,linux下时钟精度问题

在linux下有类似windows下多媒体时钟的高精度时钟吗?linux默认时钟精度为多少?

|

In kernel 2.4.x, the precision of time is 10ms, and in kernel 2.6.x, one is 1ms.

you can use sleep, nanosleep and select to implement timing.

sleep: 1 second

nanosleep: 20ms (2.4.x)  2ms (2.6.x)

select:    10ms (2.4.x)  1ms (2.6.x)

note: usleep has been discarded.

if you hope to get interval of time, you can use time, get_time_of_day or asm instructions.

|

#include

#include

#include

void Timer(int sec, long usec)

{

struct timeval tvSelect;

tvSelect.tv_sec = sec;

tvSelect.tv_usec = usec;

select(FD_SETSIZE, NULL, NULL, NULL, &tvSelect);

};

int main()

{

printf("--- begin ---n");

Timer(3, 1000*500);

printf("--- bye ---n");

}

|

用usleep是20ms

用select是10ms

|

gettimeofday(&tv, NULL);

tv的结构如下:

struct timeval

{

long tv_sec;        /* seconds */

long tv_usec;  /* microseconds */

};

gettimeofday的真正精确度是1us。

如果要最小的延时只有用汇编

__volatile__ __asm__("nop");

可以实现一条指令的精度。

我曾经做过定时和计数

|

linux的内核时钟好象是10ms的

|

man 3 sigtimedwait

这个属于POSIX的实时扩展,应该是使用具体实现中所能得到的最高精度的计时机制。

不要用上边有人说的用汇编指令的方式,因为你的进程可能在这个时候被抢占。

如果要用其他的调用,要注意计时的操作只能使用一个系统调用(也是防止被抢占,这个是POSIX的标准建议的)。

select的移植性不太好。

如果要计时的话看看所有以clock_开头的函数。这些都是POSIX的实时计时函数。

其他还可以自己对硬件时钟编程,或者使用CPU里边的Profiling计数器,但是这样难度有点大。

如果真的对实时性要求非常高,还是要使用实时操作系统,就别用Linux了。

|

1,setitimer的精度只能达到毫秒级。

2,setitimer有可能会丢失事件:

BUGS        Under Linux, the generation and delivery of a signal are distinct,  and

there each signal is permitted only one outstanding event.  It's there-

fore conceivable that under pathologically heavy  loading,  ITIMER_REAL        will  expire  before  the  signal  from  a previous expiration has been

delivered.  The second signal in such an event will be lost.

3,setitimer是SVR4和4.4BSD要求的,不属于POSIX。

|

Its precision  is equal with one of kernel rescheduler.

10ms (2.4.x) 1ms(2.6.x)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值