鉴于linux下线程的广泛使用 我们怎么查看某个进程拥有的线程id了
现在很多服务的设计 主进程->子进程->线程(比如mysql,varnish)
主进程负责侦听网络上的连接 并把连接发送给子进程 子进程派生线程去处理这些线程
mysql(父进程460,子进程863)
1 460 425 333 ? -1 S 0 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/storage/mysql/backup --pid-file=/storag460 863 425 333 ? -1 Sl 500 21:34 \_ /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/storage/mysql/backup
1、ps
-T Show threads, possibly with SPID column
[[email protected] ~]# ps -T -p 863PID SPID TTY TIME CMD863 863 ? 00:00:00mysqld863 865 ? 00:00:51mysqld863 866 ? 00:01:14mysqld863 867 ? 00:00:58mysqld863 868 ? 00:00:50mysqld863 869 ? 00:00:49mysqld863 870 ? 00:00:52mysqld863 871 ? 00:00:50mysqld863 872 ? 00:00:49mysqld863 873 ? 00:00:51mysqld863 874 ? 00:00:48mysqld863 876 ? 00:04:22mysqld863 877 ? 00:07:54mysqld863 878 ? 00:00:19mysqld863 879 ? 00:00:00mysqld863 882 ? 00:00:00mysqld863 887 ? 00:00:00mysqld863 2408 ? 00:00:00mysqld863 2428 ? 00:00:00 mysqld
我们可以看到子进程863派生出的线程 第一行spid 863是主线程(我们知道主线程就是该进程本身)
2、top
-H : Threads toggle
Starts top with the last remembered ’H’ state reversed. When this toggle is On, all individual threads will be dis-
played. Otherwise, top displays a summation of all threads in a process.
top -H -p 863
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+COMMAND872 mysql 20 0 741m 56m 4972 S 0.3 5.7 0:49.11mysqld876 mysql 20 0 741m 56m 4972 S 0.3 5.7 4:22.76mysqld863 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.06mysqld865 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:51.65mysqld866 mysql 20 0 741m 56m 4972 S 0.0 5.7 1:14.77mysqld867 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:58.15mysqld868 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:50.26mysqld869 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:49.88mysqld870 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:52.48mysqld871 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:50.83mysqld873 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:51.90mysqld874 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:48.10mysqld877 mysql 20 0 741m 56m 4972 S 0.0 5.7 7:55.01mysqld878 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:19.84mysqld879 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.00mysqld882 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.00mysqld887 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.58mysqld2408 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.00mysqld2428 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.06 mysqld
3、pstree
[[email protected] ~]# pstree -Aup -h 460mysqld_safe(460)---mysqld(863,mysql)-+-{mysqld}(865)|-{mysqld}(866)|-{mysqld}(867)|-{mysqld}(868)|-{mysqld}(869)|-{mysqld}(870)|-{mysqld}(871)|-{mysqld}(872)|-{mysqld}(873)|-{mysqld}(874)|-{mysqld}(876)|-{mysqld}(877)|-{mysqld}(878)|-{mysqld}(879)|-{mysqld}(882)|-{mysqld}(887)|-{mysqld}(2408)
`-{mysqld}(2428)
原文:http://www.cnblogs.com/metasequoia/p/4587293.html