在 Linux C/C++ 中通常是通过 pthread 库进行线程级别的操作。
在 pthread 库中有函数:
pthread_t pthread_self(void);
它返回一个 pthread_t 类型的变量,指代的是调用 pthread_self 函数的线程的 “ID”。
怎么理解这个“ID”呢?
这个“ID”是 pthread 库给每个线程定义的进程内唯一标识,是 pthread 库维持的。
由于每个进程有自己独立的内存空间,故此“ID”的作用域是进程级而非系统级(内核不认识)。
其实 pthread 库也是通过内核提供的系统调用(例如clone)来创建线程的,而内核会为每个线程创建系统全局唯一的“ID”来唯一标识这个线程。
这个系统全局唯一的“ID”叫做线程PID(进程ID),或叫做TID(线程ID),也有叫做LWP(轻量级进程=线程)的。
如何查看线程在内核的系统全局唯一“ID”呢?大体分为以下几种方式。
测试代码:
main.c
#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> void *start_routine(void *arg) { char msg[32] = ""; snprintf(msg, sizeof(msg)-1, "thd%d: i am thd%d\n", *((int *)arg), *((int *)arg)); while (1) { write(1, msg, strlen(msg)); sleep(1); } } int main() { int th1 = 1; pthread_t tid1; pthread_create(&tid1, NULL, start_routine, &th1); int th2 = 2; pthread_t tid2; pthread_create(&tid2, NULL, start_routine, &th2); int th3 = 3; pthread_t tid3; pthread_create(&tid3, NULL, start_routine, &th3); const char *msg = "main: i am main\n"; while (1) { write(1, msg, strlen(msg)); sleep(1); } return 0; }
在主线程中通过 pthread 库创建三个线程,不断输出 “i am xxx” 的信息。
运行输出:
[test1280@localhost 20190227]$ gcc -o main main.c -lpthread
[test1280@localhost 20190227]$ ./main
main: i am main
thd2: i am thd2
thd3: i am thd3
thd1: i am thd1
thd2: i am thd2
……
方法一:ps -Lf $pid
[test1280@localhost ~]$ ps -Lf 11029 UID PID PPID LWP C NLWP STIME TTY STAT TIME CMD test1280 11029 9374 11029 0 4 10:58 pts/0 Sl+ 0:00 ./main test1280 11029 9374 11030 0 4 10:58 pts/0 Sl+ 0:00 ./main test1280 11029 9374 11031 0 4 10:58 pts/0 Sl+ 0:00 ./main test1280 11029 9374 11032 0 4 10:58 pts/0 Sl+ 0:00 ./main
11209是待观察的进程的PID。
输出中可见此进程包含4个线程,他们的PID都是11209,PPID都是9374,其中LWP即我们要找的线程ID。
我们注意到有一个线程的LWP同进程的PID一致,那个线程就是主线程。
-L Show threads, possibly with LWP and NLWP columns -f does full-format listing.
方法二:pstree -p $pid
[test1280@localhost ~]$ pstree -p 11029 main(11029)─┬─{main}(11030) ├─{main}(11031) └─{main}(11032)
方法三:top -Hp $pid
[test1280@localhost ~]$ top -Hp 11029
在top中指定了进程PID,输出包含四个线程,通过PID字段可获知每个线程的PID(TID/LWP)。
man top -H:Threads toggle Starts top with the last remembered 'H' state reversed. When this toggle is> [test1280@localhost ~]$ ls -l /proc/11029/task/ total 0 dr-xr-xr-x. 6 test1280 test1280 0 Feb 27 10:58 11029 dr-xr-xr-x. 6 test1280 test1280 0 Feb 27 10:58 11030 dr-xr-xr-x. 6 test1280 test1280 0 Feb 27 10:58 11031 dr-xr-xr-x. 6 test1280 test1280 0 Feb 27 10:58 11032
方法五:pidstat -t -p $pid
[test1280@localhost ~]$ pidstat -t -p 11029 Linux 2.6.32-642.el6.x86_64 (localhost.localdomain) 02/27/2019 _x86_64_ (4 CPU) 11:20:39 AM TGID TID %usr %system %guest %CPU CPU Command 11:20:39 AM 11029 - 0.00 0.00 0.00 0.00 1 main 11:20:39 AM - 11029 0.00 0.00 0.00 0.00 1 |__main 11:20:39 AM - 11030 0.00 0.00 0.00 0.00 1 |__main 11:20:39 AM - 11031 0.00 0.00 0.00 0.00 0 |__main 11:20:39 AM - 11032 0.00 0.00 0.00 0.00 3 |__main
TGID是线程组ID,主线程的TID等同于主线程的线程组ID等同于主线程所在进程的进程ID。
man pidstat -t Also display statistics for threads associated with selected tasks. This option adds the following values to the reports: TGID:The identification number of the thread group leader. TID:The identification number of the thread being monitored.详解Linux获取线程的PID(TID、LWP)的几种方式
扫一扫手机访问
