Multithreading of viewing Processes in Linux
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 to view the tree-structured relationships of processes and threads.
View Code BASH
12345 |
[ root @ Jay-linux ~ ] # Pstree | grep qemu | -gnome-terminal-+-bash---qemu-system-x86---2 * [ { qemu-system-x8 } ] [ root @ jay-linux ~ ] # Pstree-p | grep qemu | -gnome-terminal ( 10194 ) -+-bash ( 10196 ) --- Qemu-system-x86 ( 10657 ) -+- { qemu-system-x8 } ( 10660 ) | | ' - { qemu-system-x8 } ( 10661 ) |
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.
View Code BASH
12345 |
[root@jay-linux ~] # Ps-elf | grep qemuroot 10657 10196 10657 0 3 13:48 pts/1 00:00:00 Qemu-system-x86_64-hda smep-temp.qcow-m 1024-smp 2root 10657 10196 10660 3 3 13:48 pts/1< c17/>00:00:26 Qemu-system-x86_64-hda smep-temp.qcow-m 1024-smp 2root 10657 10196 10661 2 3 13:48 Pts
/1 00:00:19 qemu-system-x86_64-hda smep-temp.qcow-m 1024-smp 2root 10789 9799 10789 0< C26/>1 14:02 pts
/0
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:
View Code BASH
12345 |
[root@jay-linux ~] # Ps-eo Ruser,pid,ppid,lwp,psr,args-l | grep qemuroot 10657 10196 10657 1 Qemu-system-x86_64-hda SMEP -temp.qcow-m 1024-smp 2root 10657 10196 10660 1 qemu-system-x86_64-hda smep-temp.qcow-m 1024-smp 2root
10657 10196 10661 2 qemu-system-x86_64-hda smep-temp.qcow-m 1024-smp 2root 10834 9799 10834
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)
View Code BASH
1234567891011121314 |
[Root@Jay-linux ~]# top-hTop-14:18:20 up 22:32, 4Users, load average:2.00, 1.99, 1.90tasks:286 Total, 1 running, 285 sleeping, 0 stopped, 0 zombiecpu(S): 0.0%US, 0.0%Sy, 0.0%ni,100.0%ID, 0.0%WA, 0.0%Hi, 0.0%Si, 0.0%stmem:3943892k Total, 1541540k used, 2402352k Free, 164404k buffersswap:4194300k Total, 0k used, 4194300k Free, 787768k cached PID USER PR NI VIRT RES SHR S%Cpu%MEM time+ COMMAND10660 Root 0 1313m 188m 2752 S 2.3 4.9 0:46.78 qemu-system-x8610661 root 20 0 13 13m 188m 2752 S 2.0 4.9 0:39.44 qemu-system-x8610867 root 0 15260 1312 960 R 0.3 0.0 0:00.07 top 1 R Oot 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 kth Readd .... |
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).
View Code BASH
123456 |
PID USER PR NI VIRT RES %MEM time+ P COMMAND10661 root 0 1313m 188m 2752 S 2.3 4.9 0:44.24 3 qemu-system-x8610660 root 0 1313m 188m 2752 s
2.0 4.9 0:51.74 0 qemu-system-x8610874 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 (i.e. thread), (light weight process, or thread).
A few additional commands:
Strace-f;
Lsof
View all sub-processes: pstree-p PID
View/proc/pid/status to see the current status of some processes
Get the PID of the process:
Ps-ef | grep process_name | Grep-v "grep" | awk ' {print $} '
View the multithreaded Pstree of the process in Linux, ps-l