C语言 使用这些 fork() 语句创建了多少进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19106576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How many processes are created with these fork() statements?
提问by Ben Reed
I believe that this creates 24 processes; however, I need verification. These questions often stump me. Thanks for the help!
我相信这会创建 24 个进程;但是,我需要验证。这些问题常常让我难受。谢谢您的帮助!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
pid_t pid = fork();
pid = fork();
pid = fork();
if (pid == 0)
{
fork();
}
fork();
return 0;
}
回答by sfstewman
It's fairly easy to reason through this. The forkcall creates an additional process every time that it's executed. The call returns 0in the new (child) process and the process id of the child (not zero) in the original (parent) process.
通过这个推理相当容易。该fork调用每次执行时都会创建一个额外的进程。该调用0在新(子)进程中返回,并在原始(父)进程中返回子进程(非零)的进程 ID。
pid_t pid = fork(); // fork #1
pid = fork(); // fork #2
pid = fork(); // fork #3
if (pid == 0)
{
fork(); // fork #4
}
fork(); // fork #5
- Fork #1 creates an additional processes. You now have two processes.
- Fork #2 is executed by two processes, creating two processes, for a total of four.
- Fork #3 is executed by four processes, creating four processes, for a total of eight. Half of those have
pid==0and half havepid != 0 - Fork #4 is executed by half of the processes created by fork #3 (so, four of them). This creates four additional processes. You now have twelve processes.
- Fork #5 is executed by all twelve of the remaining processes, creating twelve more processes; you now have twenty-four.
- Fork #1 创建了一个额外的进程。您现在有两个进程。
- Fork #2 由两个进程执行,创建两个进程,总共四个。
- Fork #3 由四个进程执行,创建四个进程,总共八个。其中一半有
pid==0,一半有pid != 0 - Fork #4 由 fork #3 创建的一半进程执行(因此,其中四个)。这将创建四个额外的进程。您现在有十二个进程。
- Fork #5 由其余 12 个进程执行,创建了另外 12 个进程;你现在有二十四个。
回答by Roshan Mehta
Calculate in this way :
这样计算:
Start with 1(Main Process) and for every fork make it twice if fork is not inside if(pid == 0) else add 1/2 of current process to current number of process.
从 1(主进程)开始,如果 fork 不在 if(pid == 0) 内,则对每个 fork 进行两次,否则将当前进程的 1/2 添加到当前进程数。
In your code: 1P Got #1 fork() so double up current number of processes. Now new number of process 2P
在您的代码中: 1P Got #1 fork() 因此将当前进程数加倍。现在新的进程数 2P
Got #2 fork() so double up current number of processes. Now new number of process 4P
得到了 #2 fork(),因此将当前进程数加倍。现在新的进程数 4P
Got #3 fork() so double up current number of processes. Now new number of process 8P
得到了 #3 fork(),因此将当前进程数加倍。现在新的进程数8P
Got #4 fork() but wait it's in if condition so (8+4 = 12)P
得到 #4 fork() 但等待它处于 if 条件所以 (8+4 = 12)P
Got #5 fork() so double up the current number of processes. Now new number of process 24P
得到 #5 fork() 所以将当前进程数加倍。现在新的进程数24P
回答by willus
You are correct. It's 24. Just compiled and ran it w/printf before the final return statement. Got 24 lines of output.
你是对的。它是 24。刚刚编译并在最终 return 语句之前使用 printf 运行它。有 24 行输出。
回答by Moeez Mehmood
This statement have 24+ child process. Each invocation of fork() results in two processes, the child and the parent. Thus the first fork results in two processes. The second fork() is reached by those two processes, yielding four processes. The final fork() is reached by those four, netting eight processes more. All but one of these processes (the original) is a child of at least one of the forks.
这个语句有 24+ 个子进程。每次调用 fork() 都会产生两个进程,子进程和父进程。因此,第一个分叉导致两个进程。这两个进程到达第二个 fork(),产生四个进程。最后的 fork() 由这四个到达,另外还有八个进程。除了其中一个进程(原始进程)之外的所有进程都是至少一个分支的子进程。

