Linux 睡眠命令 - 睡眠时间少于 1 毫秒

Bog*_*Bog 0 linux sleep command

如何才能让睡眠时间小于0.001s/1ms?

如果我使用这个,它会显示它只休眠 1 毫秒。可以少睡一点吗?

$ time sleep 0.00001
sleep 0.00001  0,00s user 0,00s system 79% cpu 0,002 total
Run Code Online (Sandbox Code Playgroud)

Fré*_*yer 6

sleep通常使用nanosleep(2)系统调用。但是,尽管这个系统调用具有高精度参数,但实际精度将取决于 Linux 使用的计时器。

\n

通常,Linux 内核是使用该CONFIG_HZ=250选项编译的。(看/boot/config-...)。

\n

扩展CONFIG_HIGH_RES_TIMERS可以提高准确性。请参阅https://elinux.org/High_Resolution_Timers

\n

那里有一个旧的行为,忙等待,但在现在的内核中没有:

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

另请注意,分叉进程并加载sleep程序需要一些时间。在我的系统上,我有:

\n
time sleep 0.000\n\nreal    0m0,007s\nuser    0m0,001s\nsys     0m0,006s\n
Run Code Online (Sandbox Code Playgroud)\n