A process can simultaneously perform several tasks by concurrent execution of multiple threads. Threads are lightweight as they share address space and resources of the same process. However, each thread has its own thread of control (i.e., registers and stack) same as the control plane of a process. Thereby, threads are Lightweight processes (LWP).
In a multi-threaded process, each thread gets a Thread Group ID and a Thread ID (TID). The Thread Group ID for the threads of the same process is the same as the PID (Process ID). However, the TIDs are unique for threads of a single process.
In this guide, let's learn how to show threads of a process in Linux.
Show Threads of a Process
In Linux, you can get the threads' details, including the number of threads, of a process through several ways.
1. Using ps command
The ps command lists the active processes with their PIDs and additional process information depending on the options.
Get the threads with PID
You can use the ps with -T option to list threads for a given process. The syntax of the ps command to view threads is as follows:
ps -T -p <PID> ps -T -C <application-name>
ps -e -T | grep <filter>
Here, you can replace <PID> with the ID of the process you want to get the thread details.
For example, to view threads of a process with PID 671, type:
ps -T -p 671
PID is the id for the process. The SPID column represents thread ids, and CMD is the thread's name.
Listing all threads of all processes
To get the detailed listing of all the threads currently active, use the following command:
Here, -e selects all the processes, -L shows threads, and -f is for full-format listing.
- PID - process ID
- PPID - parent process ID
- LWP - thread ID
- NLWP - total no of threads per process
- CMD - the name of the thread
If you are interested to know about Thread Group ID use the argument tgid with ps.
Get the number of threads
The NLWP field shows the number of threads for a process. You can use the following command to count threads for a process:
ps -o nlwp <pid>
Here -o specifies user-defined format, nlwp gets the thread count, and <pid> will be replaced with the process ID.
Another way to count the no of threads through ps is to use the following command:
ps --noheader -eL -q <pid> | wc -l
This command gets all the threads for a process given by PID. The wc command with flag -l counts the number of lines. Thereby, you will get total number of threads for this process.
2. Using top Command
You can see a real-time view of the system processes through the top command. The "-H" option enables thread view in top listings. To get all threads associated with a particular process, you can specify the PID using the -p option:
top -H -p 673
The values under the column name PID are actually the Thread IDs.
Another method to find the number of Threads is to add nTH field of top command. To get the Field Management window press f button from the keyboard after opening the top command. Using the down arrow key select nTH row and press the key d from the keyboard. Then press q to quit.
Show the number of threads using Nth field of top:
3. Using htop Command
Htop, an interactive process viewer based on ncurses, provides a more user-friendly method of displaying thread level information for a process. With this tool, you can keep track of certain threads in tree views.
Launch htop and click F2 to access the setup menu. From the Setup column select Display options, then from the Displays options on the right select "Tree view" and "Show custom thread names" by pressing the space bar.
The output will be as follows:
4. Using proc filesystem
The proc file system can also get the thread view of a particular process. The proc directory lists down the directories for all the processes using their PIDs, and each directory contains a directory named task that lists all the threads for that process.
For example, listing the /proc/<pid>/task directory displays the details of all the threads for a process id.
ls -l /proc/<pid>/task
You may count the number of threads for the process by piping wc -l to the command:
ls -l /proc/<pid>/task | wc -l
There is a limit on the maximum number of threads in the kernel. This limit is set in the file /proc/sys/kernel/threads-max. You can view the file using the following command:
For more information such as TGID you can look into /proc/<process-id>/status file.
5. Using pstree Command
The pstree command displays the active processes as a tree, a more practical approach representing the processes hierarchy and improving the output's aesthetics. The tree's root is either the init or the process with the specified pid.
pstree -p <pid>
Where <pid> is an optional parameter. If you do not specify any PID, all the processes for the current user are shown in a tree hierarchy.
[email protected]:~# pstree -p 3734849 php-fpm7.4(3734849)─┬─php-fpm7.4(334344) ├─php-fpm7.4(334345) ├─php-fpm7.4(334346) ├─php-fpm7.4(334347) ├─php-fpm7.4(334348) ├─php-fpm7.4(334349) ├─php-fpm7.4(334435) ├─php-fpm7.4(334440) ├─php-fpm7.4(334441) ├─php-fpm7.4(334442) ├─php-fpm7.4(334443) ├─php-fpm7.4(352993) [email protected]:~#
The output shows all the child threads for the process id 3734849 ( ie
php-fpm7.4). If you count there are a total of 12 threads for the process. The number inside each parenthesis indicates the Thread ID (TID).
The ps and top are the best choices for listing threads associated with the specified process.
lsof -p [PROCESS ID]
This can be useful if you want to see what resources the threads of the process are using or if you want to see which files or sockets the threads are interacting with.