我该如何评价这些fork()的使用和放大器调用;&安培;和||在C运算? [英] How do I evaluate these fork() calls with the && and || operators in C?

查看:140
本文介绍了我该如何评价这些fork()的使用和放大器调用;&安培;和||在C运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我有一些C code,它使用叉()系统调用,但我只是困惑:我怎么去结果
解决它做什么:

I have below some code in C which uses the fork() system call, but I am just confused: how do I go about
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(). Can anyone tell me how I should achieve it?

推荐答案

|| 具有较低的precedence比&放大器;&安培; ,而这些运营商短路第一个操作数的评估后,如果它含有足够的数据( || 短路,如果第一个操作数是真实和&放大器;&安培; 短路,如果第一个操作数为false)

|| 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()
                                                     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()的使用和放大器调用;&安培;和||在C运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆