sleep 命令的 CPU 性能

whi*_*edb 1 bash

这是一个相当初学者的问题。例如,我想知道sleep从 bash 脚本触发命令时会发生什么。该作业仍会运行,但优先级较低?

mur*_*uru 6

通常,sleepGNUsleep程序。该程序调用 GNU 函数xnanosleep,后者又调用 Linuxnanosleep系统调用。和:

nanosleep()  suspends  the execution of the calling thread until either
at least the time specified in *req has elapsed, or the delivery  of  a
signal  that triggers the invocation of a handler in the calling thread
or that terminates the process.
Run Code Online (Sandbox Code Playgroud)

因此,根据定义,该进程并未运行(无论优先级如何),而是暂停。

测试一下:

sleep 10 & ps aux | grep $!
[1] 25682
muru  25682  0.0  0.0   7196   620 pts/13   SN   04:10   0:00 sleep 10
Run Code Online (Sandbox Code Playgroud)

过程的状态SN

   S    interruptible sleep (waiting for an event to complete)
   N    low-priority (nice to other users)
Run Code Online (Sandbox Code Playgroud)

忽略N,我猜这就是它开始时的样子。所以 的有效状态sleepinterruptible sleep