自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux中查看進程的多線程

系統(tǒng) Linux
在SMP系統(tǒng)中,我們的應(yīng)用程序經(jīng)常使用多線程的技術(shù),那么在Linux中如何查看某個進程的多個線程呢?本文介紹3種命令來查看Linux系統(tǒng)中的線程(LWP)的情況。

在SMP系統(tǒng)中,我們的應(yīng)用程序經(jīng)常使用多線程的技術(shù),那么在Linux中如何查看某個進程的多個線程呢?

本文介紹3種命令來查看Linux系統(tǒng)中的線程(LWP)的情況:

在我的系統(tǒng)中,用qemu-system-x86_64命令啟動了一個SMP的Guest,所以有幾個qemu的線程,以此為例來說明。

1. pstree 命令

查看進程和線程的樹形結(jié)構(gòu)關(guān)系

  1. [root@jay-linux ~]# pstree | grep qemu 
  2.      |-terminal-+-bash---qemu-sys---2*[{qemu-system-x8}] 
  3. [root@jay-linux ~]# pstree -p | grep qemu 
  4.         |-terminal(194)-+-bash(196)---qemu-sys(657)-+-{qemu}(660) 
  5.         |              |                             `-{qemu}(661) 

2. ps 命令

-L參數(shù)顯示進程,并盡量顯示其LWP(線程ID)和NLWP(線程的個數(shù))。

  1. [root@jay-linux ~]# ps -eLf | grep qemu 
  2. root     657 196 657  0 3 13:48 pts/1   00:00:00 qemu-sys -m 1024 -smp 2 
  3. root     657 196 660  3 3 13:48 pts/1   00:00:26 qemu-sys -m 1024 -smp 2 
  4. root     657 196 661  2 3 13:48 pts/1   00:00:19 qemu-sys -m 1024 -smp 2 
  5. root     789  9799 10789  0 1 14:02 pts/0   00:00:00 grep --color=auto qemu 

上面命令查詢結(jié)果的第二列為PID,第三列為PPID,第四列為LWP,第六列為NLWP。

另外,ps命令還可以查看線程在哪個CPU上運行,命令如下:

  1. [root@jay-linux ~]# ps -eo ruser,pid,ppid,lwp,psr,args -L | grep qemu 
  2. root     657 196 657   1 qemu-sys -hda smep-temp.qcow -m 1024 -smp 2 
  3. root     657 196 660   1 qemu-sys -hda smep-temp.qcow -m 1024 -smp 2 
  4. root     657 196 661   2 qemu-sys -hda smep-temp.qcow -m 1024 -smp 2 
  5. root     834  9799 10834   1 grep --color=auto qemu 

其中,每一列依次為:用戶ID,進程ID,父進程ID,線程ID,運行該線程的CPU的序號,命令行參數(shù)(包括命令本身)。

3. top 命令

其中H命令可以顯示各個線程的情況。(在top命令后,按H鍵;或者top -H)

  1. [root@jay-linux ~]# top -H 
  2. top - 14:18:20 up 22:32,  4 users,  load average: 2.00, 1.99, 1.90 
  3. Tasks: 286 total,   1 running, 285 sleeping,   0 stopped,   0 zombie 
  4. Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st 
  5. Mem:   3943892k total,  1541540k used,  2402352k free,   164404k buffers 
  6. Swap:  4194300k total,      0k used,  4194300k free,   787768k cached 
  7.   
  8.   PID USER    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 
  9. 660 root      20   0 1313m 188m 2752 S  2.3  4.9   0:46.78 qemu-sys 
  10. 661 root      20   0 1313m 188m 2752 S  2.0  4.9   0:39.44 qemu-sys 
  11. 867 root      20   0 15260 1312  960 R  0.3  0.0   0:00.07 top 
  12.     1 root    20   0 19444 1560 1252 S  0.0  0.0   0:00.34 init 
  13.     2 root    20   0     0  0   0 S  0.0  0.0   0:00.02 kthreadd 
  14. .... 

在top中也可以查看進程(進程)在哪個CPU上執(zhí)行的。

執(zhí)行top后,按f,按j(選中* J: P = Last used cpu (SMP)),然后按空格或回車退出設(shè)置,在top的顯示中會多出P這一列是最近一次運行該線程(進程)的CPU。

  1. PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  P COMMAND 
  2. 661 root      20   0 1313m 188m 2752 S  2.3  4.9   0:44.24 3 qemu-sys 
  3. 660 root      20   0 1313m 188m 2752 S  2.0  4.9   0:51.74 0 qemu-sys 
  4. 874 root      20   0 15260 1284  860 R  0.7  0.0   0:00.32 2 top 
  5.     1 root    20   0 19444 1560 1252 S  0.0  0.0   0:00.34 0 init 
  6.     2 root    20   0     0  0   0 S  0.0  0.0   0:00.02 1 kthreadd 

更多信息,請 man pstree, man top, man ps 查看幫助文檔。

注: LWP為輕量級進程(即:線程),(light weight process, or thread) 。

責(zé)任編輯:奔跑的冰淇淋 來源: 笑遍世界博客
相關(guān)推薦

2021-06-11 11:28:22

多線程fork單線程

2019-02-26 11:15:25

進程多線程多進程

2009-10-28 10:01:57

2021-08-04 23:30:28

Node.js開發(fā)線程

2021-04-20 12:39:52

Node.js多線程多進程

2023-12-13 09:56:13

?多進程多線程協(xié)程

2021-06-11 06:54:35

PythonThreadingMultiproces

2023-04-06 15:22:15

Linux進程系統(tǒng)

2010-01-21 11:25:44

linux多線程線程資源

2010-01-21 11:27:30

linux多線程機制線程同步

2022-05-26 08:31:41

線程Java線程與進程

2023-03-05 16:12:41

Linux進程線程

2020-11-10 15:25:26

SemaphoreLinux翻譯

2022-03-09 17:01:32

Python多線程多進程

2012-06-20 14:07:28

多線程架構(gòu)單線程

2023-11-01 11:20:57

2020-11-12 18:08:05

JavaLinux多線程

2019-06-03 09:13:11

線程進程多線程

2010-06-04 14:31:59

Linux 查看進程

2010-07-26 09:45:09

Perl多進程
點贊
收藏

51CTO技術(shù)棧公眾號