红联Linux门户
Linux帮助

linux线程id与进程id对应关系

发布时间:2015-10-18 10:01:11来源:linux网站作者:lanyang123456

linux中的线程是基于进程实现的,每个线程都会有一个进程对应,通过gettid()可以获取到该进程id。另外,通过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


Linux下获得线程ID syscall(224):http://www.linuxdiyf.com/linux/10594.html

Linux进程与线程的区别和联系:http://www.linuxdiyf.com/linux/13897.html

Linux系统入门学习:Linux上如何查看某个进程的线程:http://www.linuxdiyf.com/linux/12580.html

Linux进程或线程绑定到CPU:http://www.linuxdiyf.com/linux/11873.html

Linux下查看进程和线程:http://www.linuxdiyf.com/linux/7149.html