multithreading 如果线程共享相同的 PID,如何识别它们?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9305992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If threads share the same PID, how can they be identified?
提问by SPSN
I have a query related to the implementation of threads in Linux.
我有一个关于 Linux 中线程实现的查询。
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.
Linux 没有明确的线程支持。在用户空间中,我们可能会使用线程库(如 NPTL)来创建线程。现在,如果我们使用 NPTL,它支持 1:1 映射。
The kernel will use the clone()
function to implement threads.
内核将使用该clone()
函数来实现线程。
Suppose I have created 4 threads. Then it would mean that:
假设我创建了 4 个线程。那么就意味着:
- 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)
.
- 将有 4
task_struct
。 - 在里面
task_struct
,将根据克隆的参数提供共享资源(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND)
。
Now I have the following query:
现在我有以下查询:
- Will the 4 threads have the same PID? If someone can elaborate, how the PIDs are shared.
- How are the different threads identified; is there some TID (thread ID) concept?
- 4 个线程是否具有相同的 PID?如果有人可以详细说明,如何共享 PID。
- 如何识别不同的线程;是否有一些 TID(线程 ID)概念?
回答by paxdiablo
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.
四个线程将具有相同的 PID,但仅限于从上方查看时。什么,你(作为一个用户)调用PID是不是有什么内核(从下面看)调用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.
在内核中,每个线程都有自己的 ID,称为 PID(尽管将其称为 TID 或线程 ID 可能更有意义),并且它们还有一个 TGID(线程组 ID),它是线程的 PID这开始了整个过程。
Simplistically, when a new processis 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.
当一个线程启动另一个线程时,该启动的线程获得自己的 PID(因此调度程序可以独立调度它)但它继承了原始线程的 TGID。
That way, the kernel can happily schedule threads independent of what process they belong to, while processes (thread group IDs) are reported to you.
这样,内核就可以愉快地调度线程,而不管它们属于哪个进程,同时向您报告进程(线程组 ID)。
The following hierarchy of threads may help(a):
以下线程层次结构可能有助于(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
You can see that starting a new process(on the left) gives you a new PID anda 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.
您可以看到启动一个新进程(左侧)会为您提供一个新的 PID和一个新的 TGID(两者都设置为相同的值),而启动一个新线程(右侧)会为您提供一个新的 PID,同时保持相同TGID 作为启动它的线程。
(a)Tremble in aweat my impressive graphical skills :-)
(a)惊叹于我令人印象深刻的图形技能:-)
回答by Jesus Ramos
Threads are identified using PIDs and TGID (Thread group id). They also know which thread is a parent of who so essentially a process shares its PID with any threads it starts. Thread ID's are usually managed by the thread library itself (such as pthread, etc...). If the 4 threads are started they should have the same PID. The kernel itself will handle thread scheduling and such but the library is the one that is going to be managing the threads (whether they can run or not depending on your use of thread join and wait methods).
线程使用 PID 和 TGID(线程组 ID)来标识。他们还知道哪个线程是谁的父线程,因此本质上一个进程与其启动的任何线程共享其 PID。线程 ID 通常由线程库本身管理(例如 pthread 等)。如果启动了 4 个线程,它们应该具有相同的 PID。内核本身将处理线程调度等,但库将管理线程(它们是否可以运行取决于您使用线程连接和等待方法)。
Note: This is from my recollection of kernel 2.6.36. My work in current kernel versions is in the I/O layer so I don't know if that has changed since then.
注意:这是我对内核 2.6.36 的回忆。我在当前内核版本中的工作是在 I/O 层,所以我不知道从那时起是否发生了变化。
回答by SAUNDARYA KUMAR GUPTA
Linux provide the fork()
system call with the traditional functionality of duplicating a process. Linux also provides the ability to create threads using the clone()
system call However , linux does not distinguish between processes and thread.
Linux 为fork()
系统调用提供了复制进程的传统功能。Linux 还提供了使用clone()
系统调用创建线程的能力。不过,linux 不区分进程和线程。