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:
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 ...
To accommodate for this very situation, where you want to delay execution waiting for no specific event, the kernel offers the schedule_timeout function so you can avoid declaring and using a superfluous wait queue head: #include <linux/sched. h> signed long schedule_timeout(signed long timeout);
Timers are used to schedule execution of a function (a timer handler) at a particular time in the future. They thus work differently from task queues and tasklets in that you can specify when in the future your function will be called, whereas you can't tell exactly when a queued task will be executed.
The Kernel's notion of time The kernel must work with system hardware in order to manage time. The system timer provides the kernel the ability to track the passing of time [1, P. 207]. The system timer uses an electronic time source, like a digital clock or the frequency of the processor.
#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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With