考虑给定的 `fork` 程序,以下程序打印了多少次 hello?

consider the `fork` program given, how many times does the following program print hello?

Please, consider the fork program given below:

下面的程序打印了多少次hello?

main(int argc, char **argv) {
 int i;
 for (i=0; i < 3; i++) {
 fork();
 printf("hello\n");
 }
}

“问候”消息总数 = 2 + 4 + 8 = 14 (see question-1 at page-5)。


下面的程序打印了多少次hello?

#include <stdio.h>
#include <unistd.h>
main()
{
int i;
for (i=0; i<3; i++)
fork();
printf("hello\n");
}

“问候”消息总数 = 8 (see question-4 at page-5)。

在我看来这两个程序是一样的。为什么 explanation/answer 不同?

Can you explain, please?

第一个程序中的 printf("hello\n”); 语句在 for 循环内。

for (i=0; i < 3; i++) {
 fork();
 printf("hello\n”);
 }

并且,第二个程序中的 printf("hello\n”); 语句在 for 循环之外。

for (i=0; i<3; i++)
fork();//after semicolon printf is outside the for loop's block scope.
printf("hello\n");

这是给定代码之间的区别。