[Linux c] 关于进程名,线程名,/proc文件系统 等

参考:

kernel - Why is the name of a process in /proc/PID/status not matching package name or ps command - Stack Overflowicon-default.png?t=N7T8https://stackoverflow.com/questions/14176058/why-is-the-name-of-a-process-in-proc-pid-status-not-matching-package-name-or-ps

设置和查看线程名:

#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

void* thread1(void* a)
{
  prctl(PR_SET_NAME,"THREAD1");
  while(1) sleep(1000);
}

void* thread2(void* a)
{
  prctl(PR_SET_NAME,"THREAD2");
  while(1) sleep(1000);
}

int main()
{

  pthread_t th1,th2;
  pthread_create(&th1,NULL,thread1,NULL);
  pthread_create(&th2,NULL,thread2,NULL);


  char name[1024]={0};


  prctl(PR_GET_NAME, (unsigned long)name);
  printf("prctl api %s\n", name);
  memset(name,0,sizeof(name));
  pthread_getname_np(pthread_self(), name, 1024);
  printf("pthread api %s\n", name);

  int ret = prctl(PR_SET_NAME,"THREADMAIN");
  printf("-------ret=%d\n",ret);

  memset(name,0,sizeof(name));
  prctl(PR_GET_NAME, (unsigned long)name);
  printf("prctl api %s\n", name);
  memset(name,0,sizeof(name));
  pthread_getname_np(pthread_self(), name, 1024);
  printf("pthread api %s\n", name);


//
  printf("/\n");

  memset(name,0,sizeof(name));
  prctl(PR_GET_NAME, (unsigned long)name);
  printf("prctl api %s\n", name);
  memset(name,0,sizeof(name));
  pthread_getname_np(pthread_self(), name, 1024);
  printf("pthread api  %s\n", name);

  int rc = pthread_setname_np(pthread_self(), "xxxx");
  printf("-------rc=%d\n",rc);

  memset(name,0,sizeof(name));
  prctl(PR_GET_NAME, (unsigned long)name);
  printf("prctl api %s\n", name);
  memset(name,0,sizeof(name));
  pthread_getname_np(pthread_self(), name, 1024);
  printf("pthread api  %s\n", name);




  while(1) sleep(1000);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值