Chinaunix首页 | 论坛 | 博客
  • 博客访问: 251586
  • 博文数量: 88
  • 博客积分: 1429
  • 博客等级:
  • 技术积分: 523
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-18 15:31
文章分类

全部博文(88)

文章存档

2017年(2)

2016年(24)

2013年(1)

2012年(24)

2011年(15)

2010年(22)

我的朋友

分类: LINUX

2011-12-13 19:03:22

看ULK3时,说getpid返回的是当前进程的tgid,而不是pid。
对于在一个进程里创建的线程都具有相同的PID,而创建这些线程的进程的pid和tpid的值是相等的。
写了个简单的程序测试下。
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<pthread.h>
  4. #include<stdlib.h> //exit

  5. typedef unsigned int uint;

  6. static void *
  7. p1_func(void *arg)
  8. {
  9.         pid_t pid;
  10.         pthread_t tid;

  11.         pid = getpid();
  12.         tid = pthread_self();
  13.         printf("[thread 1]\t\tpid = %u\t tid = %u\n", (uint)pid, (uint)tid);

  14.         return NULL;
  15. }

  16. static void *
  17. p2_func(void *arg)
  18. {
  19.         pid_t pid;
  20.         pthread_t tid;

  21.         pid = getpid();
  22.         tid = pthread_self();
  23.         printf("[thread 2]\t\tpid = %u\t tid = %u\n", (uint)pid, (uint)tid);
  24.         sleep(1);

  25.         return NULL;
  26. }

  27. static void *
  28. p3_func(void *arg)
  29. {
  30.         pid_t pid;
  31.         pthread_t tid;

  32.         pid = getpid();
  33.         tid = pthread_self();
  34.         printf("[thread 3]\t\tpid = %u\t tid = %u\n", (uint)pid, (uint)tid);

  35.         return NULL;
  36. }

  37. int main()
  38. {
  39.         pid_t pid, ppid, pid1, pid2;
  40.         pthread_t tid1, tid2;
  41.         int err1, err2;

  42.         if ((pid=fork()) < 0){
  43.                 perror("fork error in getpid.c");
  44.                 exit(-1);
  45.         }
  46.         else if (pid == 0){ //son
  47.                 ppid = getppid();
  48.                 pid1 = getpid();
  49.                 printf("[son]\t\tppid = %u\t pid = %u\n", (uint)ppid, (uint)pid1);
  50.                 err1 = pthread_create(&tid1, NULL, p1_func, NULL);
  51.                 if (err1 != 0){
  52.                         perror("pthread_create error\n");
  53.                         exit(-1);
  54.                 }
  55.         }
  56.         else{ //parent
  57.                 sleep(1);
  58.                 pid2 = getpid();
  59.                 printf("[parent]\t\tpid = %u\n", pid2);
  60.                 err2 = pthread_create(&tid2, NULL, p2_func, NULL);
  61.                 if (err2 != 0){
  62.                         perror("pthread_create error\n");
  63.                         exit(-1);
  64.                 }
  65.                 err2 = pthread_create(&tid2, NULL, p3_func, NULL);
  66.                 if (err2 != 0){
  67.                         perror("pthread_create error\n");
  68.                         exit(-1);
  69.                 }
  70.                 sleep(2);
  71.         }
  72.         
  73.         return 0;
  74. }
结果:
  1. [jrq@Fedora LinuxC]$ gcc -o getpid getpid.c -lpthread
  2. [jrq@Fedora LinuxC]$ ./getpid
  3. [son]        ppid = 28178     pid = 28179
  4. [thread 1]        pid = 28179     tid = 3086056336
  5. [parent]        pid = 28178
  6. [thread 2]        pid = 28178     tid = 3086056336
  7. [thread 3]        pid = 28178     tid = 3075566480
由后三行可知,线程2、3和进程的pid是相同的。他们在一个线程组里。
阅读(1097) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~