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

在 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 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)的几种方式相关推荐

  1. linux c 获取进程p id,详解Linux获取线程的PID(TID、LWP)的几种方式

    在 Linux C/C++ 中通常是通过 pthread 库进行线程级别的操作. 在 pthread 库中有函数: pthread_t pthread_self(void); 它返回一个 pthrea ...

  2. Linux获取线程id的方法学习

    From: http://www.linuxidc.com/Linux/2014-01/94723.htm 最近一直在想: 如何确认两段代码是不是在同一个线程中执行的呢? 通过查看资料,发现一种比较简 ...

  3. linux 僵尸进程deffunc,多进程-开启子进程的两种方式,查看进程的pid与ppid,僵尸进程与孤儿进程...

    一.开启子进程的两种方式 方式一: # 方式一: from multiprocessing import Process import time def task(name): print(" ...

  4. Linux用户态与内核态通信的几种方式(待完善)

    文章目录 1. 内核启动参数 2.模块参数与sysfs 3.sysctl 4.系统调用 5.netlink 6. procfs(/proc) 7.seq_file 8.debugfs 9.relayf ...

  5. 【Java_多线程并发编程】基础篇—线程状态及实现多线程的两种方式

    1.Java多线程的概念 同一时间段内,位于同一处理器上多个已开启但未执行完毕的线程叫做多线程.他们通过轮寻获得CPU处理时间,从而在宏观上构成一种同时在执行的假象,实质上在任意时刻只有一个线程获得C ...

  6. Linux系统安装JDK级环境变量配置(两种方式多Linux版本)

    文章目录 一.说明 二.第一种方式:通过命令直接安装 二.手动安装方式 三.总结 声明 一.说明 Java JDK在linux系统有两个版本,一个开源版本Openjdk,还有一个oracle官方版本j ...

  7. 随手记——Linux中C语言调用shell指令的三种方式

    在学习资料满天飞的大环境下,知识变得非常零散,体系化的知识并不多,这就导致很多人每天都努力学习到感动自己,最终却收效甚微,甚至放弃学习.我的使命就是过滤掉大量的无效信息,将知识体系化,以短平快的方式直 ...

  8. 在Linux安装配置Tomcat 并部署web应用 ( 三种方式 )

    系统版本:centos6.5版本 java版本:1.7 一.准备工作 1.java -version 检查是否有java环境,没有则需要去安装并配置到环境变量中. 2.下载tomcat包,下载地址:h ...

  9. linux的进程与库之间的通信两种方式

    文章目录 前言 一.进程A与算法库b的通信方式之一:动态dlopen加载算法库b,编译的时候是需要加载该头文件就可以,无需连接该算法库b 具体的实施细节: 二.进程A与算法库b的通信方式之二:进程A编 ...

最新文章

  1. BIRCH聚类算法原理
  2. python中数组的del,remove,pop区别详解
  3. PHP随机配菜_PHP+JS三级菜单联动菜单实现方法
  4. 设计模式C++实现——组合模式
  5. Android开发之RecyclerView之刷新数据notifyDataSetChanged失败的问题
  6. 如何在 ASP.Net Core 使用 分布式缓存
  7. 【Breadth-first Search 】513. Find Bottom Left Tree Value
  8. 卷积神经网络结构优化综述
  9. Java对象初始化执行顺序
  10. Robot Framework(十二) javascript基础
  11. python爬虫有道词典_Python爬取有道词典,有道的反爬很难吗?也就这样啊!
  12. php红盟,php教程_CI框架源码完全分析之核心文件URI.php
  13. 大型网络整体安装与配置解决方案
  14. 语音文字识别基本原理和经典综述
  15. 最新稳定短视频去水印免费解析API接口分享
  16. 编译原理 语法分析程序LL(1)和LR(0)实现
  17. 模拟实现透明网桥的自学习与过滤功能
  18. 2020秋招阿里测试开发一面
  19. 如何描述缺陷(Defect)?
  20. 如何在 Linux 系统中添加桌面图标

热门文章

  1. 什么是长尾关键词?长尾关键词优化方法和技巧
  2. 如何才能够主动学习,给你 2 点建议!
  3. 亚马逊17亿美元收购iRobot;谷歌·Web性能权威指南电子书;宾大·现代统计学习课程资料;轻量化爬虫实现方案;前沿论文 | ShowMeAI资讯日报
  4. react基础之--样式设置
  5. Activity流程框架
  6. 谷歌Play马甲包检测逻辑推测及应对措施
  7. 求职招聘平台(脉脉、BOSS、拉钩、猎聘)之功能体验
  8. 批处理使用技巧:批量修改文件名
  9. 基于 Python 的时序模型——AMIRA模型
  10. 逆水寒服务器维护能进游戏吗,逆水寒7月12日几点可以进游戏 逆水寒更新维护公告...