热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

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

这篇文章主要介绍了详解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 
#include 
#include 
#include 
#include 

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 
#include 
#include 
#include 
#include 
#include 
#include 

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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!


推荐阅读
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • Linuxchmod目录权限命令图文详解在Linux文件系统模型中,每个文件都有一组9个权限位用来控制谁能够读写和执行该文件的内容。对于目录来说,执行位的作用是控制能否进入或者通过 ... [详细]
  • 近年来,大数据成为互联网世界的新宠儿,被列入阿里巴巴、谷歌等公司的战略规划中,也在政府报告中频繁提及。据《大数据人才报告》显示,目前全国大数据人才仅46万,未来3-5年将出现高达150万的人才缺口。根据领英报告,数据剖析人才供应指数最低,且跳槽速度最快。中国商业结合会数据剖析专业委员会统计显示,未来中国基础性数据剖析人才缺口将高达1400万。目前BAT企业中,60%以上的招聘职位都是针对大数据人才的。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文主要讨论了在xps15上安装双系统win10和MacOS后,win10无法正常更新的问题。分析了可能的引导问题,并提供了解决方法。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 本文介绍了在Hibernate配置lazy=false时无法加载数据的问题,通过采用OpenSessionInView模式和修改数据库服务器版本解决了该问题。详细描述了问题的出现和解决过程,包括运行环境和数据库的配置信息。 ... [详细]
  • 树莓派Linux基础(一):查看文件系统的命令行操作
    本文介绍了在树莓派上通过SSH服务使用命令行查看文件系统的操作,包括cd命令用于变更目录、pwd命令用于显示当前目录位置、ls命令用于显示文件和目录列表。详细讲解了这些命令的使用方法和注意事项。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • Python语法上的区别及注意事项
    本文介绍了Python2x和Python3x在语法上的区别,包括print语句的变化、除法运算结果的不同、raw_input函数的替代、class写法的变化等。同时还介绍了Python脚本的解释程序的指定方法,以及在不同版本的Python中如何执行脚本。对于想要学习Python的人来说,本文提供了一些注意事项和技巧。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
author-avatar
我的明天谁2502931447
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有