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
- [root@jay-linux ~]# pstree | grep qemu
- |-terminal-+-bash---qemu-sys---2*[{qemu-system-x8}]
- [root@jay-linux ~]# pstree -p | grep qemu
- |-terminal(194)-+-bash(196)---qemu-sys(657)-+-{qemu}(660)
- | | `-{qemu}(661)
2. ps command
-L parameter display process, and try to display its LWP (thread ID) and NLWP (number of threads ).
- [root@jay-linux ~]# 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 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:
- [root@jay-linux ~]# 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
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)
- [root@jay-linux ~]# 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 20 0 1313m 188m 2752 S 2.3 4.9 0:46.78 qemu-sys
- 661 root 20 0 1313m 188m 2752 S 2.0 4.9 0:39.44 qemu-sys
- 867 root 20 0 15260 1312 960 R 0.3 0.0 0:00.07 top
- 1 root 20 0 19444 1560 1252 S 0.0 0.0 0:00.34 init
- 2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
- ....
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.
- PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ P COMMAND
- 661 root 20 0 1313m 188m 2752 S 2.3 4.9 0:44.24 3 qemu-sys
- 660 root 20 0 1313m 188m 2752 S 2.0 4.9 0:51.74 0 qemu-sys
- 874 root 20 0 15260 1284 860 R 0.7 0.0 0:00.32 2 top
- 1 root 20 0 19444 1560 1252 S 0.0 0.0 0:00.34 0 init
- 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 ).