Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 20 down vote accepted

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 }'
share|improve this answer
1  
ps -o nlwp <pid> returns NLWP :), what does that mean ? – Siddharth May 4 '13 at 5:12
1  
@Siddharth NLWP stands for Number of LightWeight Processes which is the number of threads. – jcollado May 7 '13 at 2:45

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.