"Linux" Why check the thread of a process, the PID of all the threads found is different AH

Source: Internet
Author: User

    • It's wrong to say that the Linux threads and processes are the same as those upstairs.

    • See the landlord's problem, feel that the landlord is to be confused by the PID, the thread process will have its own ID, this ID is called Pid,pid is not specifically the process ID, thread ID can also be called PID.

Quote Original

The four threads'll has the same PID but only if viewed from above. What do you (as a user) call a PID is isn't what the kernel (looking from below) calls a PID.

In the kernel, each of the thread has it's own ID, called a PID (although it would possibly make + sense to call this a TID, O R thread ID) and they also has a tgid (thread group ID) which is the PID of the thread that started the whole process.

Simplistically, when a new process was created, it appears as a thread where both the PID and Tgid are the same (new) Numbe R.

When a thread starts another thread, which started thread gets its own PID (so the scheduler can schedule it independently) But it inherits the Tgid from the original thread.

That's the kernel can happily schedule threads independent of what process they belong to, while processes (thread Grou P IDs) is reported to you.

The diagram for thread inheritance is as follows:

USER VIEW<--Pid43-<-----------------pid 42 -----------------> +---------+ | Process | _| pid=42 |_ _/| tgid=42 | \_ (New Thread) _ _ (Fork) _/+---------+/+---------+ +---------+ | Process | | Process | | pid=44 | | pid=43 | | tgid=42 | | tgid=43 | +---------+ +---------+ <--pid Span class= "Hljs-attribute" >43 --> < ---------pid 42 --------> <--- pid 44 ---> KERNEL VIEW  

Here you can see clearly, create a new process will give a new PID and Tgid, and 2 values are the same, when creating a new thread, will give you a new PID, and Tgid and the previous process is consistent.

    • Landlord Press H, switch to the process view, you will find that there is only one left

Suggested landlord do not be confused by the name of the PID, a thing in different perspective is not the same.

Proposed landlord with Htop, clear and convenient, efficient, but also with command line display diagram.

Also attached

Linux uses process to view thread's Method 1). htop Press T (show process thread nesting relationship) and H (display thread), and then F4 filter the process name. 2). ps -eLf | grep java (snapshot, with thread command, E is show all process, L is display thread, f full format output) 3). pstree -p <pid> (Show process tree, no PID display all) 4). top -Hp <pid> (Real-time) 5). ps -T -p <pid> (snapshot) The recommended degree is the number from small to large.

Hope to be accepted!

Resources:

https://segmentfault.com/q/1010000003586656

"Linux" Why check the thread of a process, the PID of all the threads found is different AH

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.