- How to get (from terminal) total number of threads (per process and total for all processes)
- Show Threads Using PS Command in Linux
- Show Threads Using PS Command in Linux
- Show Threads with PID
- Show Threads with Application Name
- Show Threads with Filter
- On Linux, in C, how can I get all threads of a process?
- 1 Answer 1
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Is there a way to see details of all the threads that a process has in Linux?
- 10 Answers 10
- Listing threads under Linux
- Current provide answers
- but
- ps with a pid
- ps with an application name
- ps with a filter
How to get (from terminal) total number of threads (per process and total for all processes)
To the get the sum of all threads running in the system:
ps -eo nlwp | tail -n +2 | awk ' < num_threads += $1 >END < print num_threads >'
ps -o nlwp
For finding the number of threads running a single process you can look at /proc//status . It should list the number of threads as one of the fields.
I’m basing this answer around ps axms . ps is a great tool for listing what’s running.
If you want to filter that by a process, you could try something like this:
echo $(( `ps axms | grep firefox | wc -l` - 1))
We subtract 1 because grep will show in that list.
For all threads in general this should work:
We subtract one this time because there is a header row.
On linux specifically, here is one way to do it per-process:
#!/bin/sh while read name val; do if [ "$name" = Threads: ]; then printf %s\\n "$val" break fi done < /proc/"$1"/status
You may then invoke this script with a PID as an argument, and it will report the number of threads owned by that process.
To get the thread count for the whole system, this suffices:
#!/bin/sh count() < printf %s\\n "$#" >count /proc/6*/task/3*
These approaches may seem a little unorthodox in that they rely heavily on shell features, but in return both of them are faster than the corresponding ps and awk -based approaches on my machine (while also not creating extra threads of their own for pipes). Bear in mind that the shell launched to run these scripts will have a thread of its own (or more, if you are using a strange implementation).
Show Threads Using PS Command in Linux
In the modern operating systems, threads serve as a popular programming abstraction. These threads share resources like open files, space, and memory addresses when this happens. It avoids the inter-process communication (expensive IPC) channels and reduces forking overhead. As a result, threads perform a concurrent execution mechanism.
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:
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 . The syntax is as follows:
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 |
You can filter the content through the application name. Type and execute the following command to accomplish it:
You can also filter the threads using their PIDs:
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:
On Linux, in C, how can I get all threads of a process?
How to iterate through all tids of all threads of the current process? Is there some way that doesn't involve diving into /proc ?
Do you have access to a pthread_t handle? Can you hook into those threads post-creation and call pthread_self from them?
Also. one possible approach would be to set the affinity of the thread that creates those threads since CPU affinity mask is inherited.
1 Answer 1
The code I am using, based on reading /proc
#include #include #include #include
Then, from inside a funcion:
DIR *proc_dir; < char dirname[100]; snprintf(dirname, sizeof dirname, "/proc/%d/task", getpid()); proc_dir = opendir(dirname); >if (proc_dir) < /* /proc available, iterate through tasks. */ struct dirent *entry; while ((entry = readdir(proc_dir)) != NULL) < if(entry->d_name[0] == '.') continue; int tid = atoi(entry->d_name); /* . (do stuff with tid) . */ > closedir(proc_dir); > else < /* /proc not available, act accordingly */ >
FYI, snprintf(dirname, sizeof dirname, "/proc/%d/task", getpid()); can be replaced with "/proc/self/task" for current process
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43530
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Is there a way to see details of all the threads that a process has in Linux?
For Windows, I think Process Explorer shows you all the threads under a process. Is there a similar command line utility for Linux that can show me details about all the threads a particular process is spawning? I think I should have made myself more clear. I do not want to see the process hierarcy, but a list of all the threads spawned by a particular process See this screenshot How can this be achieved in Linux? Thanks!
10 Answers 10
The classical tool top shows processes by default but can be told to show threads with the H key press or -H command line option. There is also htop, which is similar to top but has scrolling and colors; it shows all threads by default (but this can be turned off). ps also has a few options to show threads, especially H and -L .
There are also GUI tools that can show information about threads, for example qps (a simple GUI wrapper around ps ) or conky (a system monitor with lots of configuration options).
For each process, a lot of information is available in /proc/12345 where 12345 is the process ID. Information on each thread is available in /proc/12345/task/67890 where 67890 is the kernel thread ID. This is where ps , top and other tools get their information.
My htop does not seem to show me the threads. They mention the H key as a way to swap between showing and hiding threads, but it works as the Help key still.
Listing threads under Linux
Current provide answers
I would like to make it clear that each answer here is providing you with exactly what you have specified, a list of all threads associated with a process, this may not be obvious in htop as it, by default, lists all threads on the system, not just the process but top -H -p works better for example:
top - 00:03:29 up 3 days, 14:49, 5 users, load average: 0.76, 0.33, 0.18 Tasks: 18 total, 0 running, 18 sleeping, 0 stopped, 0 zombie Cpu(s): 22.6%us, 5.7%sy, 4.2%ni, 66.2%id, 0.8%wa, 0.5%hi, 0.1%si, 0.0%st Mem: 2063948k total, 1937744k used, 126204k free, 528256k buffers Swap: 1052220k total, 11628k used, 1040592k free, 539684k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 30170 daniel 20 0 371m 140m 107m S 10.0 7.0 0:31.37 source:src 30066 daniel -90 0 371m 140m 107m S 2.0 7.0 0:07.87 clementine 30046 daniel 20 0 371m 140m 107m S 0.0 7.0 0:32.05 clementine 30049 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.03 clementine 30050 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.31 clementine 30051 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine 30052 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine 30053 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine 30054 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.03 clementine 30055 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine 30056 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine 30057 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.04 clementine 30058 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine 30060 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.16 clementine 30061 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine 30062 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine 30064 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine 30065 daniel 20 0 371m 140m 107m S 0.0 7.0 0:00.00 clementine
As a side note, the thread with -90 is actually a real-time thread.
but
There's also another option which is true CLI: ps . It depends if you want to look for a process id ( pid ), for an application name , or more filters .
It does not work on MacOS or BSD, as there the -T parameter has a different meaning and there is no alternative to show threads.
ps with a pid
$ ps -T -p 30046 PID SPID TTY TIME CMD # this is here for clarity 30046 30046 pts/2 00:00:17 clementine 30046 30049 pts/2 00:00:00 clementine 30046 30050 pts/2 00:00:00 clementine 30046 30051 pts/2 00:00:00 clementine 30046 30052 pts/2 00:00:00 clementine 30046 30053 pts/2 00:00:00 clementine 30046 30054 pts/2 00:00:00 clementine 30046 30055 pts/2 00:00:00 clementine 30046 30056 pts/2 00:00:00 clementine 30046 30057 pts/2 00:00:00 clementine 30046 30058 pts/2 00:00:00 clementine 30046 30060 pts/2 00:00:00 clementine 30046 30061 pts/2 00:00:00 clementine 30046 30062 pts/2 00:00:00 clementine 30046 30064 pts/2 00:00:00 clementine 30046 30065 pts/2 00:00:00 clementine 30046 30066 pts/2 00:00:03 clementine
ps with an application name
$ ps -e -T -C clementine PID SPID TTY TIME CMD # this is here for clarity 30046 30046 pts/2 00:00:17 clementine 30046 30049 pts/2 00:00:00 clementine 30046 30050 pts/2 00:00:00 clementine 30046 30051 pts/2 00:00:00 clementine 30046 30052 pts/2 00:00:00 clementine 30046 30053 pts/2 00:00:00 clementine 30046 30054 pts/2 00:00:00 clementine 30046 30055 pts/2 00:00:00 clementine 30046 30056 pts/2 00:00:00 clementine 30046 30057 pts/2 00:00:00 clementine 30046 30058 pts/2 00:00:00 clementine 30046 30060 pts/2 00:00:00 clementine 30046 30061 pts/2 00:00:00 clementine 30046 30062 pts/2 00:00:00 clementine 30046 30064 pts/2 00:00:00 clementine 30046 30065 pts/2 00:00:00 clementine 30046 30066 pts/2 00:00:03 clementine
ps with a filter
- -e shows all processes
- -T lists all threads
- | pipes the output to the next command
- grep this filters the contents by a (in this case )
$ ps -e -T | grep clementine PID SPID TTY TIME CMD # this is here for clarity 30046 30046 pts/2 00:00:17 clementine 30046 30049 pts/2 00:00:00 clementine 30046 30050 pts/2 00:00:00 clementine 30046 30051 pts/2 00:00:00 clementine 30046 30052 pts/2 00:00:00 clementine 30046 30053 pts/2 00:00:00 clementine 30046 30054 pts/2 00:00:00 clementine 30046 30055 pts/2 00:00:00 clementine 30046 30056 pts/2 00:00:00 clementine 30046 30057 pts/2 00:00:00 clementine 30046 30058 pts/2 00:00:00 clementine 30046 30060 pts/2 00:00:00 clementine 30046 30061 pts/2 00:00:00 clementine 30046 30062 pts/2 00:00:00 clementine 30046 30064 pts/2 00:00:00 clementine 30046 30065 pts/2 00:00:00 clementine 30046 30066 pts/2 00:00:03 clementine
Each of these has the same PID so you know they are in the same process.