《操作系统概念-第10版》第3章练习题第2题:
Including the initial parent process, how many processes are created by
the program shown in the fellowing?
下面的程序创建了几个进程(包括初使的父进程)?
#include <stdio.h>
#include <unistd.h>
int main()
{
// fork a child process
fork();
// fork another child process
fork();
// and fork another
fork();
return 0;
}
答案是共创建了8个进程。其中fork_01调用了1次,fork_02调用了2次,fork_03调用了4次,再加上父进程一共是8个进程。调用一次fork()就创建一个子进程。
程序运行流程图:

第九版的中文版里,此题是调用了4次fork()函数,共创建了16个进程。

结论:若父进程顺序调用n次fork,则共创建个进程。其中:
第1个fork调用次,
第2个fork调用次,
……
第n个fork调用次。
所以共创建子进程的数量为个。
4055

被折叠的 条评论
为什么被折叠?



