为什么“运行”进程这么少?

How*_*ard 6 process scheduling top

在所有 BSD/Linux/MacOS 和 Solaris 上,我做了一个观察——无论系统有多忙,top总是报告很少(通常是 1-2 个)正在运行的进程。

根据我有限的理解,如果进程正在使用 CPU 时间或等待 CPU 时间可用,则该进程处于“运行”状态。

所以,这是一个场景:我使用 SSH 来建立 VNC 隧道,然后top在远程机器上的终端模拟器中运行:

  • top 本身被列为“正在运行”的进程(可以理解)
  • sshd 已列出,但未“运行”
  • Xvnc 已列出,但未“运行”

但是sshd是隧道 VNC 流量,Xvnc是什么让我更新屏幕内容,他们怎么能不使用 CPU 时间?

如果他们正在使用 CPU 时间,那么他们怎么不“运行”呢?

ter*_*don 6

我相信这是一个时间问题。top每 N 秒检查一次 CPU 状态。在检查运行的那一刻,top被激活,因为它正在积极地检查系统的状态。看到这一点的一个好方法是减少更新时间top并观察您的进程移动!如建议man top

   o  The  user  interface,  through  prompts and help, intentionally
      implies that the delay interval is limited to tenths of a  sec?
      ond.   However,  you're  free to set any desired delay.  If you
      want to see Linux at his scheduling best, try a  delay  of  .09
      seconds or less.

      For this experiment, under x-windows open an xterm and maximize
      it.  Then do the following:
        . provide a scheduling boost and tiny delay via:
            nice -n -10 top -d.09
        . keep sorted column highlighting Off so as to
          minimize path length
        . turn On reverse row highlighting for emphasis
        . try various sort columns (TIME/MEM work well),
          and normal or reverse sorts to bring the most
          active processes into view

      What you'll see is a very busy Linux  doing  what  he's  always
      done  for you, but there was no program available to illustrate
      this.
Run Code Online (Sandbox Code Playgroud)

如果您尝试此操作,您应该会看到更多进程进入R模式。您还可以尝试top在批处理模式下运行几次并收集标记为正在运行的进程:

sudo nice -n -10 top -d.09 -bn 100 | awk '$8~/R/{print $NF}' | sort -u
Run Code Online (Sandbox Code Playgroud)

在我当前负载下的系统上(包括一个开放的 ssh 隧道),我得到以下进程:

cinnamon
firefox
kworker/0:1
kworker/1:2
kworker/2:2
kworker/3:0
plugin-containe
ssh
tint2
top
Xorg
Run Code Online (Sandbox Code Playgroud)