linux - Linux 上的 setitimer 汇总?

标签 linux setitimer

当我使用 setitimer 设置一个短超时,然后在 Linux 2.6 上查询设置值(使用 getitimer 或另一个 setitimer)。 26系统(Debian 5.0.5),我得到一个比我设置的更高的值:

#include <sys/time.h>
#include <iostream>

int main() {
  struct itimerval wanted, got;

  wanted.it_value.tv_sec     = 0;
  wanted.it_value.tv_usec    = 7000;
  wanted.it_interval.tv_sec  = 0;
  wanted.it_interval.tv_usec = 0;

  setitimer(ITIMER_VIRTUAL, &wanted, NULL);
  getitimer(ITIMER_VIRTUAL, &got);

  std::cerr << "we said: " << wanted.it_value.tv_usec << "\n"
            << "linux set: " << got.it_value.tv_usec << std::endl;

  return 0;
}

返回:

we said: 7000
linux set: 12000

这是有问题的,因为我们使用的是经过一些计算后报告的剩余时间,而且它们也太大了。

这是一个已知问题吗? (谷歌搜索无效。)有人有好的解决方法吗?

最佳答案

POSIX documentation of the setitimer function有一张纸条

Implementations may place limitations on the granularity of timer values. For each interval timer, if the requested timer value requires a finer granularity than the implementation supports, the actual timer value shall be rounded up to the next supported value

您系统中的粒度似乎高于 1000 usec(似乎是 6000 usec)并且计时器值被四舍五入。如果您需要这样的精度,计时器粒度就是个问题。

关于linux - Linux 上的 setitimer 汇总?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6856514/

相关文章:

java - Linux shell 中一个程序同时过滤输出和获取退出码

regex - 不确定我是否理解正则表达式 : (\b\w+)\1\b?

linux - 使用 wget 指定下载文件名而不覆盖

php - 从 LEMP 服务器上的源代码安装 Memcached 1.4.2.0 并使其与 PHP5-Memcached 通信

linux - 在 Linux 上设置定时器和信号计数。信号计数与运行时间成正比吗?

c - 如何在 C 中将 setitimer 与 SIGALRM 一起使用

c - 运行计时器 30 秒

java - Raspberry Pi 上的 Jave 段错误