top
和htop
是常用的监控进程和计算机资源的工具,但为什么top
总是自己的状态R
(在S
列top
)?例如,这里是top
命令的屏幕截图:
top - 12:42:33 up 2:48, 1 user, load average: 0,11, 0,17, 0,17
Tasks: 319 total, 1 running, 318 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1,1 us, 0,1 sy, 0,0 ni, 98,8 id, 0,0 wa, 0,0 hi, 0,0 si, 0,0 st
MiB Mem : 15968,5 total, 4031,8 free, 2196,1 used, 9740,7 buff/cache
MiB Swap: 122069,0 total, 122069,0 free, 0,0 used. 13325,4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2264 xxxxxx 20 0 2784640 320344 184092 S 6,0 2,0 9:14.83 Web Content
2195 xxxxxx 20 0 3996144 481160 197788 S 2,7 2,9 7:46.13 firefox
1704 root 20 0 227712 101204 82140 S 1,3 0,6 3:26.54 Xorg
2993 xxxxxx 20 0 817416 54452 41724 S 1,0 0,3 0:04.27 gnome-terminal-
1856 xxxxxx 20 0 4521152 385832 123440 S 0,7 2,4 2:39.79 gnome-shell
1202 root -51 0 0 0 0 S 0,3 0,0 0:52.07 irq/44-nvidia
5048 xxxxxx 20 0 11980 3888 3132 R 0,3 0,0 0:00.12 top
1 root 20 0 167972 11864 8480 S 0,0 0,1 0:03.91 systemd
2 root 20 0 0 0 0 S 0,0 0,0 0:00.00 kthreadd
3 root 0 -20 0 0 0 I 0,0 0,0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0,0 0,0 0:00.00 rcu_par_gp
6 root 0 -20 0 0 0 I 0,0 0,0 0:00.00 kworker/0:0H-kblockd
9 root 0 -20 0 0 0 I 0,0 0,0 0:00.00 mm_percpu_wq
10 root 20 0 0 0 0 S 0,0 0,0 0:00.09 ksoftirqd/0
11 root 20 0 0 0 0 I 0,0 0,0 0:01.49 rcu_sched
12 root rt 0 0 0 0 S 0,0 0,0 0:00.02 migration/0
13 root -51 0 0 0 0 S 0,0 0,0 0:00.00 idle_inject/0
从top
( procps/top
) 的源代码中,它从文件 中获取进程状态/proc/<pid>/stat
,而在top
大多数情况下,S
如果使用以下命令连续打印,则状态为(睡眠):
watch -n.1 "cat /proc/<top-pid>/stat | grep -o \"[S|R]\""