Liunx获取线程Id

  1. 获取进程的id
    #include <unistd.h>
    pid_t getpid(void);
  2. 获取线程的id
需要系统调用值:
#define  __NR_gettid  224
获取某个线程的id的方法:
	cout <<  "this tread is:  " << (long  int)syscall(224);

比较好的方式是
#define  __NR_gettid  224
#define gettid1()  syscall( __NR_gettid)
#define gettid2()  syscall( SYS_gettid)

cout <<  "this tread is:  " << gettid1();
cout <<  "this tread is:  " << gettid2();

其实最后使用的都是__NR_gettid,因为:
// /usr/include/bits/syscall.h  
#define SYS_gettid __NR_gettid  

#ifndef _LIBC  
/* The Linux kernel header file defines macros `__NR_<name>', but some      
   programs expect the traditional form `SYS_<name>'.  So in building libc 
   we scan the kernel's list and produce <bits/syscall.h> with macros for 
   all the `SYS_' names.  */  
# include <bits/syscall.h>  
#endif


查看进程pid
(1) ps ux | grep prog_name
(2) pgrep prog_name 
查看线程tid
(1) ps -efL | grep prog_name
(2) ls /proc/pid/task


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值