Chinaunix

标题: linux 线程的疑问 (apue2 11章的例题) [打印本页]

作者: changyongID    时间: 2010-02-05 16:52
标题: linux 线程的疑问 (apue2 11章的例题)
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值看起来十六
        进制的表示更有意义些,这是为何?
作者: churchmice    时间: 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).

作者: changyongID    时间: 2010-02-05 17:15
1.是的
2.个人偏好,程序员看十六进制爽一些

man pthread,拉到最后看bug


可否说的更为详细一点。

ps:man pthread 出来什么也没有
      man pthreads 中没有你列出来的
      man pthread_create 也没见你列出来的那些text
你man的是啥子?
作者: churchmice    时间: 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的,也就是一样的。
作者: vbs100    时间: 2010-02-06 00:16
线程库问题 书上用的是linuxthread 而现在都用NPTL了
如果你想体验书中的结果 你加个环境变量 LD_ASSUME_KERNEL=2.2.5
作者: changyongID    时间: 2010-02-08 09:39
谢谢楼上,结贴。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2