如何在 linux 驱动程序模块中暂停 100+ 毫秒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7367615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:07:18  来源:igfitidea点击:

How can I pause for 100+ milliseconds in a linux driver module?

linuxlinux-kerneldelaylinux-device-driver

提问by Jamie

I'm writing a kernel driver for a device that produces regular amounts of data for reading periodically. The user space program is ideally suited to making this a blocking driver.

我正在为一个设备编写内核驱动程序,该设备会产生定期读取的定期数据量。用户空间程序非常适合使其成为阻塞驱动程序。

What methods are available for pausing anywhere from 4 to 100ms in a driver (i.e. doing the "block")? In user space I'd do something akin to:

有哪些方法可以在驱动程序中暂停 4 到 100 毫秒(即执行“阻止”)?在用户空间我会做一些类似于:

tv.tv_sec  = microsecond_delay / 1000000ul;
tv.tv_usec = microsecond_delay % 1000000ul;
(void)select(0, NULL, NULL, NULL, & tv);

or

或者

gettimeofday(tv,NULL);

and compare the structures.

并比较结构。

[Edit - my own answer]

[编辑 - 我自己的答案]

I will be using the following code in my driver:

我将在我的驱动程序中使用以下代码:

#include <linux/jiffies.h>
...
schedule_timeout(file->private_data->my_driver_struct.read_pause_jiffies);

Voila! I shall now test ...

瞧!我现在要测试...

采纳答案by Jamie

#include <linux/delay.h>

...
msleep(100);
...

回答by adrianmcmenamin

Using schedule_timeout does NOT sleep for a specified time but for a minimum specified time. If you really want to blockfor a specified time, you will have to use locks. Sleeping will only guarantee you a minimum time - this may not matter to you depending on much granularity you need. But a better driver would sleep until the reader asked for more data in any case.

使用 schedule_timeout 不会休眠指定的时间,而是休眠指定的最短时间。如果你真的想阻塞指定的时间,你将不得不使用锁。睡眠只能保证您有最少的时间 - 这对您来说可能无关紧要,具体取决于您需要的粒度。但是,无论如何,更好的驱动程序会一直睡到读者要求提供更多数据为止。