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


推荐阅读
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • 树莓派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程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • Linux磁盘的分区、格式化的观察和操作步骤
    本文介绍了如何观察Linux磁盘的分区状态,使用lsblk命令列出系统上的所有磁盘列表,并解释了列表中各个字段的含义。同时,还介绍了使用parted命令列出磁盘的分区表类型和分区信息的方法。在进行磁盘分区操作时,根据分区表类型选择使用fdisk或gdisk命令,并提供了具体的分区步骤。通过本文,读者可以了解到Linux磁盘分区和格式化的基本知识和操作步骤。 ... [详细]
  • 本文介绍了Linux系统中正则表达式的基础知识,包括正则表达式的简介、字符分类、普通字符和元字符的区别,以及在学习过程中需要注意的事项。同时提醒读者要注意正则表达式与通配符的区别,并给出了使用正则表达式时的一些建议。本文适合初学者了解Linux系统中的正则表达式,并提供了学习的参考资料。 ... [详细]
  • Ubuntu 9.04中安装谷歌Chromium浏览器及使用体验[图文]
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
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社区 版权所有