免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: albcamus
打印 上一主题 下一主题

Linux上posix线程库实现原理讨论 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2005-03-10 09:04 |只看该作者

Linux上posix线程库实现原理讨论

我是看《nptl-design》中这么说的:
       The necessary memory blocks are merged. I.e., the thread data structures and the thread-local storage are placed on the stack. The usable stack array starts just below (or above in case of an upward growing stack) the memory needed for the two.


如果我没理解错,就应该是这个意思线程数据结构和线程专有数据被放在栈里,传统的栈数据(那些自动变量、函数调用现场等)正好就在上面这2中数据的下边,并往下增长。如果某个系统的栈是从下向上增长的,那么上面的顺序就颠倒过来。

这样看来,线程的栈不应该在HEAP中啊

论坛徽章:
0
22 [报告]
发表于 2005-03-10 10:15 |只看该作者

Linux上posix线程库实现原理讨论

Here the stack refers to the"THREAD's STACK", not the process's stack.
You could clarify it by just do a trivial experiement such as;

1.create several threads, and let them run before main thread;
2.define some local variables in those  threads , and then check the address of those variables, assume them be At1....Atn;
3.malloc or sbrk a memory space in main thread, check the address of this space( name it Amain),  

so if the Amain < A1..An, then you got it
otherwise , you got me here.

论坛徽章:
0
23 [报告]
发表于 2006-05-16 15:11 |只看该作者
学习了!

论坛徽章:
0
24 [报告]
发表于 2006-05-16 15:24 |只看该作者
原帖由 柳五随风 于 2005-3-9 11:36 发表
关于M:N的问题
有一篇论文专门讲的,问题的根源在于MEMORY MANAGEMENT部分是在N之间共享的,是CRITICAL SECTION,影响性能(基于SINGEL CPU,或者SHAREDMEMORY MODEL的SMP系统).
对于其他比如CRO ...

能说一下是什么论文么?
M:N在实际中的性能似乎确实不太好。
多谢了。

论坛徽章:
0
25 [报告]
发表于 2006-05-16 15:43 |只看该作者
我的理解:
POSIX线程实际上只是提供了一套可移植的线程函数及语义,底层实现到底是1:1还是N:1至少在理论上对于POSIX线程的使用者来说是透明的。

论坛徽章:
0
26 [报告]
发表于 2006-05-16 16:10 |只看该作者
原帖由 JohnBull 于 2006-5-16 15:43 发表
我的理解:
POSIX线程实际上只是提供了一套可移植的线程函数及语义,底层实现到底是1:1还是N:1至少在理论上对于POSIX线程的使用者来说是透明的。

感觉应该是这样。

论坛徽章:
0
27 [报告]
发表于 2006-05-16 16:11 |只看该作者
这个程序可以看出什么在什么地方:




  1. #include <stdio.h>
  2. #include <pthread.h>

  3. int seq = 0;
  4. void thread0(int v)
  5. {
  6. int i;
  7.     while(1) {
  8.     printf("This is a pthread0 stack %p global %p args %p %d\n", &i, &seq, &v, seq++);
  9.     sleep(2);
  10.     }
  11. }

  12. void thread1(void)
  13. {
  14. int i;
  15.     while(1) {
  16.     printf("This is a pthread1 stack %p %d\n", &i, seq++);
  17.     sleep(2);
  18.     }
  19. }

  20. int main(int argc, char **argv)
  21. {
  22. pthread_t id[4];
  23. int i,ret;

  24.   printf("main stack = %p  main args =%p\n", &i, &argc);

  25.   ret = pthread_create(&id[0], NULL, (void *)thread0, NULL);
  26.   ret = pthread_create(&id[1], NULL, (void *)thread1, NULL);

  27.   if(ret!=0){
  28.     printf ("Create pthread error!\n");
  29.     exit (1);
  30.   }

  31.   printf("This is the main process.\n");

  32.   pthread_join(id[0],NULL);


  33.   return (0);

  34. }

复制代码


原帖由 albcamus 于 2005-3-10 09:04 发表
我是看《nptl-design》中这么说的:


如果我没理解错,就应该是这个意思线程数据结构和线程专有数据被放在栈里,传统的栈数据(那些自动变量、函数调用现场等)正好就在上面这2中数据的下边,并往下增长。如果 ...

论坛徽章:
0
28 [报告]
发表于 2006-05-16 20:32 |只看该作者
楼主是不是考虑重写或者更新一下这篇文档?

1、Linux 平台上真正的 M:1 (还是 1:M?就是用户态线程)是 GNU Pth
2、LinuxThreads 是 1:1 模型而不是 1:N(N:1??)模型
3、NTPL 也是 1:1 模型,但是核内 task_struct 加入了特定于线程的数据项且修改了信号响应方式等等以对 POSIX 线程标准进行支持

论坛徽章:
0
29 [报告]
发表于 2006-05-16 20:41 |只看该作者
受教,谢谢!

论坛徽章:
0
30 [报告]
发表于 2006-05-16 21:06 |只看该作者

回复 27楼 思一克 的帖子

什么系统阿?
void thread0(int v)  怎么这样定义? pthread 好像该是: void *thread0(void *v)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP