免费注册 查看新帖 |

Chinaunix

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

请问:linux的线程是由新的进程运行的么 谢谢 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-09 23:15 |只看该作者 |倒序浏览
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 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-02-09 23:34 |只看该作者
你看看两个线程 thread id,和 process id,分别有什么关系没

论坛徽章:
0
3 [报告]
发表于 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

论坛徽章:
0
4 [报告]
发表于 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

论坛徽章:
0
5 [报告]
发表于 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 编辑 ]

论坛徽章:
0
6 [报告]
发表于 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 ...


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

--

论坛徽章:
0
7 [报告]
发表于 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的实现改动了呢?

论坛徽章:
0
8 [报告]
发表于 2007-02-10 01:18 |只看该作者
--

correct

NPTL -- Native POSIX Threads Library

--

论坛徽章:
0
9 [报告]
发表于 2007-02-10 08:21 |只看该作者


[ 本帖最后由 Cocory 于 2007-2-10 08:26 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2007-02-10 08:27 |只看该作者
Advanced Linux Programming 那本书里线程部分和第二卷的部分内容有点过时了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP