Linux C/C++编程:prctl与pthread_setname_np

prctl

理论

// 用 prctl 给线程命名, prctl是个系统调用
#include <sys/prctl.h> 
int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);

实践

#include <zconf.h>
#include <sys/prctl.h>
#include <stdio.h>
#include <pthread.h>

void* tmain(void *arg)
{
    char name[32];
    prctl(PR_SET_NAME, (unsigned long)"xx");
    prctl(PR_GET_NAME, (unsigned long)name);
    printf("%s/n", name);
    while (1)
        sleep(1);
}
int main(void)
{
    pthread_t tid;
    pthread_create(&tid, NULL, tmain, NULL);
    pthread_join(tid, NULL);
    return 0;
}

执行:

$ gcc main.c -l pthread
$ ps aux | grep a.out
oceanst+   9725  0.0  0.0  14704   384 pts/1    Sl+  18:52   0:00 ./a.out
$  ps -L -p 9725
   PID    LWP TTY          TIME CMD
  9725   9725 pts/1    00:00:00 a.out
  9725   9726 pts/1    00:00:00 xx

pthread_setname_np

prctl()只能设置/获取当前线程的名字
在glibc 2.12之后的版本中提供了两个扩展的接口pthread_setname_np()和pthread_getname_np(),可以在进程中设置和读取其他线程的名字。

检查glibc版本:

# getconf GNU_LIBC_VERSION
glibc 2.17

可以使用feature_test_macro_GNU_SOURCE来检查此功能是否可用:

#ifdef _GNU_SOURCE
    pthread_setname_np(tid, "someName");
#endif

理论

#include <pthread.h>
/*
* 功能值:该pthread_setname_np()函数设置目标线程的名称。由名称指定的缓冲区必须包含一个长度为15个字符或更少的空终止字符串(不包括NULL)。如果名称长度超过15个字符,则多余的字符将被忽略。如果name为null,则该线程变为未命名
* 返回值:成功返回0
*/
 int pthread_setname_np(pthread_t thread, const char *name);
//获取线程名称
int pthread_getname_np(pthread_t thread, char *name);

实践

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

void *threadfunc(void *parm)
{
    printf("In the thread.\n");
    sleep(45);  // allow main program to set the thread name
    return NULL;
}

int main(int argc, char **argv)
{
    pthread_t             thread;
    int                   rc=0;
    char                  theName[16];

    memset(theName, 0x00, sizeof(theName));
    printf("Create thread using the default attributes\n");
    rc = pthread_create(&thread, NULL, threadfunc, NULL);
    if(0 == rc)
    {
#ifdef _GNU_SOURCE
        rc = pthread_setname_np(thread, "THREADNAMEISTOOLONG");
        sleep(10);

        if(0 == rc)
        {
            rc = pthread_getname_np(thread, theName);
            if(0 == rc)
            {
                printf("The thread name is %s.\n", theName);
            }
        }
#endif
        rc = pthread_join(thread, NULL);
    }
    if(0 != rc)
    {
        printf("An error occurred - %d\n", rc);
    }
    printf("Main completed\n");
    return(rc);
}

用prctl给线程命名

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 环境下,子进程可以使用 `prctl` 函数来检测父进程是否退出。具体实现步骤如下: 1. 父进程在启动子进程之前,调用 `prctl` 函数设置 `PR_SET_PDEATHSIG` 属性,并指定一个信号(如 `SIGTERM`)。 2. 子进程在启动后,调用 `getppid` 函数获取自己的父进程 ID。 3. 子进程通过 `kill` 函数向父进程发送一个 0 信号,检测父进程是否存在。如果父进程已经退出,子进程会收到一个 `SIGTERM` 信号。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/prctl.h> int main() { pid_t parent_pid = getpid(); // 设置 PR_SET_PDEATHSIG 属性 if (prctl(PR_SET_PDEATHSIG, SIGTERM) == -1) { perror("prctl"); exit(EXIT_FAILURE); } pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } else if (pid == 0) { // 子进程 while (1) { // 检测父进程是否存在 if (kill(parent_pid, 0) == -1) { if (errno == ESRCH) { // 父进程已经退出 printf("Parent process has exited.\n"); exit(EXIT_SUCCESS); } else { perror("kill"); exit(EXIT_FAILURE); } } sleep(1); } } else { // 父进程 atexit(parent_exit_handler); sleep(2); printf("Parent process is exiting.\n"); exit(EXIT_SUCCESS); } } void parent_exit_handler() { // 父进程退出时不需要做任何处理 } ``` 在上面的代码中,父进程在启动子进程之前,调用了 `prctl` 函数设置 `PR_SET_PDEATHSIG` 属性,并指定一个信号(如 `SIGTERM`)。子进程在启动后,通过 `getppid` 函数获取自己的父进程 ID,并通过 `kill` 函数向父进程发送一个 0 信号,检测父进程是否存在。如果父进程已经退出,子进程会收到一个 `SIGTERM` 信号。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值