How to get number of threads per process?

When you are debugging the servers it is sometimes very important to get the count of thread each process is running, Here we will see different ways by which we can see the number of threads. Keep in mind that these commands are for ubuntu and may vary for different systems. Let us start and see How to get number of threads per process?

What are threads?

Thread are light weight process which share common memory and data and are very light weight as compare to its counter part processes. Read about threads here . In linux we use threads to speed up certain execution of programs and if these threads are not used properly can cause major performance lag to the program and other programs in the system. To keep it in check we have to keep track of threads and monitor them.

What is a thread? Types of threads and differences between them.

How to get number of threads per process?

ps axo pid,ppid,nlwp,cmd | sort -nr

Here nlwp will list the threads and it stands for number of light weight processes. We are listing process id, parent process id, number of threads and commands which is executed for particular thread.

This command will list the process with threads in 3rd column like below.

How to get number of threads per process?

If you want to get the number of threads for particular process you can use the below command

ps p  pid o pid,ppid,nlwp,cmd

You can also use files in proc directory for this purpose like this

cat /proc/26309/status
26309 is process id

How to get number of threads per process?

We can also use the command below for getting the threads count

ps huH p 26309 | wc -l

This is possible cause everything in linux is files and you can see all the pids in proc directory

If you want all the process status you can use this command

cat /proc/*/status

You can use this commands to do the thread monitoring.
If you like the article please share it with your friends and and like it. This is the end of How to get number of threads per process?

You can read more such articles below.

Command to get top ten processes consuming most CPU

How to get most frequent ip address in your logs.

Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.