In Linux, threads are also known as LWP or Lightweight Processes. These threads created within the program have a “thread group ID”, the same as the program’s PID. There is an individual thread ID (TID) assigned to each thread. Scheduler threads in the Linux kernel share a few resources, which are standard processes.
Besides displaying the process-level information by default, classic command-line tools, such as top and ps, can display the thread-level information as well. Ps, top, and htop are classic commands that show you the process-level information by default. You can also view the thread-level information using the previous commands. This guide will only go through the ps command and explain how the ps command shows threads in Linux.
Show Threads Using PS Command in Linux
With the help of the ps command, you can view the threads with many filters like PID (Process ID), application name, etc. This command does not work on BSD or macOS, as there is no option to show that the thread and the -t parameter have a different meaning.
Let’s start with the following command in the terminal to list all the threads using the ps command:
Show Threads with PID
Here is the standard syntax of ps commands to view the threads using PIDs:
ps -T p <pid>
-T | List all threads |
-p | Specifies the process ID |
You can view the threads for specific PIDs. Follow the previous standard syntax and put the PID number which you want to know the threads.
For example, we use the PID <1904>. The syntax is as follows:
ps -Tp 1904
The “SPID” column shows the thread IDs in the previous output. And the “CMD” column represents the thread names.
Show Threads with Application Name
Here is the standard syntax of the ps command to view the threads using the application name:
Option | Description |
---|---|
-T | List all threads |
-C | Specifies the application name |
For example, let’s find out the thread related to the Bash application. Here is the basic command:
Show Threads with Filter
The standard syntax of the ps command is to view the threads using the filter as follows:
Option | Description |
---|---|
-T | List all threads |
-e | Shows all processes |
| | Pipes the output to the next command |
grep | It filters the content with the help of <filter> |
You can filter the content through the application name. Type and execute the following command to accomplish it:
ps -e -T | grep bash
You can also filter the threads using their PIDs:
ps -e -T | 1904
In the previous output, you may notice that all the threads have the same PID. It means that all the previous threads are in the same process.
To find the sum of all the running threads in the system, you can execute the following command:
You can use the “pgrep” or “pidof” to get the process name’s process id:
Or
Conclusion
In Linux, threads are created by a program with the “thread group ID” like the PID. There are multiple ways to show the threads in Linux using various commands. In this guide, we explained the different methods to view the threads using the ps command.
There is an advantage to using the ps command because it shows you all the details that you want. You can filter and view the threads using their PIDs, application name, etc. You can also monitor the thread count through the ps command.