美文网首页
linux下查看多线程

linux下查看多线程

作者: shuff1e | 来源:发表于2018-03-31 11:16 被阅读7次
    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
    

    相关文章

      网友评论

          本文标题:linux下查看多线程

          本文链接:https://www.haomeiwen.com/subject/ohrgcftx.html