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

查看:24
本文介绍了如果线程共享相同的 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) calls a PID is not what the kernel (looking from below) calls a PID.

内核中,每个线程都有自己的 ID,称为 PID,尽管将其称为 TID 或线程 ID 可能更有意义,而且它们还有一个 TGID(线程group ID),它是进程创建时创建的第一个线程的PID.

In the kernel, each thread has its 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 first thread that was created when the process was created.

当一个新的进程被创建时,它显示为一个线程,其中 PID 和 TGID 是相同的(当前未使用的)编号.

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

当一个线程启动另一个线程时,这个新线程会得到自己的 PID(所以调度器可以独立调度它)但它继承了原始线程的 TGID.

When a thread starts another thread, that new 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
                         vvvv vvvv
              |          
<-- PID 43 -->|<----------------- PID 42 ----------------->
              |                           |
              |      +---------+          |
              |      | process |          |
              |     _| pid=42  |_         |
         __(fork) _/ | tgid=42 | \_ (new thread) _
        /     |      +---------+          |       
+---------+   |                           |    +---------+
| process |   |                           |    | process |
| pid=43  |   |                           |    | pid=44  |
| tgid=43 |   |                           |    | tgid=42 |
+---------+   |                           |    +---------+
              |                           |
<-- PID 43 -->|<--------- PID 42 -------->|<--- PID 44 --->
              |                           |
                        ^^^^^^ ^^^^
                        KERNEL VIEW

您可以看到启动一个新的进程(在左侧)会为您提供一个新的 PID 一个新的 TGID(两者都设置为相同的值).启动一个新的线程(在右侧)会为您提供一个新的 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). Starting a new thread (on the right) gives you a new PID while maintaining the same TGID as the thread that started it.

(a) 惊叹于我令人印象深刻的图形技能 :-)

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

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