Linux 设置线程名和打印线程ID

本文介绍了如何在C语言中设置线程名,并使用`prctl`、`pthread_setname_np`等函数打印线程ID。同时展示了通过`ps`命令观察线程状态、查看进程内所有线程以及线程CPU和内存占用的方法,为理解和监控多线程程序提供了实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 设置线程名和打印线程ID
#include<stdio.h>
#include<stdlib.h>
#include <sys/prctl.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/syscall.h>

void*  thread1(void* arg)
{
	pthread_setname_np(pthread_self(),"thread1");
	while(1)
	{
		printf("thread1 %d\n", gettid());
		sleep(1000);
	}
}

pid_t gettid(void)
{
	return (pid_t)syscall(SYS_gettid);
}


void thread2(void)
{
	while(1)
	{
	   printf("thread2 %d\n",gettid());

	  sleep(1000);
	}
}
int main()
{
	pthread_setname_np(pthread_self(),"myThread");
	pthread_t th1,th2;
	void* retval;
	pthread_create(&th1,NULL,thread1,NULL);
	pthread_create(&th2,NULL,thread2,NULL);
	pthread_setname_np(th1,"myThread1");
	pthread_setname_np(th2,"myThread2");

	printf("main thread %d\n",gettid());
	sleep(1000);

	pthread_join(&th1,&retval);
	pthread_join(&th2,&retval);
}
./a.out 
thread1 5259
thread2 5260
main thread 5258
  • 通过线程名查看线程
ps  -e -T |grep myThread
PID  SPID TTY          TIME CMD
5258  5258 pts/22   00:00:00 myThread
5258  5259 pts/22   00:00:00 myThread1
5258  5260 pts/22   00:00:00 myThread2
  • 查看某进程的所有线程
ps -Lf -p 5258 
UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD
test123   5258  5067  5258  0    3 14:59 pts/22   00:00:00 ./a.out
test123   5258  5067  5259  0    3 14:59 pts/22   00:00:00 ./a.out
test123   5258  5067  5260  0    3 14:59 pts/22   00:00:00 ./a.out
  • 查看线程cpu 内存占用
ps auxww -L |grep 5258
USER       PID   LWP %CPU NLWP %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
test123   5258  5258  0.0    3  0.0  88444   712 pts/22   Sl+  14:59   0:00 ./a.out
test123   5258  5259  0.0    3  0.0  88444   712 pts/22   Sl+  14:59   0:00 ./a.out
test123   5258  5260  0.0    3  0.0  88444   712 pts/22   Sl+  14:59   0:00 ./a.out
  • 查看某进程实时占用
top -H -p 5258
top - 15:12:19 up  5:08,  5 users,  load average: 0.00, 0.00, 0.00
Threads:   3 total,   0 running,   3 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 10230620 total,   495796 free,  2944780 used,  6790044 buff/cache
KiB Swap:  4191228 total,  4189820 free,     1408 used.  6890420 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                                                                   
 5258 test123   20   0   88444    712    632 S  0.0  0.0   0:00.00 myThread                                                                                                                                  
 5259 test123   20   0   88444    712    632 S  0.0  0.0   0:00.00 myThread1                                                                                                                                 
 5260 test123   20   0   88444    712    632 S  0.0  0.0   0:00.00 myThread2      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值