consider the following c programmain()
{
fork();
fork();
printf("hello!\n");
}
为什么会print出4个'hello!'?[张翠山 (1-14 14:41, Long long ago)]
[ 传统版 |
sForum ][登录后回复]1楼
谢谢![张翠山 (1-14 14:41, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼
是不是这样读到第一个fork(),program另外copy多一个fork();printf()
so far, you have 1 parent program and exact same child program with the content---- fork(); printf();
each of them read upto second fork() will produce two children programs again, with the content---printf();
It's like a tree.
1
^
1 1
^ ^
11 11
After I tried this simply program in unix, i get the answer
4[澳洲野狗 (1-14 14:48, Long long ago)]
[ 传统版 |
sForum ][登录后回复]3楼
[A]BECAUSE YOU FORK() TWICE, YOU HAVEFOUR PROCESSES RUNNING BEFORE PRINTF IS EXCUTED.[不告诉你 (1-14 14:50, Long long ago)] [ 传统版 | sForum ][登录后回复]4楼
(引用 澳洲野狗:是不是这样读到第一个fork(),program另外copy多一个fork();printf() so far, you have 1 parent program and exact same child program w...)updated读到第一个fork(),program另外copy多一个fork();printf() so far, you have 1 parent program and exact same child program with the content---- fork(); printf(); each of them read upto second fork() will produce two children programs again, with the content---printf();(by now you will have 4 printf("hello!") After I tried this simply program in unix, i get the answer 4 It's like a tree. f:fork p:printf [澳洲野狗 (1-14 14:56, Long long ago)] [ 传统版 | sForum ][登录后回复]5楼
(引用 澳洲野狗:updated读到第一个fork(),program另外copy多一个fork();printf() so far, you have 1 parent program and exact same child program with ...)[AGREE]EXACTLY.[不告诉你 (1-14 15:13, Long long ago)] [ 传统版 | sForum ][登录后回复]6楼
pls refer to cs2106 lecture notes II.[W风R雨S雪 (1-15 1:03, Long long ago)] [ 传统版 | sForum ][登录后回复]7楼