PID和TID之间的区别 [英] Difference between PID and TID

查看:743
本文介绍了PID和TID之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PID和TID有什么区别?

What is the difference between PID and TID?

标准答案是PID用于进程,而TID用于线程.但是,我已经看到一些命令可以互换使用.例如,htop有一列用于PID的列,其中显示了同一进程的线程的PID(具有不同的值).那么PID何时代表线程或进程?

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 是线程标识符.

It is complicated: pid is process identifier; tid is thread identifier.

但是碰巧的是,内核并没有真正区分它们:线程就像进程一样,但是它们与同一 group的其他实例共享某些东西(内存,fds ...).

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.

因此, tid 实际上是内核(线程)中可调度对象的标识符,而 pid 是共享的可调度对象组的标识符内存和fds(进程).

So, a tid is actually the identifier of the schedulable object in the kernel (thread), while the pid is the identifier of the group of schedulable objects that share memory and fds (process).

但是为了使事情变得更有趣,当一个进程只有一个线程(最初的情况,而在过去的好时光只有一个线程)时, pid tid 总是一样的因此,任何与 tid 一起使用的功能都将自动与 pid 一起使用.

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 pid and the tid are always the same. So any function that works with a tid will automatically work with a pid.

值得注意的是,许多记录为与 pid 一起使用的函数/系统调用/命令行实用工具实际上都使用了 tids .但是,如果效果是全过程的,您将根本不会注意到差异.

It is worth noting that many functions/system calls/command line utilities documented to work with pid actually use tids. But if the effect is process-wide you will simply not notice the difference.

这篇关于PID和TID之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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