12

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

Sagar Jain
  • 7,475
  • 12
  • 47
  • 83
Jamie
  • 7,075
  • 12
  • 56
  • 86
  • Actually, not what I'm looking for: I want to sleep a specified time, not create an asynchronous timer event. – Jamie Sep 09 '11 at 22:41
  • Found it: `extern signed long schedule_timeout(signed long timeout);` – Jamie Sep 09 '11 at 22:57
  • Possible duplicate of [How to sleep in the Linux kernel?](https://stackoverflow.com/questions/15994603/how-to-sleep-in-the-linux-kernel) – Ciro Santilli OurBigBook.com Jun 16 '17 at 06:42
  • @CiroSantilli709大抓捕六四事件法轮功 Check the date. I asked first. – Jamie Jun 21 '17 at 14:48
  • hi, current consensus is to close by "quality": http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha Since "quality" is not measurable, I just go by upvotes. ;-) Likely it comes down to which question hit the best newbie Google keywords on the title. – Ciro Santilli OurBigBook.com Jun 21 '17 at 15:20

2 Answers2

25
#include <linux/delay.h>

...
msleep(100);
...
Jamie
  • 7,075
  • 12
  • 56
  • 86
1

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.

adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44
  • 1
    Okay ... locks, will look it up. But the granularity is going to be, what? +- 10msec? (BTW: your answer is more comment material than answer) – Jamie Sep 09 '11 at 23:15
  • ... and a pointer to something regarding my question and your suggestion of using locks would be much appreciated. Or, better (much better :)), an answer with three or four lines of relevant API code! – Jamie Sep 09 '11 at 23:18