多少进程都在这里分叉 [英] How many processes are forked here

查看:110
本文介绍了多少进程都在这里分叉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,它可能会作为一个愚蠢的问题,但任何人都可以帮助我了解以下code片段的行为

I know that it might come as a stupid question but could anyone help me understand the behavior of the following code snippet

//label 0
int main(){
  fork();//label 1
  fork();//label 2
  fork();//label 3
  return 0;
}

据我了解,进程树是这样的

As far as I understand, the process tree goes like this

              [0]
          /    |    \
        [1]   [2]   [3]
       /  \    |
      [2] [3] [3]
       |
      [3]

我说得对不对?
在这样的情况下,我很困惑,为什么第二叉不产卵对应标签1叉和第三叉没有任何进一步派生的任何进程的过程。我的意思是子进程是父(至少在code)项的精确副本,因此必须全部执行其父的code。任何人都可以请帮我这混乱......

Am I right? In case so, I am confused as to why the second fork doesn't spawn a process corresponding to label 1 fork and the third fork doesn't spawn any process any further. I mean a child process is the exact copy of the parent (at least in code) so it must execute the code of its parent in entirety. Can anyone please help me with this confusion...

推荐答案

是的,孩子是其父的副本。它继承了执行状态也包括的其中的在code母公司正在执行。当从第一叉()与PID父返回(> 0)的儿童,其第一子返回0,然后继续到第二和第三叉。孩子不回去的main()的顶部,它只是在创建它后叉下去。

Yes, the child is a copy of its parent. It inherits execution state too, including where in the code the parent was executing. When the parent returns from the first fork() with the PID (>0) of the child, its first child returns with 0 and then continues on to the second and third forks. The child doesn't go back to the top of main(), it just goes on from after the fork that created it.

修改重新字回应置评。请参阅叉所有返回值的含义(2)手册页

EDIT Re-word in response to comment. See the fork(2) man page for the meaning of all return values.

这篇关于多少进程都在这里分叉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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