Linux下获取线程TID的方法——gettid() 获取进程 getpid()

2024-09-07 17:08

本文主要是介绍Linux下获取线程TID的方法——gettid() 获取进程 getpid(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


如何获取进程的PID(process ID)?

可以使用:

  1. #include <unistd.h>  
  2. pid_t getpid(void);  
通过查看头文件说明,可以得到更详细的信息:
  1. find /usr/include -name unistd.h  
  2.   
  3. /usr/include/asm/unistd.h  
  4. /usr/include/bits/unistd.h  
  5. /usr/include/linux/unistd.h  
  6. /usr/include/sys/unistd.h  
  7. /usr/include/unistd.h  
  8.   
  9. cat /usr/include/unistd.h | grep getpid  
  10.   
  11. /* Get the process ID of the calling process.  */  
  12. extern __pid_t getpid (void) __THROW;  

如何获取线程的TID(thread ID)?

通过查看man得到如下描述:
(1) The gettid() system call first appeared on Linux in kernel 2.4.11.
(2) gettid() returns the thread ID of the current process. This is equal to the process ID (as returned by getpid(2)), unless the process is part of a thread group (created by specifying the CLONE_THREAD flag to the clone(2) system call). All processes in the same thread group have the same PID, but each one has a unique TID.
(3) gettid() is Linux specific and should not be used in programs that are intended to be portable. (如果考虑移植性,不应该使用此接口)

但是根据man的使用说明,测试后发现会报找不到此接口的错误“error: undefined reference to `gettid'”,通过下面链接可以找到更详细的说明:
http://www.kernel.org/doc/man-pages/online/pages/man2/gettid.2.html
(1) Glibc does not provide a wrapper for this system call; call it using syscall(2).(说明Glibc并没有提供此接口的声明,此接口实际使用的是系统调用,使用者可以自己创建包裹函数)
(2) The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e., the opaque value returned by pthread_self(3)).

然后查看/usr/include/sys/syscall.h(实际在/usr/include/asm/unistd.h)可以找到我们需要的system call number:
#define __NR_gettid     224

因此,要获取某个线程的TID,最nasty的方式是:

  1. #include <sys/syscall.h>  
  2. printf("The ID of this thread is: %ld\n", (long int)syscall(224));  
或者比较elegant的方式是:
  1. #include <sys/syscall.h>  
  2. #define gettidv1() syscall(__NR_gettid)  
  3. #define gettidv2() syscall(SYS_gettid)  
  4. printf("The ID of this thread is: %ld\n", (long int)gettidv1());// 最新的方式  
  5. printf("The ID of this thread is: %ld\n", (long int)gettidv2());// traditional form  
PS: 在/usr/include/sys/syscall.h中可以看到关于__NR_<name>和SYS_<name>两个宏的区别,实际最后使用的都是__NR_<name>。
  1. // /usr/include/bits/syscall.h  
  2. #define SYS_gettid __NR_gettid  
  3.   
  4. #ifndef _LIBC  
  5. /* The Linux kernel header file defines macros `__NR_<name>', but some      
  6.    programs expect the traditional form `SYS_<name>'.  So in building libc 
  7.    we scan the kernel's list and produce <bits/syscall.h> with macros for 
  8.    all the `SYS_' names.  */  
  9. # include <bits/syscall.h>  
  10. #endif  


验证TID是否正确的方法:
查看进程pid
(1) ps ux | grep prog_name
(2) pgrep prog_name 
查看线程tid
(1) ps -efL | grep prog_name
(2) ls /proc/pid/task

这篇关于Linux下获取线程TID的方法——gettid() 获取进程 getpid()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

jdk21下载、安装详细教程(Windows、Linux、macOS)

《jdk21下载、安装详细教程(Windows、Linux、macOS)》本文介绍了OpenJDK21的下载地址和安装步骤,包括Windows、Linux和macOS平台,下载后解压并设置环境变量,最... 目录1、官网2、下载openjdk3、安装4、验证1、官网官网地址:OpenJDK下载地址:Ar

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

linux本机进程间通信之UDS详解

《linux本机进程间通信之UDS详解》文章介绍了Unix域套接字(UDS)的使用方法,这是一种在同一台主机上不同进程间通信的方式,UDS支持三种套接字类型:SOCK_STREAM、SOCK_DGRA... 目录基础概念本机进程间通信socket实现AF_INET数据收发示意图AF_Unix数据收发流程图A

Springboot控制反转与Bean对象的方法

《Springboot控制反转与Bean对象的方法》文章介绍了SpringBoot中的控制反转(IoC)概念,描述了IoC容器如何管理Bean的生命周期和依赖关系,它详细讲解了Bean的注册过程,包括... 目录1 控制反转1.1 什么是控制反转1.2 SpringBoot中的控制反转2 Ioc容器对Bea

linux环境openssl、openssh升级流程

《linux环境openssl、openssh升级流程》该文章详细介绍了在Ubuntu22.04系统上升级OpenSSL和OpenSSH的方法,首先,升级OpenSSL的步骤包括下载最新版本、安装编译... 目录一.升级openssl1.官网下载最新版openssl2.安装编译环境3.下载后解压安装4.备份

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

golang获取prometheus数据(prometheus/client_golang包)

《golang获取prometheus数据(prometheus/client_golang包)》本文主要介绍了使用Go语言的prometheus/client_golang包来获取Prometheu... 目录1. 创建链接1.1 语法1.2 完整示例2. 简单查询2.1 语法2.2 完整示例3. 范围值