为啥查某个进程的线程,查出来的所有线程的pid不一样啊

发布于 2022-09-01 18:06:30 字数 212 浏览 9 评论 0

在linux下用 top -H -p <pid> 查询某个进程的线程

按理说,都是某个进程下的线程, 应该进程id PID一样啊,但实际却都不一样
图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

执着的年纪 2022-09-08 18:06:30
  • 楼上说的linux线程和进程是一样的,这个说法是错误的。

  • 看了楼主的问题,感觉楼主是被PID给弄混了,线程进程都会有自己的ID,这个ID就叫做PID,PID是不特指进程ID,线程ID也可以叫做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.

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.

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

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.

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

关于线程继承关系图如下:

               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,并且2个值相同,
当创建一个新的线程的时候,会给你一个新的PID,并且TGID和之前开始的进程一致。

  • 楼主按下H,切换到进程视图,会发现只剩下一个了

建议建议楼主不要被名字PID给迷惑,一个东西在不同视角是不一样的。

建议楼主用HTOP,清晰方便,高效,还带命令行显示图。

另外附上

Linux通过进程查看线程的方法 1).htop按t(显示进程线程嵌套关系)和H(显示线程) ,然后F4过滤进程名。2).ps -eLf | grep java(快照,带线程命令,e是显示全部进程,L是显示线程,f全格式输出) 3).pstree -p <pid>(显示进程树,不加pid显示所有) 4).top -Hp <pid> (实时) 5).ps -T -p <pid>(快照) 推荐程度按数字从小到大。

希望能被采纳!

苦行僧 2022-09-08 18:06:30

pthread库里的每一个线程都对应一个内核线程,都是有单独的pid。

聚集的泪 2022-09-08 18:06:30

比如查看多线程应用MySQL的线程:

ps -efL|head -n1 && ps -efL|grep mysqld

UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD
mysql     9333     1  9333  1   6 13:30 ?        00:00:00 /usr/sbin/mysqld
mysql     9333     1  9345  0   6 13:30 ?        00:00:00 /usr/sbin/mysqld
mysql     9333     1  9346  0   6 13:30 ?        00:00:00 /usr/sbin/mysqld
mysql     9333     1  9347  0   6 13:30 ?        00:00:00 /usr/sbin/mysqld
mysql     9333     1  9348  0   6 13:30 ?        00:00:00 /usr/sbin/mysqld
mysql     9333     1  9348  0   6 13:30 ?        00:00:00 /usr/sbin/mysqld

可见线程的PID都是一样的,其中NLWP表示线程组中线程的个数,主线程的PID号等于自身的线程编号LWP.

top -H -p `pidof mysqld` -n1

top里看到的线程PID其实是ps里的线程编号LWP.

寂寞清仓 2022-09-08 18:06:30

其实对内核而言,进程和线程是一样的

空‖城人不在 2022-09-08 18:06:30

对Linux来说,线程就是进程。所有会出现所有的父进程都是一样的。但是在window中就不一样啦,线程就是线程

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文