Liunx获取线程Id

来源:互联网 发布:千县万村农村淘宝计划 编辑:程序博客网 时间:2024/05/16 05:45
  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


原创粉丝点击