方法一:ps
在ps命令中,“-T”选项可以开启线程查看。下面的命令列出了由进程号为的进程创建的所有线程。
1.$ ps -T -p
tronlong@tronlong-virtual-machine:~$ ps -T -p9439
PID SPID TTY TIME CMD
9439 9439 ? 00:00:49 compiz
9439 9444 ? 00:00:00 gmain
9439 9452 ? 00:00:00 dconf worker
9439 9453 ? 00:00:00 gdbus
9439 9487 ? 00:00:10 compiz
9439 9488 ? 00:00:10 compiz
9439 9491 ? 00:00:10 compiz
9439 9492 ? 00:00:10 compiz
THREAD DISPLAY
H Show threads as if they were processes
-L Show threads, possibly with LWP and NLWP columns
-T Show threads, possibly with SPID column
m Show threads after processes
-m Show threads after processes
显示线程id(SPID)
方法二: top
1、
top命令可以实时显示各个线程情况。要在top输出中开启线程查看,请调用top命令的“-H”选项,该选项会列出所有Linux线程。在top运行时,你也可以通过按“H”键将线程查看模式切换为开或关。
$ top -H
2、
要让top输出某个特定进程并检查该进程内运行的线程状况:
$ top -H -p
tronlong@tronlong-virtual-machine:~$ top -H -p 9439
top - 09:15:52 up 18 min, 3 users, load average: 0.01, 0.05, 0.05
Threads: 8 total, 0 running, 8 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.9 us, 0.9 sy, 0.0 ni, 97.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem: 2032380 total, 1450720 used, 581660 free, 54176 buffers
KiB Swap: 2094076 total, 0 used, 2094076 free. 759720 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9439 tronlong 20 0 1368064 195280 62732 S 39.4 9.6 0:38.12 compiz
9487 tronlong 20 0 1368064 195280 62732 S 13.1 9.6 0:07.79 compiz
9488 tronlong 20 0 1368064 195280 62732 S 6.6 9.6 0:08.16 compiz
9491 tronlong 20 0 1368064 195280 62732 S 6.6 9.6 0:07.75 compiz
9492 tronlong 20 0 1368064 195280 62732 S 6.6 9.6 0:07.73 compiz
9444 tronlong 20 0 1368064 195280 62732 S 0.0 9.6 0:00.02 gmain
9452 tronlong 20 0 1368064 195280 62732 S 0.0 9.6 0:00.00 dconf worker
9453 tronlong 20 0 1368064 195280 62732 S 0.0 9.6 0:00.08 gdbus
PID对应的是ps -T -p9439 中显示的spid
9439 tronlong 20 0 1368064 195280 62732 S 3.3 9.6 1:14.86 compiz
top运行时通过H键来切换是否显示线程,可知虽然显示了线程,但每一列显示的都是对应的进程所占用的资源的。