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
- [Email protected] ~]# Pstree | grep QEMU
- |-terminal-+-bash---qemu-sys---2*[{qemu-system-x8}]
- [Email protected] ~]# Pstree-p | grep QEMU
- |-terminal (194)-+-bash (196)---qemu-sys (657)-+-{qemu} (660)
- | | '-{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.
- [[email protected] ~]# ps -elf | grep qemu
- root 657 196 657 0 3 13:48 pts/1 00:00:00 qemu-sys -m 1024 -smp 2
- root 657 196 660 3 3 13:48 pts/1 00:00:26 qemu-sys -m 1024 -smp 2
- root 657 196 661 2 3 13:48 pts/1 00:00:19 qemu-sys -m 1024 -smp 2
- 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:
- [Email protected] ~]# Ps-eo ruser,pid,ppid,lwp,psr,args-l | grep QEMU
- Root 657 196 657 1 Qemu-sys-hda smep-temp.qcow-m 1024-SMP 2
- Root 657 196 660 1 Qemu-sys-hda smep-temp.qcow-m 1024-SMP 2
- Root 657 196 661 2 Qemu-sys-hda smep-temp.qcow-m 1024-SMP 2
- 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)
- [Email protected] ~]# top-h
- Top-14:18:20 up 22:32, 4 users, Load average:2.00, 1.99, 1.90
- tasks:286 Total, 1 running, 285 sleeping, 0 stopped, 0 zombie
- 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
- mem:3943892k Total, 1541540k used, 2402352k free, 164404k buffers
- swap:4194300k Total, 0k used, 4194300k free, 787768k cached
- PID USER PR NI VIRT RES SHR S%cpu%MEM time+ COMMAND
- 660 root 0 1313m 188m 2752 S 2.3 4.9 0:46.78 Qemu-sys
- 661 Root 0 1313m 188m 2752 S 2.0 4.9 0:39.44 Qemu-sys
- 867 Root 0 15260 1312 960 R 0.3 0.0 0:00.07 Top
- 1 Root 0 19444 1560 1252 S 0.0 0.0 0:00.34 Init
- 2 Root 0 0 0 0 S 0.0 0.0 0:00.02 Kthreadd
- ....
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).
- PID USER PR NI VIRT RES SHR S%cpu%MEM time+ P COMMAND
- 661 Root 0 1313m 188m 2752 S 2.3 4.9 0:44.24 3 Qemu-sys
- 660 root 0 1313m 188m 2752 S 2.0 4.9 0:51.74 0 Qemu-sys
- 874 Root 0 15260 1284 860 R 0.7 0.0 0:00.32 2 Top
- 1 Root 0 19444 1560 1252 S 0.0 0.0 0:00.34 0 Init
- 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