c - 为什么这个程序打印 "forked!"4 次?

标签 c linux unix fork systems-programming

为什么这个程序打印“forked!” 4次?

#include <stdio.h>
#include <unistd.h>

int main(void) {

  fork() && (fork() || fork());

  printf("forked!\n");
  return 0;
}

最佳答案

一个来自main(),另外三个来自每个fork()

请注意所有三个 forks() 都将被执行。你可能想看看 ref :

RETURN VALUE

Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.

请注意,进程 ID 不能为零,如 here 所述.


那么到底发生了什么?

我们有:

fork() && (fork() || fork());

因此,第一个 fork() 将向父进程返回其非零进程 ID,同时将 0 返回给子进程。这意味着逻辑表达式的第一个分支将在父进程中被评估为真,而在子进程中它将被评估为假,并且由于 Short circuit evaluation。 , 它不会调用剩余的两个 fork()

所以,现在我们知道将至少获得两次打印(一次来自 main,一次来自第一个 fork())。

现在,父进程中的第 2 个 fork() 将被执行,它会执行并向父进程返回一个非零值,并在子进程中返回一个零值。

所以现在,父进程不会继续执行到最后一个fork()(因为短路),而子进程会执行最后一个fork,因为的第一个操作数|| 为 0。

这意味着我们将再打印两张。

结果,我们总共打印了四张。


短路

这里,短路 基本上意味着如果 && 的第一个操作数为零,则不评估其他操作数。同样的逻辑,如果 || 的操作数为 1,则其余操作数不需要评估。发生这种情况是因为其余的操作数无法改变逻辑表达式的结果,因此不需要执行它们,从而节省了时间。

请参阅下面的示例。


过程

请记住,父进程会创建后代进程,而后代进程又会创建其他进程,依此类推。这导致了进程的层次结构(或者可以说是一棵树)。

考虑到这一点,值得一看 similar problem ,以及 this回答。


描述性图片

我也制作了这个图,我想它可以提供帮助。我假设每次调用返回的 pid 的 fork() 都是 3、4 和 5。

fork nodes 请注意,某些 fork() 上面有一个红色的 X,这意味着它们由于逻辑表达式的短路评估而未被执行。

顶部的fork()不会被执行,因为运算符&&的第一个操作数是0,所以整个表达式会导致0,所以没有必要执行 && 的其余操作数。

底部的 fork() 不会被执行,因为它是 || 的第二个操作数,第一个操作数是一个非零数,因此无论第二个操作数是什么,表达式的结果都已经被评估为真。

在下一张图片中,您可以看到流程的层次结构: Process hierarchy 基于上图。


短路示例

#include <stdio.h>

int main(void) {

  if(printf("A printf() results in logic true\n"))
    ;//empty body

  if(0 && printf("Short circuiting will not let me execute\n"))
    ;
  else if(0 || printf("I have to be executed\n"))
    ;
  else if(1 || printf("No need for me to get executed\n"))
    ;
  else
  printf("The answer wasn't nonsense after all!\n");

  return 0;
}

输出:

A printf() results in logic true
I have to be executed

关于c - 为什么这个程序打印 "forked!"4 次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7091918/

相关文章:

Linux:信号源

unix - 如何为 mailx 命令指定 fmt 参数 'width'?

c - 在 SwiftUI 中,我如何创建一个将项目添加到 ScrollView 的函数?

c - 简单的 c 函数,无段错误

linux - 尽管包含符号,perf 也不在注释中显示源代码

ios - sysCall(Sys_gettid) 返回 -1

arrays - 空格在 UNIX 数组中被视为分隔符

c - 如何使用 scanf() 扫描其中包含空格的字符串?

c - 如何区分扩展头和上层头

linux - 当linux上安装了多个postgresql时,如何添加另一个postgres用户名?