Linux 下通过 ps 命令的不同参数获取线程状态:
~# ps -ef fUID PID PPID C STIME TTY STAT TIME CMD
root 29 0 0 11:10 ? Ss+ 0:00 /storage/.system/.console/bin/python /storage/.system/.console/bin/bu
root 61 29 0 11:10 pts/0 Ss 0:00 \_ bash
root 93 61 0 11:28 pts/0 R+ 0:00 \_ ps -ef f
root 19 0 0 11:10 ? Ss+ 0:00 /storage/.system/.console/bin/python /storage/.system/.console/bin/bu
root 1 0 0 11:10 ? Ss+ 0:00 bash
root 56 1 0 11:10 ? Ss 0:00 nginx: master process /usr/sbin/nginx
www-data 57 56 0 11:10 ? S 0:00 \_ nginx: worker process
www-data 58 56 0 11:10 ? S 0:00 \_ nginx: worker process
www-data 59 56 0 11:10 ? S 0:00 \_ nginx: worker process
www-data 60 56 0 11:10 ? S 0:00 \_ nginx: worker process
其中 PID 是线程编号,PPID 就是主进程编号。
可以看出来, www-data 都是主进程 56 的四个线程 57/58/59/60。 (当然,你的输出可能和这里不一样,但是也能发现,共享一个进程号的线程)
Linux下面没有真正的线程,是一种轻量级的进程模拟,所以 ps 中返回的都是进程编号,只是这里可以看作 PPID 的线程号是 PID。