摘自《Linux 环境编程:从应用到内核》

  在 Linux 中,目前的线程实现是 Native POSIX Thread Library,简称 NPTL。在这种实现下,线程又被称为轻量级进程(Light Weighted Process),每一个用户态的线程,在内核中都有一个调度实体,也拥有自己的进程描述符。

对于进程,可以使用下面的系统调用,获取进程 ID

pid_t getpid(void);

如:

 #include <stdio.h>
#include <sys/types.h>
#include <unistd.h> int main()
{
pid_t pid = getpid();
printf("pid = %d\n", pid);
while();
return ;
}

打印:

jingyg@jingyg:~/share/mytest/linux_userspace/$ gcc -g -Wall 0_1_hello_world.c -o hello_world
jingyg@jingyg:~/share/mytest/linux_userspace/$ ./hello_world
pid =

使用 ps 命令查看进程 ID,(加 -L 选项可以显示线程信息,LWP:线程 ID(调用 gettid()系统调用的返回值),NLWP:线程组内的线程个数)

jingyg@jingyg:~$ ps -efL
UID PID PPID LWP C NLWP STIME TTY TIME CMD
jingyg : pts/ :: ./hello_world

可以看到 PID 和 LWP 的值一样。
多线程的进程,又被称作线程组,线程组内的第一个线程,在用户态被称作主线程(main thread)。内核在创建第一个线程时,会将线程组 ID (即进程 ID) 的值设置为第一个线程的线程 ID。

虽然 Linux 提供了 gettid 系统调用来返回线程 ID,但是可惜的是 glibc 并没有将该系统调用封装并开放给程序员使用。如果确实需要获取线程 ID,可以采用如下方法:

#include <sys/syscall.h>
int TID = syscall(SYS_gettid);

 #include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h> int main()
{
pid_t pid = getpid();
printf("pid = %d\n", pid); int tid = syscall(SYS_gettid);
printf("tid = %d\n", tid);
return ;
}

打印:

jingyg@jingyg:~/share/mytest/linux_userspace/$ ./hello_world
pid =
tid =

pthread_self()

pthread 库中有一个 pthread_self() 接口用来获取线程 ID,但是这个 ID并不是内核中那个线程 ID

#include <pthread.h>
pthread_t pthread_self(void);

pthread_t 到底是个什么样的数据结构呢?因为 POSIX 标准并没有限制 pthread_t 的数据类型,所以该类型取决于具体实现。对于 Linux 目前使用的 NPTL 实现而言,pthread_t 类型的线程 ID,本质上就是一个进程地址空间上的一个地址,而且 pthread_t 类型的线程 ID很有可能被复用:

 #include <stdio.h>
#include <pthread.h> void* thread_work(void* param)
{
printf("pthread_self : %p\n", (void*)pthread_self());
return NULL;
} int main()
{
pthread_t tid = ;
int ret = pthread_create(&tid, NULL, thread_work, NULL);
ret = pthread_join(tid, NULL); ret = pthread_create(&tid, NULL, thread_work, NULL);
ret = pthread_join(tid, NULL);
return ;
}

打印:(编译 pthread 相关接口时,需要加上 -lpthread 选项,如 gcc -g -Wall 0_1_hello_world.c -o hello_world -lpthread)

jingyg@jingyg:~/share/mytest/linux_userspace/$ ./hello_world
pthread_self : 0xb7525b40
pthread_self : 0xb7525b40

如果线程退出了,重新创建的线程很可能复用同一个 pthread_t 类型的 ID。在设计调试日志时,用 pthread_t 类型的 ID 来标识进程就不太合适了。

采用 pid_t 类型的线程 ID 来唯一标识进程由以下优势:

  • 返回类型是 pid_t 类型,进程之间不会存在重复的线程 ID,而且不同线程之间也不会重复,在任意时刻都是全局唯一的值
  • proc 中记录了线程的相关信息,可以方便的查看 /proc/pid/task/tid 来获取线程对应的信息
  • ps 命令提供了查看线程信息的 -L 选项,可以通过输出中的 LWP 和 NLWP,来查看同一个线程组的线程个数和线程 ID 的信息

如:

#include <stdio.h>
#include <pthread.h>
#include <sys/syscall.h> void* thread_work(void* param)
{
int tid = syscall(SYS_gettid);
printf("tid : %d\n", tid);
sleep();
return NULL;
} int main()
{
pthread_t tid1 = ;
pthread_t tid2 = ;
pthread_t tid3 = ;
int ret = pthread_create(&tid1, NULL, thread_work, NULL);
ret = pthread_create(&tid2, NULL, thread_work, NULL);
ret = pthread_create(&tid3, NULL, thread_work, NULL); ret = pthread_join(tid1, NULL);
ret = pthread_join(tid2, NULL);
ret = pthread_join(tid3, NULL);
return ;
}

ps 命令查看线程 ID:

jingyg@jingyg:~$ ps -efL | grep hello_world
jingyg : pts/ :: ./hello_world
jingyg : pts/ :: ./hello_world
jingyg : pts/ :: ./hello_world
jingyg : pts/ :: ./hello_world

查看 proc:

jingyg@jingyg:~$ ll /proc//task/
total
dr-xr-xr-x jingyg jingyg Jul : ./
dr-xr-xr-x jingyg jingyg Jul : ../
dr-xr-xr-x jingyg jingyg Jul : /
dr-xr-xr-x jingyg jingyg Jul : /
dr-xr-xr-x jingyg jingyg Jul : /
dr-xr-xr-x jingyg jingyg Jul : /

线程 ID的更多相关文章

  1. linux查看某个进程的线程id(spid)

    鉴于linux下线程的广泛使用 我们怎么查看某个进程拥有的线程id了 现在很多服务的设计 主进程->子进程->线程(比如mysql,varnish) 主进程负责侦听网络上的连接 并把连接发 ...

  2. 得到某个进程所有线程ID和入口地址

    #include <windows.h> #include <tlhelp32.h> #include "iostream" using namespace ...

  3. 线程、线程句柄、线程ID

     什么是句柄:句柄是一种指向指针的指针.我们知道,所谓指针是一种内存地址.应用程序启动后,组成这个程序的各对象是住留在内存的.如果简单地理解,似乎我们只要获知这个内存的首地址,那么就可以随时用这个地址 ...

  4. Android 开发 知晓各种id信息 获取线程ID、activityID、内核ID

    /** * Returns the identifier of this process's user. * 返回此进程的用户的标识符. */ Log.e(TAG, "Process.myU ...

  5. 7.线程id,优先级讲解

    1.线程id可以通过Thread对象的getId()方法得到,在线程出了问题,为什么CPU占用这么高的时候,查的时候我们可以在堆栈信息中找到对应线程,然后干掉该线程就好! 2.而线程对象的getNam ...

  6. linux获取线程ID

    pthread_self()获取当选线程的ID.这个ID与pthread_create的第一个参数返回的相同.但是与ps命令看到的不同,因此只能用于程序内部,用于对线程进行操作. #include & ...

  7. 利用进程ID获取主线程ID

    利用进程ID获取主线程ID,仅适用于单线程.多线程应区分哪个是主线程,区分方法待验证 (1)好像可以用StartTime最早的,不过通过线程执行时间不一定可靠,要是在最开始就CreateThread了 ...

  8. 线程、线程ID获取

    一.进程ID获取 1.1  当前进程的Id 方法1 通过进程名获取 下面的例子中,也包含了获取该进程下的线程的方法. System.Diagnostics.Process[] processes:bo ...

  9. java 获取当前进程id 线程id

    java  获取当前进程id  线程id RuntimeMXBean (Java Platform SE 8 ) https://docs.oracle.com/javase/8/docs/api/j ...

随机推荐

  1. python3+selenium入门06-浏览器操作

    WebDriver主要提供元素操作的方法,但也提供了一些关于浏览器操作的方法,比如设置浏览器大小,浏览器前进,后退,刷新等 设置浏览器大小 有时候需要设置浏览器大小,比如访问收集网页,设置浏览器大小跟 ...

  2. 解决“错误 D8016 “/ZI”和“/Gy-”命令行选项不兼容 ”问题

    在Visual Studio 2017中调试VC++6.0代码,出现该问题,解决方案如下:(1)[项目]—>[属性]—>[C/C++]—> [常规]—>[调试信息格式]—> ...

  3. js垃圾回收(转

    和C#.Java一样JavaScript有自动垃圾回收机制,也就是说执行环境会负责管理代码执行过程中使用的内存,在开发过程中就无需考虑内存分配及无用内存的回收问题了.JavaScript垃圾回收的机制 ...

  4. 029_mac下nginx管理

    一. brew info nginx #查看nginx信息 nginx: stable 1.13.12 (bottled), HEAD Docroot is: /usr/local/var/www T ...

  5. 运维与自动化系列④自动化部署基础与git

    运维与自动化系列④自动化部署基础与git 自动化部署基础与git 一:上一篇的代码是保存在本地,但是在生产环境当中是由版本控制进行代码管理,以便于发布代码和回滚,一般是使用gitlib比较多,另外还有 ...

  6. OpenStack实践系列⑦深入理解neutron和虚拟机

    OpenStack实践系列⑦深入理解neutron和虚拟机 五.深入理解Neutron 5.1 虚拟机网卡和网桥 [root@node1 ~]# ifconfig brq65c11cc3-8e: fl ...

  7. PyJWT 使用

    最近要用 Falsk 开发一个大点的后端,为了安全考虑,弃用传统的Cookie验证.转用JWT. 其实 Falsk 有一个 Falsk-JWT 但是我觉得封装的太高,还是喜欢通用的 PyJWT . J ...

  8. Android 常用正则表达式

    前言 闲扯一下,已经有好久没更新博客了,记得上一篇博客的更新时间为 2017-05-12 15:20.截止到今天,超过一百天没更新了. 这篇博客的内容大多数是从别的博客摘抄过来的,写这篇博客的目的主要 ...

  9. textarea中的回车识别问题

    <textarea name="" id="aa" cols="30" rows="10" wrap=" ...

  10. System.TypeInitializationException: The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception. ---> Oracle.DataAccess.Client.OracleException: 提供程序与此版本的 Oracle 客户机不兼容”

    .net应用程序通过Oracle.DataAccess.dll访问64位的Oracle服务器,在连接时出现以下异常:“System.TypeInitializationException: The t ...