[英]fork() and Binary tree creation
How binary tree will be created for following code ? 如何为以下代码创建二叉树?
#include <stdio.h>
#include <unistd.h>
int main()
{
fork();
fork() && fork() || fork();
fork();
printf("forked\n");
return 0;
}
Basically i am not able to solve logical operator conditions .For unconditional fork() statements it can easily be done but what about above code . 基本上我无法解决逻辑运算符条件。对于无条件的fork()语句可以轻松完成,但是上面的代码呢? For reference here is the link http://www.geeksforgeeks.org/fork-and-binary-tree/ 供参考的链接是http://www.geeksforgeeks.org/fork-and-binary-tree/
#include <stdio.h>
int main()
{
fork(); /* A */
( fork() /* B */ &&
fork() /* C */ ) || /* B and C are grouped according to precedence */
fork(); /* D */
fork(); /* E */
printf("forked\n");
return 0;
}
So according to the diagram shown, each node will ALWAYS have 2 childs nodes. 因此,根据所示的示意图,每个节点将始终具有2个子节点。
“On success, the PID of the child process is returned in the parent, and 0 is returned in the child.” “成功后,子进程的PID在父级中返回,而0在子级中返回。”
Lets look at fork B. the parent process(m) will return a non-negetive pid and child process(c1) returns 0. 让我们看一下分叉B。父进程(m)将返回非负数pid,子进程(c1)返回0。
Therefore, the parent process executes fork C , but skips fork D due to short circuit evaluation . 因此,父进程执行分支C ,但由于短路评估而跳过分支D。
(1234 && 4392) || fork()
The newly created child process skips fork C and executes fork D for above mentioned reason. 由于上述原因,新创建的子进程跳过了分叉C并执行了分叉D。
(0 && fork() ) || fork()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.