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

可以使用:

  1. #include <unistd.h>
  2. pid_t getpid(void);

通过查看头文件说明,可以得到更详细的信息:

  1. find /usr/include -name unistd.h
  2. /usr/include/asm/unistd.h
  3. /usr/include/bits/unistd.h
  4. /usr/include/linux/unistd.h
  5. /usr/include/sys/unistd.h
  6. /usr/include/unistd.h
  7. cat /usr/include/unistd.h | grep getpid
  8. /* Get the process ID of the calling process.  */
  9. 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. #ifndef _LIBC
  4. /* The Linux kernel header file defines macros `__NR_<name>', but some
  5. programs expect the traditional form `SYS_<name>'.  So in building libc
  6. we scan the kernel's list and produce <bits/syscall.h> with macros for
  7. all the `SYS_' names.  */
  8. # include <bits/syscall.h>
  9. #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的方法的更多相关文章

  1. Linux下获取线程TID的方法——gettid()

    (转载)http://blog.csdn.net/delphiwcdj/article/details/8476547 如何获取进程的PID(process ID)? 可以使用: #include & ...

  2. linux下获取线程号

    #include <sys/syscall.h> pid_t gettid() { return syscall(SYS_gettid); }

  3. Linux获取线程tid线程名

    Linux获取线程tid线程名 1 2 3 4 5 6 //thread name char cThreadName[32] = {0}; prctl(PR_GET_NAME, (unsigned l ...

  4. Linux下查看线程数的几种方法汇总

    Linux下查看线程数的几种方法汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Linux下查看某个进程的线程数量 pstree命令以树状图显示进程间的关系(display ...

  5. 在Windows及Linux下获取毫秒级运行时间的方法

    在Windows下获取毫秒级运行时间的方法 头文件:<Windows.h> 函数原型: /*获取时钟频率,保存在结构LARGE_INTEGER中***/ WINBASEAPI BOOL W ...

  6. linux下获取占用CPU资源最多的10个进程

    linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下获取占用 ...

  7. linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合:

    linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下获取占用 ...

  8. Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches

    Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches 频繁的文件访问会导致系统的Cache使用量大增 $ free -m total used free shared ...

  9. Linux下Git和GitHub使用方法总结

    来源:Linux下Git和GitHub使用方法总结 1 Linux下Git和GitHub环境的搭建 第一步: 安装Git,使用命令 “sudo apt-get install git” 第二步: 到G ...

随机推荐

  1. Hadoop-01 搭建hadoop伪分布式运行环境

    Linux中配置Hadoop运行环境 程序清单 VMware Workstation 11.0.0 build-2305329 centos6.5 64bit jdk-7u80-linux-x64.r ...

  2. postman使用--环境变量

    变量 postman提供了变量设置,有四种变量类型本地变量全局变量环境变量 数据变量 什么是环境变量 环境变量指在不同环境,同一个变量值随着环境不同而变化,比如在测试环境时,host为:dev.pos ...

  3. OpenCV2:第五章 访问图像

    一.行/列访问 1.单行/单列访问 Mat Mat::row(int i) const Mat Mat::col(int j) const 2.多行/多列访问 Range(start,end); Ra ...

  4. git 添加外部项目地址

    github 提交第三方模块流程   // git config --global user.name 'your name' 可以设置全局用户名,在commit记录里显示的是这个配置设置的名称. / ...

  5. Spring框架针对dao层的jdbcTemplate操作之jdbc数据库连接原始操作方法 所需安装包下载

    crud指数据库或者持久层的基本操作,包括 增加(Create).读取查询(Retrieve 取回).更新(Update)和删除(Delete) Spring不仅对JDBC进行了封装,也对Hibern ...

  6. 解决浏览器自动填充input

    浏览器输入框自动填充解决办法 emmmmm:今天处理公司后台系统遇到的:登录页面浏览器保存账号密码后:浏览器会自动在其他页面进行填充:解决如下图: 浏览器会默认填充input type值为passwo ...

  7. hdu5126 stars

    题目描述 题解: 和二维的比起来差不多. 但是这是四维偏序. 所以搞一下CDQ套CDQ. CDQ是维度a已经有序,按维度b排序,然后将维度c存入一维数据结构. 所以我们在第一层CDQ中分治处理,将合法 ...

  8. c++_加法变乘法

    加法变乘法 我们都知道:1+2+3+ ... + 49 = 1225现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如:1+2+3+...+10*11+12+...+27*28+29+ ...

  9. Could not find conduit initiator for address:xxxxxxxxx and transport: http://schemas.xmlsoap.org/soap/http

    <properties> <cxf.version>3.1.12</cxf.version> </properties> <dependencie ...

  10. Vutrl 自己搞SS的些问题

    虽然是第二次搞这玩意但还是搞了我三天,有些东西还是想要记录一下的,以下是我犯的错误 至于如何开始搭建Vutrl上面的服务器,下面有两个链接自己搞,我就讲讲我自己碰到的问题 https://segmen ...