Introduction to Linux: how to view the thread of a process on Linux

Source: Internet
Author: User

Introduction to Linux: how to view the thread of a process on Linux

Q: How can I monitor a single thread after a program creates a thread in it and executes multiple threads? I want to see details of a single thread with their names (for example, CPU/memory usage ).

Threads are a popular abstract concept in programming for parallel execution in modern operating systems. When multiple threads in a program are split to execute multiple streams, these threads will share specific resources (for example, memory Address Space, open files), to minimize the split sale, and avoid a large amount of high-cost IPC (inter-process communication) channel. These functions enable threads to become an efficient mechanism During Concurrent execution.

In Linux, the threads (also called Lightweight processes, LWP) created in a program have the same "thread group ID" as the program PID ". Then, each thread obtains its own thread ID (TID ). For Linux kernel schedulers, threads are just standard processes that share specific resources. Classic command line tools, such as ps or top, can be used to display information at the Thread level, but they only display process-level information by default.

Here we provide several methods to display the thread of a process on Linux.

 

Method 1: PS

In the ps command, the "-T" option can enable thread viewing. The following command lists all threads created by processes whose process number is <pid>.

  1. $ ps -T -p <pid>

The "SID" column indicates the thread ID, while the "CMD" column displays the thread name.

 

Method 2: Top

The top command can display various threads in real time. To enable thread viewing in top output, call the "-H" option of top command. This option lists all Linux threads. During top running, you can also press the "H" key to switch the thread viewing mode to on or off.

  1. $ top -H

To allow top to output a specific process <pid> and check the running thread status in the process:

  1. $ top -H -p <pid>

 

Method 3: Htop

A more user-friendly way is to view the thread of a single process through htop, which is an interactive process viewer Based on ncurses. This program allows you to monitor a single independent thread in the Tree View.

To enable thread viewing in htop, enable htop and press <F2> to enter the htop settings menu. Select "display options" under the "Settings" column, and enable the "Tree View" and "display custom thread name" options. Press <F10> to exit the settings.

Now, you will see the thread view of a single process.

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

Author: Dan Nanni Translator: GOLinux Proofreader: wxy

This article was originally translated by LCTT and launched with the Linux honor in China

This article permanently updates the link address:

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.