首页 技术 正文
技术 2022年11月6日
0 收藏 847 点赞 387 浏览 2637 个字

在SMP系统中,我们的应用程序经常使用多线程的技术,那么在Linux中如何查看某个进程的多个线程呢?

本文介绍3种命令来查看Linux系统中的线程(LWP)的情况:

在我的系统中,用qemu-system-x86_64命令启动了一个SMP的Guest,所以有几个qemu的线程,以此为例来说明。

1. pstree 命令

查看进程和线程的树形结构关系

  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参数显示进程,并尽量显示其LWP(线程ID)和NLWP(线程的个数)。

  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

上面命令查询结果的第二列为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的序号,命令行参数(包括命令本身)。

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. PID USER    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
  8. 660 root      20   0 1313m 188m 2752 S  2.3  4.9   0:46.78 qemu-sys
  9. 661 root      20   0 1313m 188m 2752 S  2.0  4.9   0:39.44 qemu-sys
  10. 867 root      20   0 15260 1312  960 R  0.3  0.0   0:00.07 top
  11. 1 root    20   0 19444 1560 1252 S  0.0  0.0   0:00.34 init
  12. 2 root    20   0     0  0   0 S  0.0  0.0   0:00.02 kthreadd
  13. ….

在top中也可以查看进程(进程)在哪个CPU上执行的。

执行top后,按f,按j(选中* J: P = Last used cpu (SMP)),然后按空格或回车退出设置,在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) 。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289