Linux下获取线程TID的方法——gettid()
2014-01-14 18:20
399 查看
如何获取进程的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
from : http://blog.csdn.net/delphiwcdj/article/details/8476547
可以使用:
#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
from : http://blog.csdn.net/delphiwcdj/article/details/8476547
相关文章推荐
- linux下获取系统时间
- rhel 6.3 建立centos yum
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- Linux统计某文件夹下文件、文件夹的个数
- linux 内存管理浅析(二)
- 【原创】Linux下的ngix服务器安装步骤
- linux内存管理浅析(一)
- Linux下如何删除乱码文件
- Linux下的文件比较工具
- 收集linux几个快捷键
- linux驱动开发之自动创建设备节点
- linux 新建用户、用户组 以及为新用户分配权限
- linux sftp and unix ftp
- Linux驱动4_NETFILTER
- linux 定时删除过期文件夹
- Linux恢复数据
- Linux中命令链接操作符的十个最佳实例
- linux 查看硬件信息
- Linux vmstat 命令 - 报告虚拟内存统计的工具
- Linux 下如何查看一个组内的有哪些用户