How to Count the Number of Threads in a Process on Linux

LinuxHardwareComputers

In this article, we will know how to count the number of threads in a process on Linux environment, there are several ways to do it, but we will learn using ‘/proc’ and ‘ps’ command.

Using ‘/proc’

In Proc pseudo file system, this resides in /proc directory, and this is the easiest way to see the thread count of the task or active process. The ‘proc’ directory exports the form of readable text files to give the information related to the existing processes and the system hardware such as Interrupts, CPU, memory, disk, etc.

# cat /proc/<pid>/status

The above command is the general syntax and this will show detailed information about the process with <pid>, this includes process state (parent PID, UID, GID, sleeping and running status, the no of files used and no of contest). It also indicates the total no of threads and the process.

For example, we want to know the process count of a process id 2907 which is the process id of apache on my Linux environment then we can see using this below command.

# cat /proc/2907/status
Name:    php-fpm
State:   S (sleeping)
Tgid:    2907
Pid:     2907
PPid:    2124
TracerPid:    0
Uid:    48    48    48    48
Gid:    48    48    48    48
Utrace: 0
FDSize: 64
Groups: 48
VmPeak:    445536 kB
VmSize:    382172 kB
VmLck:          0 kB
VmHWM:      84688 kB
VmRSS:      21856 kB
VmData:     17376 kB
VmStk:         88 kB
VmExe:       3396 kB
VmLib:      49280 kB
VmPTE:        548 kB
VmSwap:         0 kB
Threads:        1
SigQ: 0/15205
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000001000
SigCgt: 0000000184000004
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: ffffffffffffffff
Cpus_allowed: 1
Cpus_allowed_list: 0
Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list: 0
voluntary_ctxt_switches:    11419
nonvoluntary_ctxt_switches: 25334

In this above example, we can see the Threads: 1, means the apache process is using only one thread.

We can also use this command to find the count of the no of directories found in the process, for that we can use the below command –

# /proc/<pid>/task | wc

This will show that in every thread that created in the process, there is a corresponding directory will be created in the /proc/<pid>/task, named with its ID.

For example, run the below command

# ls /proc/2907/status | wc
1 1 18

Using ‘PS’

The ‘ps’ command will display the running process of the system by adding the ‘H’ option to the command which can print the thread count for the process. The ‘h’ option will hide the headers at the top of the output.

# ps hH p 14487 | wc -l
1

In this above example, we can see the Threads: 1, means the ‘apache’ process is using only one thread

From this article we can now able to find out the no of process count used for the process ID or the process, we can use any method to find out the count of the processes running on the Linux environment.

raja
Published on 21-Jan-2020 14:38:26
Advertisements