1

Is it possible to sleep in granularity of microseconds in linux kernel ? From what i know, there is msleep() which can sleep in milliseconds and there is udelay() which busy waits for time in microsecond granularity. But I need to sleep ( not busy wait ) for granularity of microseconds ...

Is there a way to code a new API in linux kernel to allow this ?

Chinna
  • 3,930
  • 4
  • 25
  • 55
  • You may find the useful link below http://stackoverflow.com/questions/85122/sleep-less-than-one-millisecond and this question seems to be dublicated from http://www.spinics.net/lists/kernel/msg1136365.html – Shivaraj Bhat Jan 23 '15 at 08:14
  • 2
    Linux time-slices are on the order of 0.75 ms to 6 ms by default, so sleeping for less than that doesn't really make any sense. `udelay` will try to put the processor into a low-power mode anyway (which is basically what the idle task does regardless, so you aren't missing much). – Mankarse Jan 23 '15 at 08:15
  • 1
    int usleep(useconds_t usec); – LPs Jan 23 '15 at 08:17
  • I found seemingly useful information in http://www.makelinux.net/ldd3/chp-7-sect-3 . – Klas Lindbäck Jan 23 '15 at 08:34
  • Why does it matter if you're busy-waiting or not? If you require/expect other processes to get CPU time when your process is busy, you are probably expecting a bit too much out of Linux' scheduler. – unwind Jan 23 '15 at 08:41

2 Answers2

1

If you need precision of less than a jiffy, hrtimers are your best bet. Enable High-resolution timer config in your kernel, you can use hrtimer APIs like hrtimer_nanosleep(basis for user-space posix nanosleep) that sleeps in nanosecs res

More ref here http://elinux.org/High_Resolution_Timers and https://www.kernel.org/doc/Documentation/timers/hrtimers.txt

sanmara
  • 161
  • 6
0

Yes. The highest resolution wait can be achieved by using ppoll with no FDs and passing a timespec which is in nanoseconds. Note that ppoll will wait at least that amount of time - the underlying clock may be of worse granularity.

abligh
  • 24,573
  • 4
  • 47
  • 84