How to get the number / id of processes and threads?

Since I am working on Hi3518 with Linux, in order to reduce the use of computing power of the device, I used a simplified version of Linux. So, most of the applications are gone or are simplified without any option. I just summarized the methods I can use on Hi3518 and should be also work on some other Embedded Linux.

In fact, the applications in Linux usually obtain related information by reading the files in /proc. Under /proc, you can see a lot of folder, some of them are in numbers. Those folders named in numbers, which are process ids (PIDs) represent the processes run in the system. Thus, you can see the details of each process in each folder.


where *** are the corresponding process pid.

Obtaining the number, the name and the id of all processes

By using command: top

This only shows the processes which are running. The names are shown in the last column, COMMAND.

Figure 1: by using top

To obtain the total number of processes, you can use wc -l. “wc” prints the number of lines (-l), the number of characters (-m), the number of words (-w) etc. to the command prompt. You may check out more on man page:
But, since top is running, you may press Ctrl + C to stop it before printing you the value. And you need to subtract some lines that is not contributed to the number of processes, like the first 4 lines in Figure 1, 24 – 4 = 20 processes.

By using command: ps

This gives you all the processes run. Similarly, the name are in the last column, and you can use wc -l to obtain the total number of processes. And, don’t forget to subtract some lines!

Figure 2: by using ps


Obtaining the number and the id of threads in each process

By reading the status file in /proc

The status file in each folder of a process states all the details of the process, including the number of thread. Get the pid of the process you want to check first, by running the above command, and then run the following command.

cat /proc/***/status

where *** are the corresponding process pid.
Take pid 138 as an example and there is a item “Threads” with value 1, in which you can know the number of threads. You will see something like this.

Name:   sync_supers
State:  S (sleeping)
Tgid:   138
Pid:    138
PPid:   2
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 32
Threads:        1
SigQ:   0/211
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: ffffffffffffffff
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: ffffffffffffffff
CapEff: ffffffffffffffff
CapBnd: ffffffffffffffff
Cpus_allowed:   1
Cpus_allowed_list:      0
voluntary_ctxt_switches:        7
nonvoluntary_ctxt_switches:     0

To list the thread ids, you can just simply list the directory /proc/***/task with the following command.

ls -l /proc/***/task

where *** are the corresponding process pid.
And you’ll see a list of numbers (the name of directories) again, which are the thread ids of the process. You can also check whether the total number match the number shown in /proc/***/status.
Example: Case of process id 138, only one item in the list, which matches the number shown above.

#The list:
dr-xr-xr-x    5 root     root             0 Jan  1 00:04 138

Hope you find this useful.



Leave a Comment

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