我有这段代码:
#include<stdio.h>
void createChild(){
int pid;
pid=fork();
if(pid<0){
printf("creation error");exit()
}
if(pid>0)return;
printf("Processus of number %d\n",getpid());
}
int main()
{
createChild();
createChild();
createChild();
}
我需要知道这段代码生成了多少进程以及正确的解释?请任何帮助
答案 0 :(得分:1)
以下是发生的事情:
Process: Actions
1: [fork (creates 2)] [fork (creates 3)] [fork (creates 5)] [end of main]
2: [fork (creates 4)] [fork (creates 6)] [end of main]
3: [fork (creates 7)] [end of main]
4: [fork (creates 8)] [end of main]
5: [end of main]
6: [end of main]
7: [end of main]
8: [end of main]
你可以在这里看到基数2的明确指数。每个fork()
创建一个从fork调用返回开始执行的子节点。 (您可以使用返回值将孩子与父母区别开来。)