Multithreading of viewing Processes in Linux

Source: Internet
Author: User

In SMP systems, our applications often use multithreaded technology, so how do you see multiple threads of a process in Linux?

This article describes 3 kinds of commands to view threads (LWP) in Linux systems:

In my system, I started an SMP guest with the qemu-system-x86_64 command, so there are a few qemu threads, as an example to illustrate.

1. Pstree command

View the tree-structured relationships of processes and threads

    1. [Email protected] ~]# Pstree | grep QEMU
    2. |-terminal-+-bash---qemu-sys---2*[{qemu-system-x8}]
    3. [Email protected] ~]# Pstree-p | grep QEMU
    4. |-terminal (194)-+-bash (196)---qemu-sys (657)-+-{qemu} (660)
    5. |                             | '-{qemu} (661)

2. PS command

The-l parameter shows the process and tries to display its LWP (thread id) and NLWP (number of threads) as much as possible.

    1. [[email protected] ~]# 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 above command query results of the second column PID, the third column ppid, the fourth column LWP, the six column NLWP.

In addition, the PS command can see which CPU the thread is running on, with the following command:

    1. [Email protected] ~]# 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

where each column is: User ID, process ID, parent process ID, thread ID, serial number of the CPU running the thread, command-line arguments (including the command itself).

3. Top command

Where the H command can show the situation of each thread. (Press the H key after the top command, or top-h)

  1. [Email protected] ~]# 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 0 1313m 188m 2752 S 2.3 4.9 0:46.78 Qemu-sys
  9. 661 Root 0 1313m 188m 2752 S 2.0 4.9 0:39.44 Qemu-sys
  10. 867 Root 0 15260 1312 960 R 0.3 0.0 0:00.07 Top
  11. 1 Root 0 19444 1560 1252 S 0.0 0.0 0:00.34 Init
  12. 2 Root 0 0 0 0 S 0.0 0.0 0:00.02 Kthreadd
  13. ....

In top, you can also see which CPU the process (process) is executing on.

When top is executed, press F, press J (check * j:p = last used CPU (SMP)), and then press space or enter to exit the setting, and in the top display there will be more P This column is the last CPU that ran the thread (process).

  1. PID USER PR NI VIRT RES SHR S%cpu%MEM time+ P COMMAND
  2. 661 Root 0 1313m 188m 2752 S 2.3 4.9 0:44.24 3 Qemu-sys
  3. 660 root 0 1313m 188m 2752 S 2.0 4.9 0:51.74 0 Qemu-sys
  4. 874 Root 0 15260 1284 860 R 0.7 0.0 0:00.32 2 Top
  5. 1 Root 0 19444 1560 1252 S 0.0 0.0 0:00.34 0 Init
  6. 2 Root 0 0 0 0 S 0.0 0.0 0:00.02 1 Kthreadd

For more information, please see the help documentation for man Pstree, man top, and man Ps.

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

Multithreading of viewing Processes in Linux

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.