linux cadaver 命令,对于linux中线程id的讨论
在LINUX系统中,POSIX threads库提供了pthread_t来标识一个线程,通过pthread_self()可以得到,如下:
#include
#include
using namespace std;
void* thread_func(void*)
{
//pthread_t other_thread_id = pthread_self();
//cout << "other_thread_id=" << other_thread_id << endl;
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_func, NULL);
cout << t1 << endl;
pthread_join(t1, NULL);
//pthread_create(&t2, NULL, thread_func, NULL);
//cout << t2 << endl;
//pthread_join(t2, NULL);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
得到结果:
typedef unsigned long int pthread_t;
这个数值很大。而且比较两个线程是否相同也需要用pthread_equal(pthread_t t1, pthread_t t2)来比较。
那么,使用pthread_t来标识线程id是否是最合适的?
答案是否
原因:这个pthread_t的值很大,无法作为一些容器的key值。
pthread_t是由POSIX pthread库内部提供的,只在进程内部有意义,无法关联操作系统的任务调度之类的信息。比方说在/proc查找不到关于pthread_t得到的task。
glibc的Pthreads实现实际上把pthread_t作为一个结构体指针,指向一块动态分配的内存,但是这块内存是可以反复使用的,也就是说很容易造成pthread_t的重复。也就是说pthreads只能保证同一进程内,同一时刻的各个线程不同;不能保证同一个进程全程时段每个线程具有不同的id,不能保证线程id的唯一性。下面可以通过一个例子:
#include
#include
using namespace std;
void* thread_func(void*)
{
//pthread_t other_thread_id = pthread_self();
//cout << "other_thread_id=" << other_thread_id << endl;
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_func, NULL);
cout << t1 << endl;
pthread_join(t1, NULL);
pthread_create(&t2, NULL, thread_func, NULL);
cout << t2 << endl;
pthread_join(t2, NULL);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
运行结果:
上述原因显然表明pthread_t是不适合作为线程的标识的。所以应该使用什么来标识呢。
首先对于进程id,有函数getpid返回的pid_t来标识。那么线程有没有类似的gettid来标识呢。
在LINUX系统中,建议使用gettid系统调用的返回值作为线程id,这么做的原因:
返回值是一个pid_t,其值是一个很小的整数,方便输出。
在linux系统中,它直接标识内核任务调度id,可通过/proc文件系统中找到对应项:/proc/tid 或者 /proc/pid/task/tid,方便定位到具体线程
任何时刻都是唯一的,并且由于linux分配新的pid采用递增轮回办法,短时间内启动多个线程也会具有不同的id
0是非法值,操作系统第一个进程init的pid是1
glibc没有封装这个gettid,需要我们手动封装。
举例:
#include
#include
#include //获取线程id的系统调用头文件
#include
#include
using namespace std;
void* thread_func(void*)
{
//获取线程id的系统调用
cout << syscall(__NR_gettid) << endl;
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_func, NULL);
cout << t1 << endl;
pthread_join(t1, NULL);
pthread_create(&t2, NULL, thread_func, NULL);
cout << t2 << endl;
pthread_join(t2, NULL);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
结果:
和pthread_t的描述完全不一样。
使用比较优雅的方式是定义一个gettid的宏
#include //获取线程id的系统调用头文件
#include
#include
//定义宏
#define gettid() syscall(__NR_gettid)
1
2
3
4
5
6
验证TID是否正确的方法:
查看进程pid
(1) ps ux | grep prog_name
(2) pgrep prog_name
查看线程tid
(1) ps -efL | grep prog_name
(2) ls /proc/pid/task
————————————————
版权声明:本文为CSDN博主「ZgZeQi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zeqi1991/article/details/81611919
linux cadaver 命令,对于linux中线程id的讨论相关推荐
- Linux线程ID是32位的,对于linux中线程id的讨论
在LINUX系统中,POSIX threads库提供了pthread_t来标识一个线程,通过pthread_self()可以得到,如下: #include #include using namespa ...
- linux cadaver 命令,备份Linux操作系统的数据到坚果云的方法
坚果云支持WebDAV,这样就能在Linux操作系统中使用命令行或者脚本的方式把数据备份到坚果云空间中,本文提供实现的方法. 首先开启坚果云WebDAV服务 "账户信息"--&qu ...
- linux cadaver 命令,备份Linux系统的数据到坚果云
坚果云支持webdav,则可以在Linux下使用命令行或脚本将数据备份到坚果云空间. 开启坚果云WebDAV服务 "账户信息"--"安全选项" 方式1:使用ca ...
- linux cadaver 命令,【Linux学习第三篇】[Tab].[Ctrl]-c.[Ctrl]-d
[root@localhost ~]# ls -al ~/.bash [Tab][Tab] //显示所有以.bash开头的文件名 .bash_history .bash_log ...
- linux ps 命令的结果中VSZ,RSS,STAT的含义和大小
linux ps 命令的结果中VSZ,RSS,STAT的含义和大小 ps是linux系统的进程管理工具,相当于windows中的资源管理器的一部分功能. 一般来说,ps aux命令执行结果的几个列的信 ...
- linux oracle流复制文件,【学习笔记】Oracle ASM linux dd命令复制asm中文件 操作磁盘或者分区...
天萃荷净 使用dd复制asm中文件,随着数据库新版本的推广ASM肯定会越来越被重视,最近准备系统的学习下ASM,以备突发情况需要,这是asm深入学习笔记 1.查询ASM某个数据文件AU信息 SQL&g ...
- linux+echo+权限不够,解决linux echo命令以及linux echo命令提示权限不够的方法
linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法.下面通过本文给大家介绍linux echo命令以及linux ...
- linux下执行php命令echo不输出,linux echo命令以及linux echo命令提示权限不够的方法...
linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法.下面通过本文给大家介绍linux echo命令以及linux ...
- linux mv命令的功能,linux常用命令:Linux常用命令之mv命令是什么?
Linux是目前使用比较广泛的服务器操作系统,可以安装在各种计算机硬件设备中,例如手机,路由器等.由于Linux是开源和免费的,因此Linux系统的稳定性和安全性更高一些,所以Linux具有很高的市场 ...
最新文章
- MATLAB知识点总结
- 修改 Oracle 数据库 sequence(序列) 的 increment (步长)
- href 里面 链接前面加/与不加的区别?(绝对路径与相对路径)
- association in CDS view is converted to LEFT OUTER MANY TO ONE JOIN in the runtime
- 防数据泄密:是否应实施“多重认证”?
- java php cms_内容管理系统的开发策略研究——以PHP CMS、Node.js CMS、Java CMS为例
- redis源码剖析(2):基础数据结构ADLIST
- 从程序员小仙飞升上神,java技术开发要如何实现?
- 简单的实现HTTP密码验证登陆
- 在非controllers中获取httpServletRequest
- SpringMVC自定义拦截器拦截请求后返回中文时乱码
- java解指派问题,运用Excel规划求解解决指派问题
- 从“果链”到新能源,立讯精密的B端生意成色几何?
- JavaWeb——JQuery之高级案例实战(打开网页自动弹出广告效果、抽奖效果实现)
- 斐那契波黄金数列MATLAB,广义斐波那契数列的性质及推广
- html制作横向菜单,CSS 横向菜单的制作
- appium driver参数及命令行参数
- python金融大数据分析师工资待遇_请问数据分析师这个工作怎么样,是否值得成为努力方向?...
- office卸载重新安装,并安装mathtype7数学编辑公式
- vue实现自定义步骤条