<分区>
我需要在 Linux 内核中进行一些轮询以持续一段时间,因此我需要设计在几毫秒间隔后退出的 while 循环。那我该怎么做呢?
虽然我不得不使用 gettimeofday()
但它可以在用户空间使用,我想在内核空间使用它。
标签 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/