Linux: How to View Threads of a Process

Objective: View threads of a process on Linux.

Threads (also known as Lightweight Processes (LWP)) are created within a program that will have the same “thread group ID” as the program’s PID. They share their address space and system resources with other LWPs within the same process. Command-line tools such as ps or top, display only process level information by default, but they can be instructed to display thread-level information.

View Threads Using ps

Use either the “-T” or “-L” option of ps to display the threads of a process. The command below displays the threads of the mysqld process.

# pidof mysqld
3692
# ps -T -p 3692
  PID  SPID TTY          TIME CMD
 3692  3692 ?        00:00:27 mysqld
 3692  3707 ?        00:02:26 mysqld
 3692  3708 ?        00:02:30 mysqld
 3692  3709 ?        00:02:31 mysqld
 3692  3710 ?        00:02:36 mysqld
 3692  3711 ?        00:02:36 mysqld
 3692  3712 ?        00:02:31 mysqld
 3692  3713 ?        00:02:31 mysqld
 3692  3714 ?        00:02:33 mysqld
# ps -L -p 3692
  PID   LWP TTY          TIME CMD
 3692  3692 ?        00:00:27 mysqld
 3692  3707 ?        00:02:26 mysqld
 3692  3708 ?        00:02:30 mysqld
 3692  3709 ?        00:02:31 mysqld
 3692  3710 ?        00:02:36 mysqld
 3692  3711 ?        00:02:36 mysqld
 3692  3712 ?        00:02:31 mysqld
 3692  3713 ?        00:02:31 mysqld
 3692  3714 ?        00:02:33 mysqld

If you would like to view the thread id field, use the following syntax.

# ps -To pid,tid,tgid,tty,time,comm -p 3692    
  PID   TID  TGID TT           TIME COMMAND
 3692  3692  3692 ?        00:00:27 mysqld
 3692  3707  3692 ?        00:02:26 mysqld
 3692  3708  3692 ?        00:02:30 mysqld
 3692  3709  3692 ?        00:02:31 mysqld
 3692  3710  3692 ?        00:02:36 mysqld
 3692  3711  3692 ?        00:02:36 mysqld
 3692  3712  3692 ?        00:02:31 mysqld
 3692  3713  3692 ?        00:02:31 mysqld
 3692  3714  3692 ?        00:02:33 mysqld

View Threads Using top

Use the “-H” option of top to view threads. The command below will run top in batch mode and will exit after one iteration.

# top -Hbn1 -p 3692
top - 22:20:52 up 48 days,  1:13,  1 user,  load average: 0.03, 0.05, 0.05
Threads:  22 total,   0 running,  22 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.6 us,  0.4 sy,  0.0 ni, 98.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.2 st
KiB Mem:   2047496 total,  1953516 used,    93980 free,   158964 buffers
KiB Swap:   524284 total,     8252 used,   516032 free.  1246584 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 3692 mysql     20   0  886964  72944   6912 S  0.0  3.6   0:27.23 mysqld
 3707 mysql     20   0  886964  72944   6912 S  0.0  3.6   2:26.93 mysqld
 3708 mysql     20   0  886964  72944   6912 S  0.0  3.6   2:30.84 mysqld
 3709 mysql     20   0  886964  72944   6912 S  0.0  3.6   2:31.88 mysqld
 3710 mysql     20   0  886964  72944   6912 S  0.0  3.6   2:36.61 mysqld
 3711 mysql     20   0  886964  72944   6912 S  0.0  3.6   2:36.66 mysqld
 3712 mysql     20   0  886964  72944   6912 S  0.0  3.6   2:31.58 mysqld
 3713 mysql     20   0  886964  72944   6912 S  0.0  3.6   2:31.40 mysqld
 3714 mysql     20   0  886964  72944   6912 S  0.0  3.6   2:33.09 mysqld

htop is also capable of displaying threads, but it’s interactive and not suitable for batch mode.

View Threads Using proc Filesystem

The proc filesystem will list threads under the /proc/[pid]/task (where [pid] is the pid of the process that we are interested in) directory.

# ls -l /proc/3692/task
total 0
dr-xr-xr-x 7 mysql mysql 0 Apr 17 21:30 3692
dr-xr-xr-x 7 mysql mysql 0 Apr 17 21:30 3707
dr-xr-xr-x 7 mysql mysql 0 Apr 17 21:30 3708
dr-xr-xr-x 7 mysql mysql 0 Apr 17 21:30 3709
dr-xr-xr-x 7 mysql mysql 0 Apr 17 21:30 3710
dr-xr-xr-x 7 mysql mysql 0 Apr 17 21:30 3711
dr-xr-xr-x 7 mysql mysql 0 Apr 17 21:30 3712
dr-xr-xr-x 7 mysql mysql 0 Apr 17 21:30 3713
dr-xr-xr-x 7 mysql mysql 0 Apr 17 21:30 3714

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, perl, php, python, java, javascript, nodejs, angular, react); plays_on(xbox, ps4); linux_desktop_user(true); }