如何获取进程的线程id?------说说gettid与pthread_self的区别

2024-02-06 10:48

本文主要是介绍如何获取进程的线程id?------说说gettid与pthread_self的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       一切源于需求,需要用到,所以来说说。

 

       之前一直用pthread_self来获取线程id, 这个id通常臭大臭大的。让我纳闷的是,翻遍了所有资料,没有办法通过linux命令来获取线程id, 我不信这个邪。

       当然, 我也查到了, 可以用 ps -Tp pid或者top -Hp pid的方式来获取线程id,  我用了这两个命令,但获取不到如上的线程id. 后来我才明白, 这两个命令确实可以获取到线程id,  但不是上述那个臭大臭大的id, 其实, 这就涉及到gettid与pthread_self的区别了。

       gettid获取的是内核中的真实线程id,  而pthread_self获取的是posix线程id, 不一样的。上述命令获取的线程id与gettid对应, 跟pthread_self没有毛关系。

       pthread_self不说了, 来看看gettid:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#include <pthread.h>void* threadFunc(void* p)
{printf("threadFunc is %d\n",  gettid());int i = 0;while (1){sleep(1);}return NULL;}int main ()
{printf("main thread id is %d\n", gettid());pthread_t id;pthread_create (&id, NULL, threadFunc, NULL);pthread_create (&id, NULL, threadFunc, NULL);pthread_create (&id, NULL, threadFunc, NULL);pthread_create (&id, NULL, threadFunc, NULL);pthread_create (&id, NULL, threadFunc, NULL);pthread_create (&id, NULL, threadFunc, NULL);int i = 0;while (1){sleep(1);}return 0;
}

         编译:g++ test.cpp -lpthread

         运行起来, 然后查看一下:

ubuntu@VM-0-15-ubuntu:~$ ps -aux |grep a.out
ubuntu   26810  0.0  0.0  55692   696 pts/3    Sl+  20:12   0:00 ./a.out
ubuntu   26908  0.0  0.1  13228   972 pts/4    S+   20:13   0:00 grep a.out
ubuntu@VM-0-15-ubuntu:~$ 
ubuntu@VM-0-15-ubuntu:~$ ps -Tp 26810PID  SPID TTY          TIME CMD
26810 26810 pts/3    00:00:00 a.out
26810 26811 pts/3    00:00:00 a.out
26810 26812 pts/3    00:00:00 a.out
26810 26813 pts/3    00:00:00 a.out
26810 26814 pts/3    00:00:00 a.out
26810 26815 pts/3    00:00:00 a.out
26810 26816 pts/3    00:00:00 a.out
ubuntu@VM-0-15-ubuntu:~$ 

       看到没: 主线程 + 6个子线程。 第二列就是线程id.  进程id和主线程id是相等的。

 

       看到这里, 有个疑问, 快速启动多个a.out进程, 如果进程id紧紧挨在一起, 那么线程id是不是就重叠了呢?

       我快速开启4个a.out进程, 来看看:

ubuntu@VM-0-15-ubuntu:~$ ps -aux | grep a.out 
ubuntu   27224  0.0  0.0  55692   752 pts/6    Sl+  20:18   0:00 ./a.out
ubuntu   27232  0.0  0.0  55692   700 pts/5    Sl+  20:18   0:00 ./a.out
ubuntu   27239  0.0  0.0  55692   692 pts/4    Sl+  20:18   0:00 ./a.out
ubuntu   27247  0.0  0.0  55692   692 pts/3    Sl+  20:18   0:00 ./a.out

         可以看到, 进程id并没有紧挨在一起, 给线程id留了空隙, 有点意思。

 

         最后思考一个问题:进程的线程id一定是如上所示的连续的吗?

         当然不一定!

 

      

这篇关于如何获取进程的线程id?------说说gettid与pthread_self的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/684071

相关文章

native和static native区别

本文基于Hello JNI  如有疑惑,请看之前几篇文章。 native 与 static native java中 public native String helloJni();public native static String helloJniStatic();1212 JNI中 JNIEXPORT jstring JNICALL Java_com_test_g

[Linux]:进程(下)

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:Linux学习 贝蒂的主页:Betty’s blog 1. 进程终止 1.1 进程退出的场景 进程退出只有以下三种情况: 代码运行完毕,结果正确。代码运行完毕,结果不正确。代码异常终止(进程崩溃)。 1.2 进程退出码 在编程中,我们通常认为main函数是代码的入口,但实际上它只是用户级

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

Collection List Set Map的区别和联系

Collection List Set Map的区别和联系 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文章。 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否

javascript中break与continue的区别

在javascript中,break是结束整个循环,break下面的语句不再执行了 for(let i=1;i<=5;i++){if(i===3){break}document.write(i) } 上面的代码中,当i=1时,执行打印输出语句,当i=2时,执行打印输出语句,当i=3时,遇到break了,整个循环就结束了。 执行结果是12 continue语句是停止当前循环,返回从头开始。

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令

maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令 在日常的工作中由于各种原因,会出现这样一种情况,某些项目并没有打包至mvnrepository。如果采用原始直接打包放到lib目录的方式进行处理,便对项目的管理带来一些不必要的麻烦。例如版本升级后需要重新打包并,替换原有jar包等等一些额外的工作量和麻烦。为了避免这些不必要的麻烦,通常我们

ActiveMQ—Queue与Topic区别

Queue与Topic区别 转自:http://blog.csdn.net/qq_21033663/article/details/52458305 队列(Queue)和主题(Topic)是JMS支持的两种消息传递模型:         1、点对点(point-to-point,简称PTP)Queue消息传递模型:         通过该消息传递模型,一个应用程序(即消息生产者)可以

线程的四种操作

所属专栏:Java学习        1. 线程的开启 start和run的区别: run:描述了线程要执行的任务,也可以称为线程的入口 start:调用系统函数,真正的在系统内核中创建线程(创建PCB,加入到链表中),此处的start会根据不同的系统,分别调用不同的api,创建好之后的线程,再单独去执行run(所以说,start的本质是调用系统api,系统的api

java 进程 返回值

实现 Callable 接口 与 Runnable 相比,Callable 可以有返回值,返回值通过 FutureTask 进行封装。 public class MyCallable implements Callable<Integer> {public Integer call() {return 123;}} public static void main(String[] args