linux 线程id 与进程id对应关系
2015-08-09 11:02
721 查看
linux 中的线程是基于进程实现的,每个线程都会有一个进程对应,通过gettid()可以获取到该进程id。
另外,通过pthread_self()获取到的是POSIX thread id。
下面简单举个例子。
测试结果
$ gcc test.c -o test -lpthread
$ ./test
——thread main begin pid=8010, tid=8010, thread id=3075487488—–
——thread 1 begin pid=8010, tid=8011, thread id=3075484480—–
——thread 2 begin pid=8010, tid=8012, thread id=3075349312—–
========do a lot of work====
========do a lot of work====
^C
$ ps -aux | grep test
lanyang 8010 0.0 0.0 2536 316 pts/1 Sl+ 10:30 0:00 ./test
$ ls /proc/8010/task/
8010 8011 8012
从结果可以看到主线程创建了两个线程
3075484480、3075349312对应的进程ID分别为8011、8012.
由于gettid()没有在标准C库中,所以使用时要使用系统调用号去调用。系统调用号可以通过查看文件得到:
/usr/include/asm/unistd.h
另外,通过pthread_self()获取到的是POSIX thread id。
下面简单举个例子。
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <sys/types.h> #include <unistd.h> #include <sys/syscall.h> #define THREAD_STACK_SIZE 128*1024 static void error_handler(const char *arg) { printf(arg); exit(EXIT_FAILURE); } static void *thread_1(void *arg) { pid_t pid = getpid(); pthread_t pth_id = pthread_self(); //pid_t tid = gettid(); pid_t tid = syscall(SYS_gettid); printf("------thread 1 begin pid=%d, tid=%d, thread id=%lu-----\n", pid, tid, pth_id); printf("========do a lot of work====\n"); while(1) { pause(); } printf("---->thread 1 EXIT\n"); return; } static void *thread_2(void *arg) { pid_t pid = getpid(); pthread_t pth_id = pthread_self(); //pid_t tid = gettid(); pid_t tid = syscall(SYS_gettid); printf("------thread 2 begin pid=%d, tid=%d, thread id=%lu-----\n", pid, tid, pth_id); printf("========do a lot of work====\n"); while(1) { pause(); } printf("---->thread 2 EXIT\n"); return; } void test_thread_init(void* thread_func()) { pthread_t pthread_id; pthread_attr_t attr; if (pthread_attr_init(&attr) < 0) { error_handler("test_thread_init init thread attribute failed\n"); } if (pthread_attr_setstacksize (&attr, THREAD_STACK_SIZE) !=0) { error_handler("test_thread_init set stack size failed\n"); } if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) < 0) { error_handler("test_thread_init set setdetachstate failed\n"); } if (pthread_create(&pthread_id, &attr, thread_func, NULL) < 0) { error_handler("test_thread_init create thread failed\n"); } pthread_attr_destroy(&attr); return; } int main() { test_thread_init(thread_1); test_thread_init(thread_2); pid_t pid = getpid(); pthread_t pth_id = pthread_self(); //pid_t tid = gettid(); pid_t tid = syscall(SYS_gettid); printf("------thread main begin pid=%d, tid=%d, thread id=%lu-----\n", pid, tid, pth_id); while(1) { pause(); } printf("====>main EXIT\n"); return 0; }
测试结果
$ gcc test.c -o test -lpthread
$ ./test
——thread main begin pid=8010, tid=8010, thread id=3075487488—–
——thread 1 begin pid=8010, tid=8011, thread id=3075484480—–
——thread 2 begin pid=8010, tid=8012, thread id=3075349312—–
========do a lot of work====
========do a lot of work====
^C
$ ps -aux | grep test
lanyang 8010 0.0 0.0 2536 316 pts/1 Sl+ 10:30 0:00 ./test
$ ls /proc/8010/task/
8010 8011 8012
从结果可以看到主线程创建了两个线程
3075484480、3075349312对应的进程ID分别为8011、8012.
由于gettid()没有在标准C库中,所以使用时要使用系统调用号去调用。系统调用号可以通过查看文件得到:
/usr/include/asm/unistd.h
相关文章推荐
- centOS7 下找不到eth0 的解决方案!
- Linux内核头文件
- linux下php代码不执行、不解析问题
- Linux IO
- Centos下安装mysql 总结
- 将linux用在开发环境中
- linux自定义脚本添加到rc.local脚本无法正常运行的问题
- Linux 利用管道父子进程间传递数据
- linux下一个C语言要求CPU采用
- Linux基础知识全面总结
- Linux内核模块编译----Makefile 模板
- Cgroup maintainer丽泽范:解剖Linux核心容器技术
- Linux的特殊文件权限
- Linux下如何实现备份和还原
- Linux内核makefile解析
- 有哪些高效但是不常见的Linux命令?
- Linux下批量修改文件名
- Linux log日志路径
- Linux 汇编
- linux mount命令详解