0

How can I sleep less than 0.001s / 1ms?

If I use this, it shows me that is is only sleeping for 1ms. Is it possible to sleep less?

$ time sleep 0.00001
sleep 0.00001  0,00s user 0,00s system 79% cpu 0,002 total
10
  • I also tried to put it inside a for-loop and see if adding 0s has any effect on it, but it does not. Anything below 0.001 does not work...
    – Bog
    Apr 21, 2023 at 15:05
  • so, that's 0.0000000001s == 0.1 ns by the way. Measurement equipment with a timing accuracy of that is ... expensive. And it makes no sense whatsoever, physically, inside a computer running Linux Apr 21, 2023 at 15:17
  • Considering trying to make a general-purpose computer sleep for a tenth of a nanosecond makes not sense, what are the durations you are actually considering? What are you trying to do in the bigger picture. Apr 21, 2023 at 15:23
  • @MarcusMüller I know it doesn't make any sense for a computer to measure this precisely. And what am I trying to do? As said in the question, my goal is to sleep less than 1ms. And I also know, all those extra 0's may be a bit exaggerated ^^
    – Bog
    Apr 27, 2023 at 7:10
  • 1
    Believe me I was as surprised as you that that worked! Apr 27, 2023 at 9:28

1 Answer 1

6

sleep typically use the nanosleep(2) syscall. But despite this syscall has an high precision parameter, the actual precision will depend of the timers used by Linux.

Typically, a Linux kernel is compiled with the CONFIG_HZ=250 option. (See /boot/config-...).

An extension CONFIG_HIGH_RES_TIMERS can improve the accuracy. See https://elinux.org/High_Resolution_Timers

There where an old behaviour with a busy waiting, but not in nowadays kernel:

  In  order  to  support  applications requiring much more precise pauses
  (e.g., in order to control some  time-critical  hardware),  nanosleep()
  would  handle  pauses  of up to 2 milliseconds by busy waiting with mi‐
  crosecond precision when called from a thread scheduled under  a  real-
  time  policy  like  SCHED_FIFO or SCHED_RR.  This special extension was
  removed in kernel 2.5.39, and is thus not available in Linux 2.6.0  and
  later kernels.

Note also that, forking a process and load a sleep program takes some time. On my system, I have:

time sleep 0.000

real    0m0,007s
user    0m0,001s
sys     0m0,006s
5
  • Really interesting, thanks. Where did you learn about this? :)
    – Bog
    Apr 21, 2023 at 15:08
  • I have added some words about the actual time spend running sleep even with 0ms... Apr 21, 2023 at 15:09
  • as said the accurracy of time is way way way less than that of the underlying sycall, because it needs to launch a process, which looks for an executable file, loads that file, jumps to its initialization code, which reserves memory, opens iostreams, looks for libraries, jumps to the starting function, the actually sleeps, then finishes, then typically closes all standard streams, and finally returns from the execution of that process, which then the time process needs to notice. Timing submillisecond sleep calls using a command line tool is like trying to measure the weight of feathers by Apr 21, 2023 at 15:21
  • … taking a feather and counting how many trucks you need to carry that feather. Apr 21, 2023 at 15:21
  • @Pixelbog the syscall use is in the code (on Debian apt-get source coreutils fetch the code). I had the design of a UNIX kernel in mind and know how to search the CONFIG_HZ option, but it may be obsolete with CONFIG_HIGH_RES_TIMERS And a time sleep 0.000 shows me that it is not with an external command you will be able to do something which is sensible. Note also that syscalls are not "simple" functions. There are more things under the hood (interrupt handling, process handling, restoring the context...) then you shouldn't expect a reactivity comparable with a simple function call. Apr 21, 2023 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .