Linux线程ID和进程ID

本文探讨了Linux线程的线程ID和进程ID,并通过程序展示了如何查看线程相关的ID。实验结果显示,每个线程都有其独立的线程ID,而所有线程共享相同的进程ID。此外,文章还引用了线程创建和克隆的示例代码,进一步解释了线程与进程的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()的时候需要传递一些参数标志来指明需要共享的资源:
  1. clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
       上面的代码产生的结果和调用fork()差不多,只是父子俩共享地址空间、文件系统资源、文件描述符和信号处理程序。换个说法就是, 新建的进程和它的父进程就是流行的所谓线程 。  
       对比一下,一个普通的fork()的实现是:
  1. clone(SIGCHLD, 0);
      而vfork()的实现是:
  1. clone(CLONE_VFORK | CLONE_VM | SIGCHLD, 0);
      传递给clone()的参数标志决定了新创建进程的行为方式和父子进程之间共享的资源种类。下面列举部分clone()参数标志,这些是在<linux/sched.h>中定义的。
       CLONE_FILES     父子进程共享打开的文件
       CLONE_FS    父子进程共享文件系统信息
       CLONE_SIGHAND      父子进程共享信号处理函数
       CLONE_VM     父子进程共享地址空间
       CLONE_VFORK    调用vfork(),所以父进程准备睡眠等待子进程将其唤醒

下面用一段程序测试一下,代码如下:
这段测试程序是主线程创建10个线程,并分别打印PID及TID。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <string.h>

  7. pthread_t tid[10];

  8. void * thread_handler(void *arg)
  9. {
  10.     printf("tid%d:%u,pid:%u\n", (int)arg, (unsigned)pthread_self(), 
  11.         (unsigned)getpid());
  12.     while(1){
  13.         sleep(1);
  14.     }
  15.     return NULL;
  16. }

  17. int main(void)
  18. {
  19.     int i, ret;
  20.     pid_t pid;
  21.     printf("main tid:%u,pid:%u\n", (unsigned)pthread_self(),
  22.         (unsigned)getpid());
  23.     for(= 0; i < 10; i++){
  24.         if((ret = pthread_create(&tid[i], NULL, thread_handler, 
  25.             (void *)i)) != 0){
  26.             fprintf(stderr, "pthread_create:%s\n",
  27.                 strerror(ret));
  28.             exit(1);
  29.         }
  30.     }
  31.     sleep(3);
  32.     pid = fork();
  33.     if(pid == 0){
  34.         printf("son tid:%u,pid:%u\n", (unsigned)pthread_self(),
  35.             (unsigned)getpid());
  36.         while(1);
  37.             sleep(1);
  38.     }
  39.     while(1)
  40.         sleep(2);
  41.     exit(0);
  42. }
编译运行:
  1. zx@zhangxu:~/lianxi/apue$ gcc -o test pthreadid.-lpthread
  2. zx@zhangxu:~/lianxi/apue$ ./test
  3. main tid:3077888816,pid:2418
    tid0:3077880688,pid:2418
    tid1:3069487984,pid:2418
    tid2:3061095280,pid:2418
    tid3:3052702576,pid:2418
    tid4:3044309872,pid:2418
    tid5:3035917168,pid:2418
    tid6:3027524464,pid:2418
    tid7:3019131760,pid:2418
    tid8:3010739056,pid:2418
    tid9:3002346352,pid:2418
    son tid:3077888816,pid:2429
从结果可以看出,测试程序中所有线程的PID都相同。

在另一个终端调用pstree -pu
  1. ├─gnome-terminal(1624,zx)─┬─bash(1628)
    │                         ├─bash(1704)───pstree(2430)
    │                         ├─bash(1927)───test(2418)─┬─test(2429)
    │                         │                         ├─{test}(2419)
    │                         │                         ├─{test}(2420)
    │                         │                         ├─{test}(2421)
    │                         │                         ├─{test}(2422)
    │                         │                         ├─{test}(2423)
    │                         │                         ├─{test}(2424)
    │                         │                         ├─{test}(2425)
    │                         │                         ├─{test}(2426)
    │                         │                         ├─{test}(2427)
    │                         │                         └─{test}(2428)

从中可以看出,每个线程的PID其实是不同的,因为测试程序是理想状态,只有一个主线程在创建线程,所以PID的值都是连续的。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值