- http://smilejay.com/2012/06/linux_view_threads/
- https://blog.csdn.net/inuyashaw/article/details/55095545
- http://www.361way.com/linux-process-threads/3664.html
- https://segmentfault.com/q/1010000003586656
- https://www.qiancheng.me/post/coding/show-thread
-
https://blog.csdn.net/jason314/article/details/5640969
这个地方写的太好了 - https://stackoverflow.com/questions/9305992/if-threads-share-the-same-pid-how-can-they-be-identified?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
The four threads will have the same PID but only when viewed from above. What you (as a user) call a PID is not what the kernel (looking from below) calls a PID.
In the kernel, each thread has it's own ID, called a PID (although it would possibly make more sense to call this a TID, or thread ID) and they also have a TGID (thread group ID) which is the PID of the thread that started the whole process.
Simplistically, when a new process is created, it appears as a thread where both the PID and TGID are the same (new) number.
When a thread starts another thread, that started thread gets its own PID (so the scheduler can schedule it independently) but it inherits the TGID from the original thread.
That way, the kernel can happily schedule threads independent of what process they belong to, while processes (thread group IDs) are reported to you.
关于线程继承关系图如下:
USER VIEW
<-- PID 43 --> <----------------- PID 42 ----------------->
+---------+
| process |
_| pid=42 |_
_/ | tgid=42 | \_ (new thread) _
_ (fork) _/ +---------+ \
/ +---------+
+---------+ | process |
| process | | pid=44 |
| pid=43 | | tgid=42 |
| tgid=43 | +---------+
+---------+
<-- PID 43 --> <--------- PID 42 --------> <--- PID 44 --->
KERNEL VIEW
网友评论