Linux PID 和 TID 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4517301/
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
Difference between PID and TID
提问by apoorv020
What is the difference between PID and TID?
PID 和 TID 有什么区别?
The standard answer would be that PID is for processes while TID is for threads. However, I have seen that some commands use them interchangeably. For example, htop
has a column for PIDs, in which PIDs for threads of the same process are shown (with different values). So when does a PID represent a thread or a process?
标准答案是 PID 用于进程,而 TID 用于线程。但是,我已经看到一些命令可以互换使用它们。例如,htop
有一个 PID 列,其中显示了同一进程的线程的 PID(具有不同的值)。那么PID什么时候代表线程或进程呢?
采纳答案by rodrigo
It is complicated: pidis process identifier; tidis thread identifier.
它很复杂:pid是进程标识符;tid是线程标识符。
But as it happens, the kernel doesn't make a real distinction between them: threads are just like processes but they share some things (memory, fds...) with other instances of the same group.
但碰巧的是,内核并没有真正区分它们:线程就像进程一样,但它们与同一组的其他实例共享一些东西(内存、fds...)。
So, a tidis actually the identifier of the schedulable object in the kernel (thread), while the pidis the identifier of the group of schedulable objects that share memory and fds (process).
所以,一个tid实际上是内核(线程)中可调度对象的标识符,而pid是共享内存和fds(进程)的一组可调度对象的标识符。
But to make things more interesting, when a process has only one thread (the initial situation and in the good old times the only one) the pidand the tidare always the same. So any function that works with a tidwill automatically work with a pid.
但是为了让事情变得更有趣,当一个进程只有一个线程(最初的情况和过去只有一个线程)时,pid和tid总是相同的。所以任何使用tid 的函数都会自动使用pid。
It is worth noting that many functions/system calls/command line utilities documented to work with pidactually use tids. But if the effect is process-wide you will simply not notice the difference.
值得注意的是,许多被记录为使用pid 的函数/系统调用/命令行实用程序实际上使用tids。但如果影响是整个流程的,您将根本不会注意到差异。
回答by ezpz
pid and tid are the same except when a process is created with a call to clone
with CLONE_THREAD
(per the man pages of gettid
). In this case, you get a unique thread id but all threads belonging to the same thread group share the same process id.
pid 和 tid 是相同的,除非通过调用clone
with创建进程CLONE_THREAD
(根据 的手册页gettid
)。在这种情况下,您将获得唯一的线程 ID,但属于同一线程组的所有线程共享相同的进程 ID。
However, I also recall reading (though I cant find the source) that the values returned from getpid
may be cached.
但是,我还记得读过(虽然我找不到来源)从返回的值getpid
可能会被缓存。
[UPDATE]
See the NOTES
section herefor a discussion on the effects of caching pids
.
[更新] 有关缓存效果的讨论,请参阅此处的NOTES
部分。pids
回答by sandeep
Actually, each thread in a Linux process is Light Weight Process (LWP). So, people may call thread as a process... But there is surely a difference. Each thread in a process has a different thread ID (TID) and share the same process ID (PID).
实际上,Linux 进程中的每个线程都是轻量级进程(LWP)。所以,人们可能会称线程为进程......但肯定有区别。进程中的每个线程都有不同的线程 ID (TID) 并共享相同的进程 ID (PID)。
If you are working with pthread library functions, then these functions don't use these TIDs because these are kernel/OS level thread IDs.
如果您正在使用 pthread 库函数,则这些函数不使用这些 TID,因为它们是内核/操作系统级线程 ID。
回答by wick
Just to add to other answers, according to man gettid
:
只是为了添加其他答案,根据man gettid
:
The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e., the opaque value returned by pthread_self(3)).
此调用返回的线程 ID 与 POSIX 线程 ID 不同(即 pthread_self(3) 返回的不透明值)。
So there are two different things one could mean by TID!
因此,TID 有两种不同的含义!