如果线程共享相同的PID,则如何识别它们? [英] If threads share the same PID, how can they be identified?

查看:104
本文介绍了如果线程共享相同的PID,则如何识别它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与Linux中线程实现有关的查询.

I have a query related to the implementation of threads in Linux.

Linux没有显式线程支持.在用户空间中,我们可能会使用线程库(例如NPTL)来创建线程.现在,如果我们使用NPTL,它支持1:1映射.

Linux does not have an explicit thread support. In userspace, we might use an thread library (like NPTL) for creating threads. Now if we use NPTL it supports 1:1 mapping.

内核将使用clone()函数来实现线程.

The kernel will use the clone() function to implement threads.

假设我已经创建了4个线程.那意味着:

Suppose I have created 4 threads. Then it would mean that:

  • 将有4个task_struct.
  • task_struct内,将根据克隆(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND)的参数提供共享资源.
  • There will be 4 task_struct.
  • Inside the task_struct, there will be provision of sharing resources as per the arguments to clone (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND).

现在我有以下查询:

  1. 这4个线程是否具有相同的PID?如果有人可以详细说明,则如何共享PID.
  2. 如何识别不同的线程;有一些TID(线程ID)概念吗?

推荐答案

这四个线程将具有相同的PID,但仅当从上方进行查看时.您是什么 (如用户)调用PID并不是内核(从下面看)称为PID.

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.

内核中,每个线程都有其自己的ID,称为PID(尽管将其称为TID或线程ID可能更有意义),并且它们还具有TGID(线程)组ID),它是启动整个过程的线程的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.

简而言之,当创建一个新的 process 时,它将显示为一个线程,其中PID和TGID都是相同的(新的)数字.

Simplistically, when a new process is created, it appears as a thread where both the PID and TGID are the same (new) number.

当一个线程启动另一个线程时,启动该线程的线程会获得自己的PID(因此调度程序可以独立调度它),但是它会从原始线程继承TGID.

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.

这样,内核可以愉快地调度线程,而与线程所属的进程无关,而将进程(线程组ID)报告给您.

That way, the kernel can happily schedule threads independent of what process they belong to, while processes (thread group IDs) are reported to you.

以下线程层次结构可能会有所帮助(a):

The following hierarchy of threads may help(a):

                      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

您可以看到,启动新的进程(在左侧)会在启动时为您提供新的PID 新的TGID(都设置为相同的值)一个新的 thread (在右侧)为您提供了一个新的PID,同时保持与启动它的线程相同的TGID.

You can see that starting a new process (on the left) gives you a new PID and a new TGID (both set to the same value), while starting a new thread (on the right) gives you a new PID while maintaining the same TGID as the thread that started it.

(a) 敬畏的颤抖,以我令人印象深刻的图形技巧:-)

(a) Tremble in awe at my impressive graphical skills :-)

这篇关于如果线程共享相同的PID,则如何识别它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆