linux下推荐的方式1ms的高分辨率定时器 [英] 1ms resolution timer under linux recommended way

查看:758
本文介绍了linux下推荐的方式1ms的高分辨率定时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在linux下1毫秒分辨率的计时器滴答声。它是用来递增,反过来被用于查看各种活动应当触发的定时器值。 POSIX的timerfd_create不是因为glibc的要求的选项。我试图timer_create和timer_settimer,但我从他们那里得到最好的是10ms的决议,较小的值似乎默认为10ms的分辨率。 Getittimer和setitimer函数有根据手册页10毫秒的分辨率。

I need a timer tick with 1ms resolution under linux. It is used to increment a timer value that in turn is used to see if various Events should be triggered. The POSIX timerfd_create is not an option because of the glibc requirement. I tried timer_create and timer_settimer, but the best I get from them is a 10ms resolution, smaller values seem to default to 10ms resolution. Getittimer and setitimer have a 10 ms resolution according to the manpage.

要做到这一点定时器我目前能想到的唯一的办法是,如果一个MS已经通过我的主循环使用clock_gettime与CLOCK_MONOTONIC一个测试,如果是增加计数器(然后检查的各项活动应火)。

The only way to do this timer I can currently think of is to use clock_gettime with CLOCK_MONOTONIC in my main loop an test if a ms has passed, and if so to increase the counter (and then check if the various Events should fire).

有没有更好的方法来做到这一点,而不是不断地查询在主循环?推荐的解决方案是什么呢?

Is there a better way to do this than to constantly query in the main loop? What is the recommended solution to this?

我使用的语言是普通旧的C

The language I am using is plain old c

更新

我使用的是2.6.26内核。我知道你可以把它打断1kHz时,和POSIX timer_ *函数则可以通过编程到1毫秒,但似乎并不可靠,我不希望使用,因为它可能需要一些新的内核系统。有些股票内核似乎仍然有配置的100Hz的。而我需要检测。该应用程序可以在比我的系统别的东西来运行:)

Update
I am using a 2.6.26 Kernel. I know you can have it interrupt at 1kHz, and the POSIX timer_* functions then can be programmed to up to 1ms but that seems not to be reliable and I don't want to use that, because it may need a new kernel on some Systems. Some stock Kernel seem to still have the 100Hz configured. And I would need to detect that. The application may be run on something else than my System :)

我无法入睡时间为1ms,因为有可能是网络事件,我必须作出反应。

I can not sleep for 1ms because there may be network events I have to react to.

如何解决它
由于它并不重要,我只是宣布全球计时器有100​​ms的分辨率。用自己的计时器的所有事件必须设置至少100毫秒计时器超时。我或多或少知道是否有将是一个更好的办法,所以这个问题。

How I resolved it Since it is not that important I simply declared that the global timer has a 100ms resolution. All events using their own timer have to set at least 100ms for timer expiration. I was more or less wondering if there would be a better way, hence the question.

为什么我接受了答案
我想从自由空间的回答最好说明为什么它是不是真的有可能没有实时Linux系统。

Why I accepted the answer I think the answer from freespace best described why it is not really possible without a realtime Linux System.

推荐答案

轮询在主循环是不是一个答案无论是 - 你的进程可能不会得到太多的CPU时间,所以超过10ms将您的code前等待得到运行,使其没有实际意义。

Polling in the main loop isn't an answer either - your process might not get much CPU time, so more than 10ms will elapse before your code gets to run, rendering it moot.

10ms的是有关标准计时器分辨率的大部分非实时操作系统(RTOS)。但它是在非实时操作系统实际意义 - 调度器的行为,并且调度将要极大地影响如何可以快速定时器即将过期响应。例如,即使假设您有一个子10ms的高分辨率定时器,你不能到计时器到期,如果你的code未运行响应。既然你不能predict当你的code会跑,你不能到计时器到期准确地响应。

10ms is about the standard timer resolution for most non-realtime operating systems (RTOS). But it is moot in a non-RTOS - the behaviour of the scheduler and dispatcher is going to greatly influence how quickly you can respond to a timer expiring. For example even suppose you had a sub 10ms resolution timer, you can't respond to the timer expiring if your code isn't running. Since you can't predict when your code is going to run, you can't respond to timer expiration accurately.

有当然的实时Linux内核,请参阅http://www.linuxdevices.com/articles/AT8073314981.html为列表。一个实时操作系统提供的设施,让你可以当你的code将运行约软或硬的保证。这大约是唯一的方法可靠,准确地向计时器到期等反应。

There is of course realtime linux kernels, see http://www.linuxdevices.com/articles/AT8073314981.html for a list. A RTOS offers facilities whereby you can get soft or hard guarantees about when your code is going to run. This is about the only way to reliably and accurately respond to timers expiring etc.

这篇关于linux下推荐的方式1ms的高分辨率定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆