多叉声明()■ [英] Statement with multiple fork() s

查看:190
本文介绍了多叉声明()■的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

子进程开始,其中最后一个离开的确切地点执行 - 叉后声明。如果什么语句包含多个fork()的就像条件前pression如下所示。究竟哪里开始执行程序的子进程。担心多少进程被​​创建之前,我想知道如果每个孩子进程创建尝试评估叉()及和放大器;叉()||叉(); 语句。如果是这样的。这是如何得到,因为第二叉()语句创建一个子进程已经从第一fork()的信息来评估叉()及和放大器;叉()

 的main(){
叉()及&放大器;叉()||叉子();
}


解决方案

这是从第二叉结果子()所知道的第一个<$ C的结果$ C>叉(),因为它是一个的精确副本的父进程的。

您可以制定出画一个小树自己会发生什么。开始第一个叉:

 叉()
           / \\
          / \\
父 - / \\ - child1

父回来了 child1 进程的PID,而 child1 回来0,所以我们有一些这样的:

  PID(child1)及和放大器;叉()||叉子()

在父母和:

  0安培;&安培;叉()||叉子()

在这个孩子。短路意味着中间叉()原来前pression的是不能为孩子只有在父执行。所以,现在发生了什么树?

 叉()
                    / \\
                   / \\
         父 - / \\ - child1
         叉子()
           / \\
          / \\
父 - / \\ - 的child2

是原工艺,并获得的child2 的PID。 的child2 ,就像 child1 0获得我们做前pressions是什么样子呢?

 父:PID(child1)及和放大器; PID(的child2)||叉()= 1 ||叉子()
孩子:0 ||叉子()
的child2:PID(child1)及&放大器; 0 ||叉()= 0 ||叉子()

现在,再由短路,做,并且不执行最后叉()。无论孩子的child2 有,但是。这使得我们与下面的树:

 叉()
                       / \\
                      / \\
            父 - / \\ - child1
            fork()的fork()的
              / \\ / \\
             / \\ / \\
            / \\ child1 - / \\ - child1-1
           / \\
          / \\
父 - / \\ - 的child2
                        叉子()
                          / \\
                         / \\
               的child2 - / \\ - child2-1

就是这样。 child1 的child2 分别获得各自孩子的PID,以及 child1-1 child2-1 每次回到0代中的这些值,最终的前pressions是:

 父:1
child1:0 || PID(child1-1)= 1
的child2:0 || PID(child2-1)= 1
child1-1:0 || 0 = 0
child2-1:0 || 0 = 0

这就是它 - 他们都退出

The child process begins executing at the exact point where the last one left off - after the fork statement. What if the statement contains multiple fork()s like the conditional expression like the following. Where exactly program execution starts for the child process. Before worrying about how many processes gets created, I wanted to know if every child process created tries to evaluate fork() && fork() || fork(); statement. If it is so. How does a child process that got created because of second fork() statement have information from the first fork() to evaluate fork() && fork().

main(){
fork() && fork() || fork();
}

解决方案

The child that results from the second fork() knows about the results of the first fork() because it is an exact copy of the parent process.

You can work out what happens by drawing a little tree for yourself. Start with the first fork:

         fork()
           /\
          /  \
parent --/    \-- child1

The parent gets back the PID of the child1 process, and child1 gets back 0. So we have something like:

PID(child1) && fork() || fork()

in the parent, and:

0 && fork() || fork()

in the child. Short circuiting means that the middle fork() of the original expression doesn't get executed in the child, only in the parent. So now what happens to the tree?

                  fork()
                    /\
                   /  \
         parent --/    \-- child1
         fork()
           /\
          /  \
parent --/    \-- child2

parent is the original process, and gets the PID of child2. child2, just like child1, gets 0. What do our expressions look like now?

parent:  PID(child1) && PID(child2) || fork() = 1 || fork()
child:   0 || fork()
child2:  PID(child1) && 0 || fork() = 0 || fork()

Now, again by short-circuiting, parent is done, and doesn't execute the last fork(). Both child and child2 have to, however. That leaves us with the following tree:

                     fork()
                       /\
                      /  \
            parent --/    \-- child1
            fork()            fork()
              /\                /\
             /  \              /  \
            /    \   child1 --/    \-- child1-1
           /      \
          /        \
parent --/          \-- child2
                        fork()
                          /\
                         /  \
               child2 --/    \-- child2-1

And that's it. child1 and child2 each get the PID of their respective children, and child1-1 and child2-1 each get back 0. Substituting those values in, the final expressions are:

parent:   1
child1:   0 || PID(child1-1) = 1
child2:   0 || PID(child2-1) = 1
child1-1: 0 || 0 = 0
child2-1: 0 || 0 = 0

And that's it - they all exit.

这篇关于多叉声明()■的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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