欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

详解Linux获取线程的PID(TID、LWP)的几种方式

程序员文章站 2022-04-07 19:39:06
在 linux c/c++ 中通常是通过 pthread 库进行线程级别的操作。在 pthread 库中有函数:pthread_t pthread_self(void);它返回一个 pthread_t...

在 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

详解Linux获取线程的PID(TID、LWP)的几种方式

在top中指定了进程pid,输出包含四个线程,通过pid字段可获知每个线程的pid(tid/lwp)。

man top
-h:threads toggle
starts top with the last remembered 'h' state reversed.
when this toggle is on, all individual threads will be displayed.
otherwise, top displays a summation of all threads in a process.
-p:monitor pids

方法四:ls -l /proc/$pid/task/

[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.

方法六:源码级获取

main.c

#define _gnu_source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>

pid_t gettid() {
 return syscall(sys_gettid);
}

void *start_routine(void *arg) {
 pid_t pid = gettid();
 pthread_t tid = pthread_self();
 printf("thd%d: pid=%d, tid=%lu\n", *((int *)arg), pid, tid);

 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() {

 pid_t pid = gettid();
 pthread_t tid = pthread_self();
 printf("main: pid=%d, tid=%lu\n", pid, tid);

 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;
}

syscall(sys_gettid) 系统调用返回一个 pid_t 类型值,即线程在内核中的id。

[test1280@localhost 20190227]$ gcc -o main main.c -lpthread
[test1280@localhost 20190227]$ ./main
main: pid=11278, tid=140429854775040
main: i am main
thd3: pid=11281, tid=140429833787136
thd3: i am thd3
thd2: pid=11280, tid=140429844276992
thd2: i am thd2
thd1: pid=11279, tid=140429854766848
thd1: i am thd1
……

线程的pid(tid、lwp)有什么价值?

很多命令参数的 pid 实际指代内核中线程的id,例如 taskset、strace 等命令。

例如 taskset 命令,可以将进程绑定到某个指定的cpu核心上。

如果进程是多线程模式,直接使用 taskset 将仅仅把主线程绑定,其他线程无法被绑定生效。

example:

# 将 11282 进程绑定到cpu第0核心
[test1280@localhost ~]$ ps -lf 11282
uid   pid ppid lwp c nlwp stime tty  stat time cmd
test1280 11282 9374 11282 0 4 11:33 pts/0 sl+ 0:00 ./main
test1280 11282 9374 11283 0 4 11:33 pts/0 sl+ 0:00 ./main
test1280 11282 9374 11284 0 4 11:33 pts/0 sl+ 0:00 ./main
test1280 11282 9374 11285 0 4 11:33 pts/0 sl+ 0:00 ./main
[test1280@localhost ~]$ taskset -pc 0 11282
pid 11282's current affinity list: 0-3
pid 11282's new affinity list: 0

# 查看其他线程是否真的绑定到cpu第0核心
[test1280@localhost ~]$ taskset -pc 11283
pid 11283's current affinity list: 0-3
[test1280@localhost ~]$ taskset -pc 11284
pid 11284's current affinity list: 0-3
[test1280@localhost ~]$ taskset -pc 11285
pid 11285's current affinity list: 0-3
[test1280@localhost ~]$ taskset -pc 11282
pid 11282's current affinity list: 0
# 此时实际只绑定主线程到cpu第0核心

# 将其他四个线程一并绑定到cpu第0核心
[test1280@localhost ~]$ taskset -pc 0 11283
pid 11283's current affinity list: 0-3
pid 11283's new affinity list: 0
[test1280@localhost ~]$ taskset -pc 0 11284
pid 11284's current affinity list: 0-3
pid 11284's new affinity list: 0
[test1280@localhost ~]$ taskset -pc 0 11285
pid 11285's current affinity list: 0-3
pid 11285's new affinity list: 0
# 此时,进程pid=11282的进程所有线程都将仅在cpu第0核心中运行

strace 同理,可以指定线程pid,追踪某个线程执行的系统调用以及信号。

到此这篇关于详解linux获取线程的pid(tid、lwp)的几种方式的文章就介绍到这了,更多相关linux获取线程的pid内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: Linux 线程 PID