Linux asks: How to view threads for a process on Linux

Source: Internet
Author: User


Original: LCTT https://linux.cn/article-5633-1.html Translator: Golinux
This address: https://linux.cn/article-5633-1.html
2015-6-15 15:44 Favorites: 9
Question: How can I monitor a single thread after my program has created and executed multiple threads inside it? I want to see a single thread detail with their name (for example, cpu/memory usage).

Threads are a popular programming abstraction for parallel execution on modern operating systems. When multiple threads in a program are forked to execute multiple streams, these threads share specific resources (such as memory address space, open files) between them to minimize fork splits and avoid a large number of high-cost IPC (interprocess communication) channels. These features enable threads to become an efficient mechanism when executing concurrently.

In Linux, threads created in the program (also known as lightweight processes, LWP) have the same thread group ID as the PID of the program. Each thread then obtains its own thread ID (TID). For the Linux kernel Scheduler, threads are just the standard process of sharing a specific resource. Classic command-line tools, such as PS or top, can be used to display thread-level information, but they display process-level information by default.

Here are a few ways to show the threads of a process on Linux.

Method One: PS

In the PS command, the "-t" option can turn on thread viewing. The following command lists all threads created by process number <pid> processes.

$ ps-t-P <pid>

The SID column represents the thread ID, and the "CMD" bar displays the thread name.

Method Two: Top

The top command displays the individual thread conditions in real time. To turn on thread viewing in the top output, call the "-H" option of the top command, which lists all Linux threads. In the top run, you can also switch the thread viewing mode to on or off by pressing the "H" key.

$ top-h

To have top output a specific process <pid> and check the thread condition running within that process:

$ top-h-P <pid>

Method Three: Htop

A more user-friendly approach is to view the thread of a single process through Htop, which is a ncurses-based interactive process viewer. This program allows you to monitor individual independent threads in a tree view.

To enable thread viewing in Htop, turn on htop and press <F2> to enter the Htop settings menu. Select display options under the Settings bar, and then turn on the tree view and show custom thread names options. Press <F10> to exit settings.


Now you will see a thread view of a single process such as the following.


Via:http://ask.xmodulo.com/view-threads-process-linux.html

Dan Nanni Translator: Golinux proofreading: Wxy

This article by LCTT original translation, Linux China honors launch

Linux asks: How to view threads for a process on Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.