Multi-thread viewing of processes in Linux

Source: Internet
Author: User

In the SMP system, our applications often use multithreading technology. How can we view multiple threads of a process in Linux?

This article describes how to view the Linux threads (LWP) by using three commands:

In my system, I started an SMP Guest with the qemu-system-x86_64 command, so there are several qemu threads, as shown in this example.

1. pstree command

View the tree structure relationship between processes and threads

 
 
  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 command

-L parameter display process, and try to display its LWP (thread ID) and NLWP (number of threads ).

 
 
  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 

The second column of the preceding command query result is PID, the third column is PPID, the fourth column is LWP, and the sixth column is NLWP.

In addition, the ps command can also view the CPU on which the thread runs. The command is as follows:

 
 
  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 

Each column contains the user ID, process ID, parent process ID, thread ID, and CPU number of the running thread. The command line parameters include the command itself ).

3. top Command

The H command can display the status of each thread. After the top Command, press the H key; or 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. .... 

You can also view the CPU on which the process is executed.

After running the top Command, press f, press j, select * J: P = Last used cpu (SMP), and press space or press enter to exit the settings, in the top display, the P column is the CPU Of the last thread running.

 
 
  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 

For more information, see man pstree, man top, and man ps.

Note:LWP is a lightweight process (light weight process, or thread ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.