Mir*_*ira 2 c operating-system fork
我试图弄清楚为什么这个陈述会产生2个过程
if(fork()&&(fork()||fork()))
printf("Hello");
Run Code Online (Sandbox Code Playgroud)
我理解短路问题,而且我知道如果没有我们将总共获得4个进程而执行此语句.那么你能解释一下插入if这样的语句所使用的标准吗?
应该创建4个进程.您可以通过printf在if声明后添加额外的调用来轻松验证这一点.
在printf("Hello")只运行两次,因为条件是唯一真正对其中的两个过程.
具体来说,根进程生成两个子进程,第二个进程生成一个子进程:
<parent> : condition is true because the two first fork calls return non-0 : (true && (true || ??))
<child1> : condition is false because the first fork returns 0: (false && (?? || ??))
<child2> : condition is true because the first and third fork calls return non-0: (true && (false || true))
<child3> : condition is false because the second and third fork calls return 0: (true && (false || false))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |