我是靠谱客的博主 爱笑小鸭子,这篇文章主要介绍prctl函数设置线程别名prctl函数用法,现在分享给大家,希望可以做个参考。

prctl函数用法

复制代码
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
29
30
31
32
33
34
35
NAME prctl - operations on a process SYNOPSIS #include <sys/prctl.h> int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5); DESCRIPTION prctl() is called with a first argument describing what to do (with values defined in <linux/prctl.h>), and further arguments with a significance depending on the first one. The first argument can be: PR_SET_NAME (since Linux 2.6.9) Set the name of the calling thread, using the value in the location pointed to by (char *) arg2. The name can be up to 16 bytes long, and should be null-terminated if it contains fewer bytes. This is the same attribute that can be set via pthread_setname_np(3) and retrieved using pthread_getname_np(3). The attribute is likewise accessible via /proc/self/task/[tid]/comm, where tid is the name of the calling thread. PR_GET_NAME (since Linux 2.6.11) Return the name of the calling thread, in the buffer pointed to by (char *) arg2. The buffer should allow space for up to 16 bytes; the returned string will be null-terminated if it is shorter than that.

示例代码

复制代码
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
29
30
31
32
33
#include <iostream> #include <unistd.h> #include <thread> #include <vector> #include <sys/prctl.h> // PR_SET_NAME inline void SetThreadName(const std::string& name) { ::prctl(PR_SET_NAME, name.c_str()); } volatile bool g_is_running = false; int main() { std::vector<std::thread*> thread_manager; thread_manager.reserve(20); g_is_running = true; for (int i = 0; i < 10; i++) { thread_manager.emplace_back(new std::thread([&](){ SetThreadName("t-" + std::to_string(i)); while (g_is_running) { usleep(1); } })); } for (auto& thr : thread_manager) { thr->join(); } return 0; }

查看cpu使用情况

复制代码
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
➜ ~ top -H -p 10287 top - 23:35:22 up 235 days, 12:17, 2 users, load average: 0.22, 0.11, 0.07 Threads: 11 total, 2 running, 9 sleeping, 0 stopped, 0 zombie %Cpu(s): 4.3 us, 10.5 sy, 0.0 ni, 85.2 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 8008616 total, 175312 free, 625272 used, 7208032 buff/cache KiB Swap: 0 total, 0 free, 0 used. 7068056 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 10293 root 20 0 97904 1416 1212 S 17.6 0.0 0:10.94 t-6 10289 root 20 0 97904 1416 1212 S 17.3 0.0 0:10.95 t-2 10290 root 20 0 97904 1416 1212 S 17.3 0.0 0:10.91 t-4 10291 root 20 0 97904 1416 1212 S 17.3 0.0 0:10.93 t-4 10292 root 20 0 97904 1416 1212 R 17.3 0.0 0:10.94 t-5 10294 root 20 0 97904 1416 1212 S 17.3 0.0 0:10.92 t-7 10295 root 20 0 97904 1416 1212 R 17.3 0.0 0:10.92 t-8 10296 root 20 0 97904 1416 1212 S 17.3 0.0 0:10.92 t-9 10297 root 20 0 97904 1416 1212 S 17.3 0.0 0:10.89 t-10 10288 root 20 0 97904 1416 1212 S 16.9 0.0 0:10.92 t-1 10287 root 20 0 97904 1416 1212 S 0.0 0.0 0:00.00 app

最后

以上就是爱笑小鸭子最近收集整理的关于prctl函数设置线程别名prctl函数用法的全部内容,更多相关prctl函数设置线程别名prctl函数用法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部