鉴于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=/storag 460 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
[root@pdd1 ~]# ps -T -p 863 PID SPID TTY TIME CMD 863 863 ? 00:00:00 mysqld 863 865 ? 00:00:51 mysqld 863 866 ? 00:01:14 mysqld 863 867 ? 00:00:58 mysqld 863 868 ? 00:00:50 mysqld 863 869 ? 00:00:49 mysqld 863 870 ? 00:00:52 mysqld 863 871 ? 00:00:50 mysqld 863 872 ? 00:00:49 mysqld 863 873 ? 00:00:51 mysqld 863 874 ? 00:00:48 mysqld 863 876 ? 00:04:22 mysqld 863 877 ? 00:07:54 mysqld 863 878 ? 00:00:19 mysqld 863 879 ? 00:00:00 mysqld 863 882 ? 00:00:00 mysqld 863 887 ? 00:00:00 mysqld 863 2408 ? 00:00:00 mysqld 863 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+ COMMAND 872 mysql 20 0 741m 56m 4972 S 0.3 5.7 0:49.11 mysqld 876 mysql 20 0 741m 56m 4972 S 0.3 5.7 4:22.76 mysqld 863 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.06 mysqld 865 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:51.65 mysqld 866 mysql 20 0 741m 56m 4972 S 0.0 5.7 1:14.77 mysqld 867 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:58.15 mysqld 868 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:50.26 mysqld 869 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:49.88 mysqld 870 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:52.48 mysqld 871 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:50.83 mysqld 873 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:51.90 mysqld 874 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:48.10 mysqld 877 mysql 20 0 741m 56m 4972 S 0.0 5.7 7:55.01 mysqld 878 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:19.84 mysqld 879 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.00 mysqld 882 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.00 mysqld 887 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.58 mysqld 2408 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.00 mysqld 2428 mysql 20 0 741m 56m 4972 S 0.0 5.7 0:00.06 mysqld
3、pstree
[root@pdd1 ~]# pstree -Aup -h 460 mysqld_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