Linux中四个fork()后创建了多少个进程? [英] How many processes are created after four fork() in Linux?
问题描述
我在操作系统教科书中看到了一个愚蠢的问题.问题如下:
假设所有fork系统调用都成功.执行如下所示程序后.屏幕上有多少行?
我假设每个 fork 调用都会使进程加倍,因此结果应该是创建了 16 个进程.但是当我从教科书中输入相同的代码时,我得到了 30 行.这是结果和代码:
http://imgur.com/zrdOP0X
I have a dumb question seeing on the Operating system textbook. The question is below:
Assume that all fork system calls are successful.After executing program shown below. How many lines are on the screen?
I assume that every fork call doubles the processes, so the result should be 16 process are created. But when I type the same code from the textbook, I got 30 lines. Here is the result and the code:
http://imgur.com/zrdOP0X
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
fork();
printf("After fork1
");
fork();
printf("After fork2
");
fork();
printf("After fork3
");
fork();
printf("After fork4
");
}
推荐答案
结果应该是这样的:
fork1叉子2叉子3叉子4叉子4叉子3叉子4fork4
叉子2叉子3叉子4叉子4叉子3叉子4叉子4叉子1叉子2叉子3叉子4叉子4叉子3叉子4fork4
叉子2叉子3叉子4叉子4叉子3叉子4fork4
(空格只是为了更好的阅读)
(the spaces are only for better reading)
这篇关于Linux中四个fork()后创建了多少个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!