Linux下获取线程TID的方法——gettid()
(转载)http://blog.csdn.net/delphiwcdj/article/details/8476547
如何获取进程的PID(process ID)?
可以使用:
- #include <unistd.h>
- pid_t getpid(void);
通过查看头文件说明,可以得到更详细的信息:
- find /usr/include -name unistd.h
- /usr/include/asm/unistd.h
- /usr/include/bits/unistd.h
- /usr/include/linux/unistd.h
- /usr/include/sys/unistd.h
- /usr/include/unistd.h
- cat /usr/include/unistd.h | grep getpid
- /* Get the process ID of the calling process. */
- 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的方式是:
- #include <sys/syscall.h>
- printf("The ID of this thread is: %ld\n", (long int)syscall(224));
或者比较elegant的方式是:
- #include <sys/syscall.h>
- #define gettidv1() syscall(__NR_gettid)
- #define gettidv2() syscall(SYS_gettid)
- printf("The ID of this thread is: %ld\n", (long int)gettidv1());// 最新的方式
- 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>。
- // /usr/include/bits/syscall.h
- #define SYS_gettid __NR_gettid
- #ifndef _LIBC
- /* The Linux kernel header file defines macros `__NR_<name>', but some
- programs expect the traditional form `SYS_<name>'. So in building libc
- we scan the kernel's list and produce <bits/syscall.h> with macros for
- all the `SYS_' names. */
- # include <bits/syscall.h>
- #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()的更多相关文章
- Linux下获取线程TID的方法
如何获取进程的PID(process ID)? 可以使用: #include <unistd.h> pid_t getpid(void); 通过查看头文件说明,可以得到更详细的信息: fi ...
- linux下获取线程号
#include <sys/syscall.h> pid_t gettid() { return syscall(SYS_gettid); }
- Linux获取线程tid线程名
Linux获取线程tid线程名 1 2 3 4 5 6 //thread name char cThreadName[32] = {0}; prctl(PR_GET_NAME, (unsigned l ...
- Linux下查看线程数的几种方法汇总
Linux下查看线程数的几种方法汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Linux下查看某个进程的线程数量 pstree命令以树状图显示进程间的关系(display ...
- 在Windows及Linux下获取毫秒级运行时间的方法
在Windows下获取毫秒级运行时间的方法 头文件:<Windows.h> 函数原型: /*获取时钟频率,保存在结构LARGE_INTEGER中***/ WINBASEAPI BOOL W ...
- linux下获取占用CPU资源最多的10个进程
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下获取占用 ...
- linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合:
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下获取占用 ...
- Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches
Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches 频繁的文件访问会导致系统的Cache使用量大增 $ free -m total used free shared ...
- Linux下Git和GitHub使用方法总结
来源:Linux下Git和GitHub使用方法总结 1 Linux下Git和GitHub环境的搭建 第一步: 安装Git,使用命令 “sudo apt-get install git” 第二步: 到G ...
随机推荐
- cocos2d-x ios 设置横屏/竖屏(全)
Cocos2d-x项目\iOS\RootViewController.mm文件中. 以下方法任选其一即可… 本人机子函数二ok! 函数一: (BOOL)shouldAutorotateToI ...
- 使用vue-cli脚手架安装的eslint 容易犯错的地方
1. 函数名字与括号之间要有空格. 2. 不要使用双引号 3. 不要有多月的空行 4.函数参数的逗号后要有空格 5.每个结束语句以后不用加“分号”
- Spark Accumulators
概述 Accumulator即累加器,与Mapreduce counter的应用场景差不多,都能很好地观察task在运行期间的数据变化,Spark中的Accumulator各task可以对Accumu ...
- uva 11081 - Strings(LCS)
题目链接:11081 - Strings 题目大意:给出三个字符串,从分别从第一个字符串和第二个字符串中挑选子串a,b,用a和b组成第三个字符串,问可组成的子串有多少种. 解题思路:说起来惭愧啊,题目 ...
- Windows下文件列举,搜索
Windows下列举文件用的函数是 FindFirstFile 和 FindNextFile ,另外一个结构体是WIN32_FIND_DATA 以下是MSDN对于WIN32_FIND_DATA的定义 ...
- hdu4756 Install Air Conditioning(MST + 树形DP)
题目请戳这里 题目大意:给n个点,现在要使这n个点连通,并且要求代价最小.现在有2个点之间不能直接连通(除了第一个点),求最小代价. 题目分析:跟这题一样样的,唉,又是原题..先求mst,然后枚举边, ...
- Codeforces 468B Two Sets 并查集
题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...
- C++11 多线程 教学(2)
C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等, ...
- 空值排序(oracle和sqlserver)
oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 改变空值办法: (1)用nvl函数或decode函数将null转换为一特定值 替 ...
- hdu1054 树状dp
B - 树形dp Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:10000KB 64bit ...