Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ...

like image 344
Jamie Avatar asked Sep 09 '11 21:09

Jamie


People also ask

How do I add a delay to a Linux kernel?

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);

What is timer Linux?

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.

How is time managed in kernel space?

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.


2 Answers

#include <linux/delay.h>

...
msleep(100);
...
like image 109
Jamie Avatar answered Oct 22 '22 09:10

Jamie


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.

like image 36
adrianmcmenamin Avatar answered Oct 22 '22 09:10

adrianmcmenamin