一、前言
有时候我们通过fork()、pthread_create()创建出来的新任务其名字和创建任务的parent是一样的,如果有需要,该如何修改这些新创建任务的名字呢?
有办法,用proctl()函数来实现。这个函数可用来对任务进行控制,其具体情况如下所示:
复制代码
其中第一个参数“option”用来指明prctl()对线程(也可以称为任务)实施的具体的控制行为,其可选的值在<linux/protl.h>文件中定义;而"option"后面参数的值意义需要根据"option"来定。
1
2
3#include <sys/prctl.h> int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);
对于"option"的选项有很多,我们这里只关注的是"PR_SET_NAME"这个选项,其他的选项这里就不一一给出了,感兴趣的小哥哥小姐姐们可以参考: prctl(2)。
当入参"option"值为"PR_SET_NAME"时,prctl()会为 调用此函数的 线程设置新名字,新名字设置为参数(char *)arg2。其中,线程名字的参数arg2字符串的最大长度为16 bytes(包括字符串终止符)。如果arg2长度超过16 bytes,它将会被截断为16 bytes。
除了使用“PR_SET_NAME”参数可以设置线程名字外,另外一个函数pthread_setname_np()也可以实现此功能。设置完成后我们可以通过线程的tid来查看它的新名字:
复制代码
上面的"pid"表示这个线程所属线程组leader的"pid","tid"则是调用prctl()函数的线程id。
1cat /proc/pid/task/[tid]/comm
二、代码
了解了proctl()的使用后我们看看具体的例子:
复制代码
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#include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <pthread.h> #include <stdarg.h> /* asked by va_star and va_end */ #include <sys/prctl.h> /* asked by PR_SET_NAME */ void die(const char *fmt, ...); int * thread1(void *arg); int * thread2(void *arg); int * thread3(void *arg); int thread_create(void); void process_create(int num_proc); int main(int argc,char *argv[]){ printf("will create threadsn"); thread_create(); while(1){sleep(1);} } void die(const char *fmt, ...) { va_list args; va_start(args, fmt); vfprintf(stderr, fmt, args); va_end(args); fflush(stdout); fflush(stderr); exit(1); } void process_create(int num_proc) { pid_t pid; int i = 0; char childname[][10] = { "process-1", "process-2", "process-3", "process-4", "process-5", "process-6", }; if (num_proc < 1) die("num of proc must > 1n"); while(i < num_proc) { if ((pid = fork()) < 0) die("fork failedn"); if (0 == pid) { prctl(PR_SET_NAME, childname[i], NULL, NULL, NULL); while(1); exit(0); } i++; } } int * thread1(void *arg) { prctl(PR_SET_NAME, "THREAD1"); printf("thread id is %lu.n", (unsigned long)pthread_self()); while(1){sleep(5);}; return NULL; } int * thread2(void *arg) { prctl(PR_SET_NAME,"THREAD2"); printf("thread id is %lu.n", (unsigned long)pthread_self()); while(1){sleep(5);}; return NULL; } int * thread3(void *arg) { prctl(PR_SET_NAME, "THREAD3"); char command[128]= {0}; process_create(5); /* create 5 processes */ printf("thread id is %lu.n",(unsigned long)pthread_self()); while(1){sleep(5);}; return NULL; } int thread_create(void) { pthread_t id1, id2, id3; printf("Main thread id is %lu n", (unsigned long)pthread_self()); if(!pthread_create(&id1, NULL, (void *)thread1, NULL)) { printf("thread 1 succed!n"); } if(!pthread_create(&id2, NULL, (void *)thread2, NULL)) { printf("thread 2 succed!n"); } if(!pthread_create(&id3, NULL, (void *)thread3, NULL)) { printf("thread 3 succed!n"); } }
三、代码分析
上面的例子中创建来三个线程,并通过prctl(PR_SET_NAME,....)分别将名字设置为THREAD1/THREAD2/THREAD3;接着THREAD3又创建来5个子进程,同样,通过prctl(PR_SET_NAME,....)函数这些子进程将自己的名字分别设置为"process-1" ~ "process-5"。
可以通过如下命令进行编译:
复制代码
1gcc -pthread -o prctl_name prctl_name.c
编译完成后生成可执行文件prctl_name ,然后运行 ./prctl_name &, 并在终端中通过 "ps -L"来查看结果。
最后
以上就是秀丽白昼最近收集整理的关于linu中使用prctl函数为线程指定名字一、前言二、代码的全部内容,更多相关linu中使用prctl函数为线程指定名字一、前言二、代码内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复