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.

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

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.

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.

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