- 获取进程的id
#include <unistd.h> pid_t getpid(void);
- 获取线程的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();
// /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