Count the number of threads in a process on linux

Information TechnologyLinux

Linux process can be visualized as a running instance of a program where each thread in the Linux is nothing but a flow of execution of the processes. Do you know how to see the number of threads per process on Linux environment? There are several ways to count the number of threads. This article deals with, how to read the information about processes on Linux and also to count the number of threads per process.

Read Process Information

To read the process information use ‘ps’ command. This command is used to read a snapshot of the current processes on Linux. However, ps -e or ps aux command displays the names of all processes.

To read the process information, use the following command –

$ ps

The sample output should be like this –

PID TTY TIME CMD
5843 pts/0 00:00:00 bash
5856 pts/0 00:00:00 ps

To display all process names, use the following command –

$ ps -e

The sample output should be like this –

PID TTY     TIME CMD
1 ?       00:00:01 init
2 ?       00:00:00 kthreadd
3 ?       00:00:00 ksoftirqd/0
5 ?       00:00:00 kworker/0:0H
7 ?       00:00:07 rcu_sched
8 ?       00:00:00 rcu_bh
9 ?       00:00:02 rcuos/0
10 ?       00:00:00 rcuob/0
11 ?       00:00:00 migration/0
12 ?       00:00:00 watchdog/0
13 ?       00:00:00 watchdog/1
14 ?       00:00:00 migration/1
15 ?       00:00:00 ksoftirqd/1
17 ?       00:00:00 kworker/1:0H
18 ?       00:00:01 rcuos/1
19 ?       00:00:00 rcuob/1
20 ?       00:00:00 watchdog/2
21 ?       00:00:00 migration/2
22 ?       00:00:00 ksoftirqd/2
24 ?       00:00:00 kworker/2:0H
25 ?       00:00:04 rcuos/2
26 ?       00:00:00 rcuob/2
27 ?       00:00:00 watchdog/3
28 ?       00:00:00 migration/3
29 ?       00:00:00 ksoftirqd/3
31 ?       00:00:00 kworker/3:0H
32 ?       00:00:01 rcuos/3
33 ?       00:00:00 rcuob/3
34 ?       00:00:00 khelper
35 ?       00:00:00 kdevtmpfs
36 ?       00:00:00 netns
37 ?       00:00:00 perf
38 ?       00:00:00 khungtaskd
39 ?       00:00:00 writeback
40 ?       00:00:00 ksmd
41 ?       00:00:00 khugepaged
42 ?       00:00:00 crypto
43 ?       00:00:00 kintegrityd
44 ?       00:00:00 bioset
45 ?       00:00:00 kblockd
46 ?       00:00:00 ata_sff
............

Counting Threads per Process

There are several ways to count the threads per process. They are shown as below-

Method 1 – /proc

This is the easiest way to see the thread count of any active process on a Linux machine. proc command exports text file of process and system hardware information, such as CPU, interrupts, memory, disk, etc.

To see the thread count of process, use the following command-

$ cat /proc/<pid>/status

For example, here we are adding PID as 1041. Then, the command should be like this –

$ cat /proc/1041/status

The sample output should be like this-

Name: cups-browsed
State: S (sleeping)
Tgid: 1041
Ngid: 0
Pid: 1041
PPid: 1
TracerPid: 0
Uid: 0 0 0 0
Gid: 0 0 0 0
FDSize: 64
Groups: 0
VmPeak: 75364 kB
VmSize: 75364 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 5932 kB
VmRSS: 5932 kB
VmData: 568 kB
VmStk: 136 kB
VmExe: 48 kB
VmLib: 8712 kB
VmPTE: 164 kB
VmSwap: 0 kB
Threads: 1
SigQ: 0/31329
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000001000
SigCgt: 0000000180004a02
CapInh: 0000000000000000
CapPrm: 0000003fffffffff
CapEff: 0000003fffffffff
CapBnd: 0000003fffffffff
Seccomp: 0
Cpus_allowed: f
Cpus_allowed_list: 0-3
Mems_allowed: 00000000,00000001
Mems_allowed_list: 0
voluntary_ctxt_switches: 134
nonvoluntary_ctxt_switches: 1

The above example is having one thread per process. An alternative way is to count the number of directories found in /proc/<pid>/task.

Because, every thread which is created in a process, there will be a respective directory created in /proc/<pid>/task, named with its thread ID. Thus, the total number of directories in /proc/<pid>/ task represents the number of threads in the process.

To verify it use the following command –

$ ls /proc/<pid>/task | wc

In the above command, we are giving PID as 1041, Then, the command should be like this –

$ ls /proc/1041/task | wc

The sample output should be like this-

tp@linux:~$ ls /proc/1041/task |wc
1 1 5

The above output is describing about 1041 processes and it is having one directory in it.

Method 2  – ps

The ps command shows the individual threads with “H” option. The following command shows the thread count of the process.

$ ps hH p 1041 | wc -l

The output should be like this-

tp@linux:~$ ps hH p 1041 | wc -l
1
tp@linux:~$

Congratulations! Now, you know “How to count the number of threads in a process on Linux”. We’ll learn more about these types of commands in our next Linux post. Keep reading!

raja
Published on 21-Oct-2019 16:05:39
Advertisements