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 d开发者_C百科river.
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:
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 ...
#include <linux/delay.h>
...
msleep(100);
...
Using schedule_timeout does NOT sleep for a specified time but for a minimum specified time. If you really want to block for 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.
精彩评论