linux多线程在/proc目录的结构

linux的多线程,其实就是clone系统调用的实现的(共享文件等)

首先看个具体的例子:

[cpp]  view plain  copy
  1. #include <stdio.h>    
  2. #include <stdlib.h>    
  3. #include <pthread.h>    
  4. #include <unistd.h>  
  5. #include <string.h>  
  6. #include <sched.h>  
  7. #include <errno.h>  
  8.     
  9. void *print_message_function( void *ptr );    
  10.   
  11. int main()    
  12. {    
  13.   
  14.       pthread_t thread1, thread2;    
  15.       const char *message1 = "Thread 1";    
  16.       const char *message2 = "Thread 2";    
  17.       int  iret1, iret2;    
  18.      
  19.      /* Create independent threads each of which will execute function */   
  20.       iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);    
  21.       iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);    
  22.   
  23.       /* Wait till threads are complete before main continues. Unless we  */   
  24.       /* wait we run the risk of executing an exit which will terminate   */   
  25.       /* the process and all threads before the threads have completed.   */       
  26.   
  27.       pthread_join( thread1, NULL);    
  28.       pthread_join( thread2, NULL);         
  29.       printf("Thread 1 returns: %d\n",iret1);    
  30.       printf("Thread 2 returns: %d\n",iret2);    
  31.       for(;;)  
  32.       {  
  33.          usleep(250*1000);  
  34.       }   
  35.       return 0;  
  36.         
  37. }        
  38. void *print_message_function( void *ptr )    
  39. {    
  40.       char *message;    
  41.       for(;;)  
  42.       {  
  43.          message = (char *) ptr;    
  44.          printf("%s \n", message);  
  45.          usleep(3*250*1000);  
  46.       }    
  47.       return 0;  
  48. }   


此时一共有三个进程在跑(相对于kernel来说,只有进程的概念)

一般情况是,一个进程在/proc 目录下就对应一个,以该进程ID号名字的

文件目录,该目录下保存着该进程的所有信息。

但是对应,所谓的多线程,它的目录结构该会这么样呢?

假如该程序名为:thread

$ top  -t  | grep thread
PID      TID PR CPU% S     VSS     RSS PCY UID      Thread          Proc

31694 31694  0   0% S   3008K    416K     root     thread          system/bin/thread
31694 31695  0   0% S   3008K    416K     root     thread          system/bin/thread
31694 31696  0   0% S   3008K    416K     root     thread          system/bin/thread

可知他们对应于一个进程ID号,而在此目录下,分别在有其他三个线程目录信息:

proc/31694/task # ls
31694
31695
31696

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值