免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1881 | 回复: 5
打印 上一主题 下一主题

[函数] linux 线程的疑问 (apue2 11章的例题) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-05 16:52 |只看该作者 |倒序浏览
apue2中11章第一个例题,具体代码如下
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>

  5. pthread_t ntid;

  6. void
  7. err_sys(const char *s)
  8. {
  9.         printf("%s\n", s);
  10.         exit(1);
  11. }

  12. void
  13. printids(const char *s)
  14. {
  15.         pid_t pid;
  16.         pthread_t tid;

  17.         pid = getpid();
  18.         tid = pthread_self();
  19.         printf("%s pid %u tid %u (0x%x) \n", s,
  20.                 (unsigned int)pid,
  21.                 (unsigned int)tid,
  22.                 (unsigned int)tid);
  23. }

  24. void *
  25. thr_fn(void *arg)
  26. {
  27.         printids("new thread:");
  28.         return ((void *)0);
  29. }

  30. int
  31. main(void)
  32. {
  33.         int err;

  34.         err = pthread_create(&ntid, NULL, thr_fn, NULL);
  35.         if (err != 0)
  36.                 err_sys("pthread_create error\n");
  37.         printids("main thread:");
  38.         sleep(1);

  39.         exit(0);
  40. }
复制代码
编译
        gcc -Wall test.c -lpthread

./a.out (执行结果如下)
new thread: pid 30381 tid 3085904784 (0xb7ef2b90)
main thread: pid 30381 tid 3085907648 (0xb7ef36c0)

疑问:
        观察程序执行结果,与apue2上面所描述的Linux下执行结果有两点差别:

      o 这里两个线程的pid相同(说明是同一个进程)
        而书中所叙述的是:Linux使用clone系统调用来实现pthread_create,clone用于创建
        子进程。按书所说,主线程和新线程的pid应该不同(书上的结果确实是不同pid)。
        这是否说明2.6内核对线程实现机制有了改变?(书上的结果是在2.4内核环境下的)

      o Linux里pthread_t实现为unsigned long int,但这里打印出来的线程ID值看起来十六
        进制的表示更有意义些,这是为何?

论坛徽章:
0
2 [报告]
发表于 2010-02-05 16:58 |只看该作者
1.是的
2.个人偏好,程序员看十六进制爽一些

man pthread,拉到最后看bug

BUGS
       In the obsolete LinuxThreads implementation, each of the threads  in  a
       process  has a different process ID.  This is in violation of the POSIX
       threads specification, and is the source of many other non-conformances
       to the standard; see pthreads(7).

论坛徽章:
0
3 [报告]
发表于 2010-02-05 17:15 |只看该作者
1.是的
2.个人偏好,程序员看十六进制爽一些

man pthread,拉到最后看bug


可否说的更为详细一点。

ps:man pthread 出来什么也没有
      man pthreads 中没有你列出来的
      man pthread_create 也没见你列出来的那些text
你man的是啥子?

论坛徽章:
0
4 [报告]
发表于 2010-02-05 18:58 |只看该作者
回复 3# changyongID
额,错了
是man pthread_create

POSIX要求要求uid是一样的,但是很古老的linux有可能不是这样。


man 7 pthreads有更详细的信息

THREADS(7)                Linux Programmer's Manual               PTHREADS(7)



NAME
       pthreads - POSIX threads
      
DESCRIPTION
       POSIX.1  specifies  a  set  of interfaces (functions, header files) for
       threaded programming commonly known as POSIX threads, or  Pthreads.   A
       single process can contain multiple threads, all of which are executing
       the same program.  These threads share the same global memory (data and
       heap  segments),  but  each  thread  has its own stack (automatic vari‐
       ables).

       POSIX.1 also requires that threads share a range  of  other  attributes
       (i.e., these attributes are process-wide rather than per-thread):
      
       -  process ID

       -  parent process ID

       -  process group ID and session ID
      
       -  controlling terminal


这里明确说了,pthread的process ID是shared的,也就是一样的。

论坛徽章:
1
寅虎
日期:2014-11-30 21:25:54
5 [报告]
发表于 2010-02-06 00:16 |只看该作者
线程库问题 书上用的是linuxthread 而现在都用NPTL了
如果你想体验书中的结果 你加个环境变量 LD_ASSUME_KERNEL=2.2.5

论坛徽章:
0
6 [报告]
发表于 2010-02-08 09:39 |只看该作者
谢谢楼上,结贴。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP