在if语句/管理流程中使用fork [英] using fork in an if-statement/ managing processes

查看:120
本文介绍了在if语句/管理流程中使用fork的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

   printf("L1 ");

   if(fork() != 0) {
      printf("L2 ");

      if(fork() != 0) {

         printf("L3 ");
         fork();
      }
   }
printf("End \n");

作为练习,我试图找出一些有效/无效输出的示例,这些示例将由于运行此代码(而没有实际运行)而产生.

As an exercise I am trying to figure out some examples of valid/ invalid output that would result from running this code (without actually running it).

我仍然对fork()方法在if语句中的工作方式感到困惑.我知道一旦调用它就会返回两次,表明它已经创建了两个进程.因此,如果我做了类似的事情,

I am still a bit confused with how the fork() method works exactly within an if-statement. I know that once it is called it returns twice, indicating that it has created two processes.So that if I did something like,

printf("L1 ");
fork();
printf("L2 ");

我会得到L1 L2 L2

但是我仍然对它在第一行代码中的语句中的工作方式感到困惑.

But I am still shaky on how it works in in statements like in the first bit of code.

我认为这是一些有效/无效的输出:

Here is what I think are a few valid/ invalid outputs:

Valid: L1 L1 L2
       L1 L2 L3
       L1 L2 L1

Invalid: (Anything hat doesn't start with L1)
      L1 L2 L2
      L1 L3 L2

这些有意义吗?有没有一种简单的方法来解释if语句中发生的事情,这样我就可以大致了解fork()在其中的工作方式?这些输出值正确/不正确吗?

Doe these make sense? Is there an simple way to explain sort of what is going on in the if-statements so that I can get a general idea as to how the fork() works in them? And are these output values correct/ incorrect?

推荐答案

如果fork()调用全部失败,则只有一个进程,因此它将打印:

If the fork() calls all fail, there'll only be one process, so it will print:

L1 L2 L3 End

如果每个fork()调用均成功,则:

If every fork() call succeeds, then:

  1. 假定行缓冲或完全缓冲输出,
  2. 将有4个打印输出,但是它们的顺序是不确定的,并且可能(但可能不会)在输出上交错.
  3. 每个输出将以L1开始,以End结尾.
  4. fork()返回非零值时,它是父进程.
  5. 第一个if在第一个子代中失败,因此它打印L1 End.
  6. 第一个if传入父级,随后的输出将包括L2.
  7. 第二个if在第二个子节点中失败,因此它显示L1 L2 End.
  8. 第二个if传入父级,随后的输出将包括L3.
  9. 第三个fork()创建两个进程(或创建一个并在父进程中继续),两个进程均打印L1 L2 L3 End.
  1. Assuming line buffered or fully buffered output,
  2. There will be 4 lots of print out, but their sequence is indeterminate, and could be (but probably won't be) interleaved on the output.
  3. Each output will start with L1 and end with End.
  4. When fork() returns a non-zero value, it is the parent process.
  5. The first if fails in the first child, so it prints L1 End.
  6. The first if passes in the parent, and the subsequent outputs will then include L2.
  7. The second if fails in the second child, so it prints L1 L2 End.
  8. The second if passes in the parent, and the subsequent outputs will then include L3.
  9. The third fork() creates two processes (or creates one and continues in the parent), both of which print L1 L2 L3 End.

因此,输出将包括:

L1 End
L1 L2 End
L1 L2 L3 End
L1 L2 L3 End

但不能保证顺序.

发布上述分析后,我通过运行对其进行了检查,并生成了第一个示例运行:

After posting the analysis above, I checked it by running it, and the first sample run produced:

L1 End 
L1 L2 L3 End 
L1 L2 End 
L1 L2 L3 End 


请注意,如果输出是非缓冲的,则输出是不同的. L1将出现一次; L2将出现一次; L3将出现一次;和End将出现四次.


Note that if the output is non-buffered, then the output is different. L1 will appear once; L2 will appear once; L3 will appear once; and End will appear four times.

一个可能的顺序是:

L1 L2 End 
L3 End 
End 
End 

另一个观察到的序列(两次运行中的第二次)是:

Another observed sequence (second of two runs) was:

L1 L2 L3 End 
End End 

End 

这篇关于在if语句/管理流程中使用fork的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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