c - 如何在 linux 内核中运行 while 循环几毫秒?

标签 c linux time timer linux-kernel

<分区>

我需要在 Linux 内核中进行一些轮询以持续一段时间,因此我需要设计在几毫秒间隔后退出的 while 循环。那我该怎么做呢?

虽然我不得不使用 gettimeofday() 但它可以在用户空间使用,我想在内核空间使用它。

最佳答案

使用 msecs_to_jiffies :

unsigned long j0,j1,delay;
delay = msecs_to_jiffies(20); /* 20 msec delay */
j0 = jiffies; 
j1 = j0 + delay; 

while (time_before(jiffies, j1)) 
        /* do something */

如果您的系统上有高分辨率计时器,您可以将它用于小于一瞬间的时间。一般考虑使用内核定时器或者hrt

关于c - 如何在 linux 内核中运行 while 循环几毫秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16032228/

相关文章:

c - C中Server和Client之间用Ring Buffer(循环缓冲区)进行数据交换

linux - sybase ase数据库是免费的吗

windows - windows .bat 脚本中的测量命令 : command not found

java运行时cmd日期和时间命令

perl - 在 Perl 模板工具包中显示时间增量

c++ - 如果我将 extern "C++"与 C 工具链一起使用会怎样?

c - 为什么 Kernighan 和 Ritchie 包含看似不必要的类型转换?

linux - 设置 (Linux) Hadoop 集群

c - GCC 结构双指针数组初始化

linux - Bash - 从 AWS 中提取区域