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