54

I tried googling it, but I can't find it. I am looking for:

  1. number of threads in process X

  2. total number of threads running currently

1

6 Answers 6

74

To get the number of threads for a given pid:

ps -o nlwp <pid>

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 }'
4
  • 3
    ps -o nlwp <pid> returns NLWP :), what does that mean ?
    – Siddharth
    May 4, 2013 at 5:12
  • 4
    @Siddharth NLWP stands for Number of LightWeight Processes which is the number of threads.
    – jcollado
    May 7, 2013 at 2:45
  • You can suppress the "NLWP" with "h" (hide headers); ie: ps h -o nlwp $pid
    – bufh
    Aug 9, 2016 at 9:00
  • 2
    For total threads, this is simpler and give the same result ps -eLf | wc -l
    – MikeKulls
    Feb 9, 2023 at 23:32
10

For finding the number of threads running a single process you can look at /proc/<pid>/status. It should list the number of threads as one of the fields.

5

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:

echo $(( `ps axms | wc -l`  - 1))

We subtract one this time because there is a header row.

1
  • This is inaccurate, as it reports an extra thread per process Jan 29, 2019 at 17:14
2

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/[0-9]*/task/[0-9]*

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).

1

To get the total number of the threads(tiny pieces of a process running simultaneously) of a you can use the command ps -o nlwp <pid> It works all the time. But if you prefer to try to see it through a file. you should probably look at the files that were created for each and every process of the system. There you can get the ultimate details of the process. For each and every process, there is a folder created in /proc/<pid> there you can see all the other details also.

0

In my case it was useful to count just the total number of running threads for which top in -H mode works very nicely. The full command runs top in batch mode (-b) a single time (-n 1) and strips the top 8 lines. Then counts the rest.

top -bH -n 1 | tail -n +8 | wc -l

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .