Chinaunix

标题: 请问:linux的线程是由新的进程运行的么 谢谢 [打印本页]

作者: wtwang1985    时间: 2007-02-09 23:15
标题: 请问:linux的线程是由新的进程运行的么 谢谢
advance linux programing上说:
Whenever you call pthread_create to create a new thread, Linux creates a new process that runs that thread.
还附了一个程序:
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. void* thread_function (void* arg) {
  5. fprintf (stderr, “child thread pid is %d\n”, (int) getpid ());
  6. /* Spin forever. */
  7. while (1);
  8. return NULL;
  9. }
  10. int main () {
  11. pthread_t thread;
  12. fprintf (stderr, “main thread pid is %d\n”, (int) getpid ());
  13. pthread_create (&thread, NULL, &thread_function, NULL);
  14. /* Spin forever. */
  15. while (1);
  16. return 0;
  17. }
复制代码

作者的运行结果是:
% ./thread-pid &
[1] 14608
main thread pid is 14608
child thread pid is 14610
% ps x
PID TTY STAT TIME COMMAND
14042 pts/9 S 0:00 bash
14608 pts/9 R 0:01 ./thread-pid
14609 pts/9 S 0:00 ./thread-pid
14610 pts/9 R 0:01 ./thread-pid
14611 pts/9 R 0:00 ps x
但我的机子上运行的结果是:
./thread-pid &
[1] 13071
david@david-desktop:~/alp/ALP-listings/chapter-4$ main thread pid is 13071
child thread pid is 13071
呵呵。小弟是个菜鸟。请各位指教。

[ 本帖最后由 wtwang1985 于 2007-2-9 23:17 编辑 ]
作者: langue    时间: 2007-02-09 23:34
你看看两个线程 thread id,和 process id,分别有什么关系没
作者: wtwang1985    时间: 2007-02-09 23:41
一样的。
david@david-desktop:~/alp/ALP-listings/chapter-4$ ./thread-pid
main thread pid is 13183
child thread pid is 13183
child thread pid is 13183
作者: wtwang1985    时间: 2007-02-09 23:44
david@david-desktop:~/alp/ALP-listings/chapter-4$ ./thread-pid &
[1] 13209
main thread pid is 13209
david@david-desktop:~/alp/ALP-listings/chapter-4$ child thread pid is 13209
child thread pid is 13209
david@david-desktop:~/alp/ALP-listings/chapter-4$ ps
  PID TTY          TIME CMD
12732 pts/0    00:00:00 bash
13209 pts/0    00:00:02 thread-pid
13212 pts/0    00:00:00 ps
作者: wtwang1985    时间: 2007-02-09 23:51
david@david-desktop:~/alp/ALP-listings/chapter-4$ ./thread-pid&
[1] 13318
main thread pid is 13318 and thread_id is 3084891840
david@david-desktop:~/alp/ALP-listings/chapter-4$ child thread pid is 13318 and thread_id is 3084889008
child thread pid is 13318 and thread_id is 3076496304

[ 本帖最后由 wtwang1985 于 2007-2-9 23:56 编辑 ]
作者: langue    时间: 2007-02-10 00:28
原帖由 wtwang1985 于 2007-2-9 23:51 发表
david@david-desktop:~/alp/ALP-listings/chapter-4$ ./thread-pid&
[1] 13318
main thread pid is 13318 and thread_id is 3084891840
david@david-desktop:~/alp/ALP-listings/chapter-4$ child thread ...


说明在你的系统上,进程的多个线程被封装到一个统一的进程里。别的系统可能不是这种情况。

--
作者: wtwang1985    时间: 2007-02-10 00:47
我在一台Linux sarge.lpicn.org 2.4.30-osl2.0 #9 Fri Jan 6 17:26:47 CST 2006 i686 GNU/Linux
机子上测试的结果是:
[root@sarge ~/public] ./thread-pid &
[1] 12350
[root@sarge ~/public] main thread pid is 12350 and thread_id is 16384
child thread pid is 12352 and thread_id is 16386

[root@sarge ~/public] ps
  PID TTY          TIME CMD
11978 pts/1    00:00:00 bash
12350 pts/1    00:00:01 thread-pid
12351 pts/1    00:00:00 thread-pid
12352 pts/1    00:00:01 thread-pid
12364 pts/1    00:00:00 ps
[root@sarge ~/public] killall thread-pid
[root@sarge ~/public] ps
  PID TTY          TIME CMD
11978 pts/1    00:00:00 bash
12453 pts/1    00:00:00 ps
[1]+  Terminated              ./thread-pid
我就想是不是2.6内核将pthread的实现改动了呢?
作者: Iamlangue    时间: 2007-02-10 01:18
--

correct

NPTL -- Native POSIX Threads Library

--
作者: Cocory    时间: 2007-02-10 08:21


[ 本帖最后由 Cocory 于 2007-2-10 08:26 编辑 ]
作者: wolf0403    时间: 2007-02-10 08:27
Advanced Linux Programming 那本书里线程部分和第二卷的部分内容有点过时了。
作者: picobsd    时间: 2007-02-13 13:09
以前确实是这样
现在已经开始改观了,好像使用了新的线程库
作者: jackk294    时间: 2007-02-14 16:34
原帖由 picobsd 于 2007-2-13 13:09 发表
以前确实是这样
现在已经开始改观了,好像使用了新的线程库


就是,现在使用readhat开发的nptl线程库了
http://www.ibm.com/developerworks/cn/linux/l-threading.html




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