prctl()为线程指定名字

Llinu中使用prctl函数为线程指定名字


一、前言
    有时候我们通过fork()、pthread_create()创建出来的新任务其名字和创建任务的parent是一样的,如果有需要,该如何修改这些新创建任务的名字呢?

    有办法,用proctl()函数来实现。这个函数可用来对任务进行控制,其具体情况如下所示:
#include <sys/prctl.h>
 
int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);
    其中第一个参数“option”用来指明prctl()对线程(也可以称为任务)实施的具体的控制行为,其可选的值在<linux/protl.h>文件中定义;而"option"后面参数的值意义需要根据"option"来定。
    对于"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来查看它的新名字:
cat /proc/pid/task/[tid]/comm
    上面的"pid"表示这个线程所属线程组leader的"pid","tid"则是调用prctl()函数的线程id。
二、代码
    了解了proctl()的使用后我们看看具体的例子:

#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 threads\n");
    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 > 1\n");
    while(i < num_proc) {
        if ((pid = fork()) < 0) 
            die("fork failed\n");
        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"。

    可以通过如下命令进行编译:

gcc -pthread -o prctl_name prctl_name.c
    编译完成后生成可执行文件prctl_name ,然后运行 ./prctl_name &,   并在终端中通过 "ps -L"来查看结果。
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值