在视觉上会发生什么到餐桌()在for循环中 [英] Visually what happens to fork() in a For Loop

查看:108
本文介绍了在视觉上会发生什么到餐桌()在for循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图了解 叉() 的行为。这次在 for循环。遵守以下code:

I have been trying to understand fork() behavior. This time in a for-loop. Observe the following code:

#include <stdio.h>

void main()
{
   int i;

   for (i=0;i<3;i++)
   {
      fork();

      // This printf statement is for debugging purposes
      // getppid(): gets the parent process-id
      // getpid(): get child process-id

      printf("[%d] [%d] i=%d\n", getppid(), getpid(), i);
   }

   printf("[%d] [%d] hi\n", getppid(), getpid());
}

下面是输出:

[6909][6936] i=0
[6909][6936] i=1
[6936][6938] i=1
[6909][6936] i=2
[6909][6936] hi
[6936][6938] i=2
[6936][6938] hi
[6938][6940] i=2
[6938][6940] hi
[1][6937] i=0
[1][6939] i=2
[1][6939] hi
[1][6937] i=1
[6937][6941] i=1
[1][6937] i=2
[1][6937] hi
[6937][6941] i=2
[6937][6941] hi
[6937][6942] i=2
[6937][6942] hi
[1][6943] i=2
[1][6943] hi

我是一个非常直观的人,所以对我来说真正理解事物的唯一方法是通过图表。我的教练说会有8的 HI 的语句。我写跑了code,确实有8的 HI 的语句。但我真的不明白。所以我得出如下图:

I am a very visual person, and so the only way for me to truly understand things is by diagramming. My instructor said there would be 8 hi statements. I wrote and ran the code, and indeed there were 8 hi statements. But I really didn’t understand it. So I drew the following diagram:

图更新,以反映的意见:)


  1. 父进程(主)必须重复循环3次。然后printf的被称为

  2. 在父母的每个迭代的for循环叉子()被调用

  3. 每个fork()的调用后,我被增加,所以每一个孩子开始一个for循环,从我这递增之前

  4. 在每一个for循环结束后,喜打印


  • 是正确的我的图?

  • 为何会出现的两个的输出 I = 0 的实例?

  • 什么价值叉后,结转到每个孩子()?如果的相同值i 结转,那么什么时候了分叉停下来?

  • 是不是总是这样 2 ^ N - 1 将是一个方法来计算被分叉儿童的数量?所以,在这里 N = 3 ,这意味着 2 ^ 3 - 1 = 8 - 1 = 7 的孩子,这是是否正确?

Here are my questions:

  • Is my diagram correct?
  • Why are there two instances of i=0 in the output?
  • What value of i is carried over to each child after the fork()? If the same value of i is carried over, then when does the "forking" stop?
  • Is it always the case that 2^n - 1 would be a way to count the number of children that are forked? So, here n=3, which means 2^3 - 1 = 8 - 1 = 7 children, which is correct?
  • 推荐答案

    下面是如何理解它,开始于循环。

    Here's how to understand it, starting at the for loop.


    1. 循环开始在父母,我== 0

    叉() S,创建子1。

    您现在有两个进程。这两种印刷 I = 0

    You now have two processes. Both print i=0.

    循环重新开始在这两个进程,现在我== 1

    Loop restarts in both processes, now i == 1.

    父母子女1 叉(),创造儿童2和3。

    Parent and child 1 fork(), creating children 2 and 3.

    您现在有四个过程。所有四个打印 I = 1

    You now have four processes. All four print i=1.

    循环重新开始所有四个过程,现在我== 2

    Loop restarts in all four processes, now i == 2.

    3,通过家长和孩子都1 叉(),创造孩子4至7。

    Parent and children 1 through 3 all fork(), creating children 4 through 7.

    您现在有八道工序。所有八个印刷 I = 2

    You now have eight processes. All eight print i=2.

    循环重新开始所有八个流程,现在我== 3

    Loop restarts in all eight processes, now i == 3.

    循环终止所有八个流程, I&LT; 3 不再是正确的。

    Loop terminates in all eight processes, as i < 3 is no longer true.

    所有八道工序印刷

    所有八道工序终止。

    所以,你得到 0 印刷两次, 1 印刷四次, 2 印刷8次,印刷8次。

    So you get 0 printed two times, 1 printed four times, 2 printed 8 times, and hi printed 8 times.

    这篇关于在视觉上会发生什么到餐桌()在for循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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