简体   繁体   English

如何使用&&和||评估这些fork()调用C中的运营商?

[英]How do I evaluate these fork() calls with the && and || operators in C?

I have below some code in C which uses the fork() system call, but I am just confused: how do I go about 我在C下面有一些使用fork()系统调用的代码,但我只是感到困惑:我该怎么做
solving what it does: 解决它的作用:

int main()
{
    fork();
    fork() || fork() && fork();
    fork();
    printf("ämit");
}

fork() && fork() || fork(); is evaluated some thing like this below: 评估如下所示:

       fork()
      /      \
    0/        \>0
 || fork()     && fork()
     /\            /   \
    /  \         0/     \>0
   *    *     || fork()  *
                /   \
               *     *

I need same kind of tree for fork() || fork() && fork() 我需要fork() || fork() && fork()同一种树 fork() || fork() && fork() . fork() || fork() && fork() Can anyone tell me how I should achieve it? 任何人都可以告诉我应该如何实现它?

If this is an interview question (likely) or homework (slightly less likely), you first calmly explain how you could evaluate it. 如果这是一个面试问题(可能)或作业(可能性略低),你先冷静地解释你如何进行评估。

By that I mean, you state that the first, second and fifth forks are unconditional but the third and fourth depend on the second and third (and that these second and third will be happening in multiple processes). 我的意思是,你说第一,第二和第五个叉子是无条件的,但第三个和第四个叉子取决于第二个和第三个(并且这些第二个和第三个将在多个过程中发生)。

That will show that you understand the concepts. 将表明您理解这些概念。

Then you explain that it doesn't really matter because, if any coder actually handed you code like that, there'd be some serious tar'n'feather action going on. 然后你解释它并不重要,因为如果有任何编码器实际上交给你这样的代码,就会有一些严重的tar'n'feather动作正在进行中。 Code like this monstrosity has no place in the real world and, although it's good for testing your understanding, you'll never encounter it in reality. 像这种怪物的代码在现实世界中没有地位,虽然它有助于测试你的理解,但你永远不会在现实中遇到它。

|| has lower precedence than && , and those operators short-circuit after evaluation of the first operand if it contains sufficient data ( || short-circuits if first operand is true and && short-circuits if first operand is false). 优先级低于&& ,并且这些运算符在评估第一个操作数后如果包含足够的数据则短路(如果第一个操作数为真,则为||如果第一个操作数为假,则为&&短路)。

fork()||fork()&&fork() is equivalent to fork() || ( fork() && fork() ) fork()||fork()&&fork()等效于fork() || ( fork() && fork() ) fork() || ( fork() && fork() )

Therefore: 因此:

                                                       fork()
                                                     0/     \>0
                                                     /       *  ==> || short-circuits, no evaluation of  fork()&&fork()
                                                   fork()
                                                   0/    \>0
 && short-circuits, no evaluation of &&fork() ==>  *     fork()
                                                         /   \
                                                        *     *

For you first example it was equivalent to ( fork() && fork() ) || fork() 对于你的第一个例子,它等同于( fork() && fork() ) || fork() ( fork() && fork() ) || fork() . ( fork() && fork() ) || fork()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM