问题描述
int main()
{
fork();
fork() && fork() || fork();
fork();
printf("forked\n");
return 0;
}
当我们调用 fork 函数时,父进程得到一个 非零 pid 而子进程得到一个 0 作为回报基于这个逻辑,在第二个语句中,我们将不得不应用短路(afaik)的原则....第一次调用后会有2个进程,
When we call fork function , the parent gets a non-zero pid while the child gets a 0 in returnBased on this Logic, in the second statement we will have to apply the principle of shortcircuiting(afaik)....After the 1st call there will be 2 process,
在第二行之后,有 8 个进程,[父进程在 (fork() && fork()) 中分叉了两次,第二个子进程也因"fork()||fork()"]
After the second line, 8 processes,[A parent gets forked two times in (fork() && fork()), and the second child also gets twice due to the "fork()||fork()"]
最后是 16(根据我的推理)
And finally 16(According to my reasoning)
请告诉我这是正确的还是涉及其他逻辑
Please let me know if this is correct or some other logic is involved
推荐答案
要计算 (fork() && fork() || fork()
) 之后的进程数,只需请记住:
To calculate the number of process after (fork() && fork() || fork()
), just remember that :
In (
&&
) 逻辑运算符:右侧仅在左侧为非零时计算
In (
&&
) logical operator : the right side is evaluated only when the left side is NON ZERO
In (||
) 逻辑运算符:右侧仅在左侧为零
In (||
) logical operator : the right side is evaluated only when the left side is ZERO
使用运算符优先级,我们可以这样写:
And with operators precedence we wan write this like so:
(fork() && fork()) ||fork()
还要记住 fork 返回 NON ZERO 给 parent 和 ZERO 给 child
Remember also that fork return NON ZERO to the parent and ZERO to the child
为了简化说明,我们重命名:
To simplify the explanation, we rename :
fork() &&fork()
操作 A
和最后的 fork() 操作 B,所以前一行相当于:
and the last fork() Operation B, so the precedent line is equivalent to:
(fork() && fork()) ||fork()
=> A ||乙
第一行(fork
):
---> 2 个进程(Father 和 Child1)
第二行:
- 操作:
第一个叉 =>
父亲会给孩子=> 父亲(Child2的PID)和Child2(零)Child1 将给一个孩子 => Child1(Child3 的 PID) 和 Child3(ZERO)
Father will give a child => Father(PID of Child2) and Child2(ZERO)Child1 will give a child => Child1(PID of Child3) and Child3(ZERO)
我们有4个进程:Father(Child2的PID)、Child2(零)、Child1(Child3的PID)和Child3(零)
(&& fork()
) 将仅对返回非零 => Father 和 Child1 的最后操作执行
The (&& fork()
) will be executed only for last operations that returns NON ZERO => Father and Child1
父亲愿意给孩子=> 父亲(Child4的PID)和Child4(零)Child1 将给一个孩子 => Child1(Child5 的 PID) 和 Child5(ZERO)
Father wille give a child => Father(PID of Child4) and Child4(ZERO)Child1 will give a child => Child1(PID of Child5) and Child5(ZERO)
总结一下:
我们有6个流程:
父亲(Child4 的PID)、Child4(零)、Child1(Child5 的PID)、Child5(零)、Child2(零)和Child3(零)
- B 操作:
只对最后一个返回零的命令执行 => 从一个操作中返回零的进程,关注的进程是:
Is only executed for last commands that returns ZERO => process that returns ZERO from A Operation, Concerned Process are:
Child4(零)、Child5(零)、Child2(零)和 Child3(零)
当分叉这 4 个进程时,我们以 4 个新进程结束 => 第二行之后的进程总数 = 10
When forking this 4 process we end with 4 new process => Total number of process after second line = 10
第三行:这只是一个简单的叉子
Third Line :It's just a simple fork
=> 进程总数= 20
为了证明:使用这个 (fork_quiz.c
)
To demonstrate that: use this (fork_quiz.c
)
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv)
{
fork();
fork() && fork() || fork();
fork();
sleep(10);
return 0;
}
并编译它:
gcc -Wall fork_quiz.c -o fork_quiz
然后像这样运行:
toc@UnixServer:~$ ./fork_quiz & (sleep 1; ps -o "%P%p%c")
[1] 15455
PPID PID COMMAND
15046 15047 bash
15047 15455 fork_quiz
15047 15456 bash
15455 15457 fork_quiz
15455 15458 fork_quiz
15455 15459 fork_quiz
15455 15460 fork_quiz
15457 15462 fork_quiz
15457 15463 fork_quiz
15457 15464 fork_quiz
15458 15465 fork_quiz
15458 15466 fork_quiz
15459 15467 fork_quiz
15459 15468 fork_quiz
15465 15469 fork_quiz
15467 15470 fork_quiz
15463 15471 fork_quiz
15463 15472 fork_quiz
15462 15473 fork_quiz
15462 15474 fork_quiz
15473 15475 fork_quiz
15471 15476 fork_quiz
15456 15477 ps
这篇关于在以下程序中将产生多少进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!