1. Linux线程的线程ID和进程ID
Linux内核并不支持真正意义上的线程,Linux线程库是用与普通进程具有同样内核调度视图的轻量级进程来实现线程支持的。这些轻量级进程拥有独立的进程id,在进程调度、信号处理、IO等方面享有与普通进程一样的能力。每个Linux线程都同时具有线程id和进程id,其中进程id就是内核所维护的进程号,而线程id则由线程库分配和维护。
1)每个进程都有自己的PID,这是唯一的。
2)除此之外,每个进程还有其它的ID标识,比如处于某个线程组中的所有进程都有一个统一的线程组ID(TGID),如果进程没有使用线程,PID和TGID是相同的,其实主线程的PID和TGID就是相同的。
3)独立进程可以合并成为进程组。
4)几个进程组可以合并成一个会话,会话中的所有进程都有同样的会话ID。
重点关注一下 task_struct 中与进程ID相关的两个成员:
struct task_struct{
pid_t pid;
pid_t tgid;
}
属于同一个进程的所有线程,tgid是相同的,都指向线程组第一个进程(主线程)的PID值。
可以分别通过getpid(), syscall(SYS_gettid)获取对应的值。结合下面第2部分的程序,能够更直观地看出getpid()获取tgid、syscall()获取PID。
因此,对于Linux而言,所谓线程组共享进程ID,其实是通过 tgid 表示的,而线程组中的每个线程本质上是有自己的进程ID号的。
2. 通过程序查看线程相关的ID
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <linux/unistd.h>
pid_t gettid()
{
return syscall(__NR_gettid);
}
static void *phandle(void *arg)
{
pid_t pid_chld, pid_chld1;
pthread_t tid;
pid_chld = getpid();
pid_chld1 = gettid();
tid = pthread_self();
printf("<child pthread>@(pid=%d/ptid=%d/tid=%lu) Child pthread ready to sleep 5 \r\n", pid_chld, pid_chld1,tid);
sleep(2);
printf("<child pthread>@(pid=%d/ptid=%d/tid=%lu) Child pthread exit\r\n", pid_chld, pid_chld1,tid);
return NULL;
}
int main(int argc, char *argv[])
{
int cnt;
pid_t pid;
pthread_t g_tid,tid;
pid = getpid();
g_tid = pthread_self();
printf("[[main pthread]] (pid=%lu/tid_mian=%lu) start! \r\n", pid, g_tid);
for(cnt=0; cnt<2; cnt++)
{
pthread_create(&tid,NULL,phandle,NULL);
printf("[[main pthread]] create a child pthread_%d(%lu)\r\n",cnt,tid);
}
sleep(5);
printf("[[main pthread]] (pid=%lu/tid_mian=%lu) exit!\r\n", pid, g_tid);
exit(0);
}
执行结果如下:
# gcc -o pid pthread_pid_tid.c -lpthread
# ./pid
[[main pthread]] (pid=3221/tid_mian=3076355776) start!
[[main pthread]] create a child pthread_0(3076352832)
[[main pthread]] create a child pthread_1(3067960128)
<child pthread>@(pid=3221/ptid=3223/tid=3067960128) Child pthread ready to sleep 5
<child pthread>@(pid=3221/ptid=3222/tid=3076352832) Child pthread ready to sleep 5
<child pthread>@(pid=3221/ptid=3223/tid=3067960128) Child pthread exit
<child pthread>@(pid=3221/ptid=3222/tid=3076352832) Child pthread exit
[[main pthread]] (pid=3221/tid_mian=3076355776) exit!
3. 转载网上的一篇关于线程ID和进程ID的文章
原文:http://blog.chinaunix.net/uid-24567872-id-100482.html 点击打开链接在linux中,线程与进程最大的区别就是是否共享同一块地址空间,而且共享同一块地址空间的那一组线程将显现相同的PID号。
在实际编程应用中,我们会很容易发现并证明,一组同源线程的PID都是一样的,但它们的PID真的一样么?
在linux中,线程的创建和普通进程的创建类似,只不过在调用clone()的时候需要传递一些参数标志来指明需要共享的资源:
- clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
对比一下,一个普通的fork()的实现是:
- clone(SIGCHLD, 0);