admin管理员组文章数量:1122846
Why does the second piece of code does not create 7 processes just like the first code? Anywhere it says that fork()'s child process runs exactly from the same point as the parent. Shouldn't this mean that the second fork() in the if statement should run inside the child as well?
This piece of code outputs:
int a = fork(), b = fork(), c = fork();
if (!a || !b || !c) {
printf("msg: pid = %d\n", getpid());
}
return 0;
outputs:
msg: pid = 28740
msg: pid = 28737
msg: pid = 28738
msg: pid = 28742
msg: pid = 28739
msg: pid = 28741
msg: pid = 28743
while the following piece of code:
if (!fork() || !fork() || !fork()) {
printf("msg: pid = %d\n", getpid());
}
return 0;
outputs:
msg: pid = 29385
msg: pid = 29386
msg: pid = 29387
Why does the second piece of code does not create 7 processes just like the first code? Anywhere it says that fork()'s child process runs exactly from the same point as the parent. Shouldn't this mean that the second fork() in the if statement should run inside the child as well?
This piece of code outputs:
int a = fork(), b = fork(), c = fork();
if (!a || !b || !c) {
printf("msg: pid = %d\n", getpid());
}
return 0;
outputs:
msg: pid = 28740
msg: pid = 28737
msg: pid = 28738
msg: pid = 28742
msg: pid = 28739
msg: pid = 28741
msg: pid = 28743
while the following piece of code:
if (!fork() || !fork() || !fork()) {
printf("msg: pid = %d\n", getpid());
}
return 0;
outputs:
msg: pid = 29385
msg: pid = 29386
msg: pid = 29387
Share
Improve this question
edited Nov 23, 2024 at 10:23
chqrlie
144k12 gold badges130 silver badges207 bronze badges
asked Nov 22, 2024 at 18:05
mikerinomikerino
232 bronze badges
3
|
1 Answer
Reset to default 4In the parent process fork()
always evaluates to the PID of the child process. And in the child process it always evaluates to zero. As a PID cannot be 0, the return value of fork()
is always inherently truth-y in the parent process and always false-y in the child process.
When you couple fork()
with the logical not (!
) and logical or operator (||
), the net effect is that in the parent the !fork() || ...
statement always executes all three forks(), and does not enter the body of the if
as it is equivalent to (0 || 0 || 0)
. However, in the child, at the point that the child starts executing, the last executed !fork()
evaluates to a truth-y value, and so the logical or operator short circuits and stops executing the rest of the conditional statement, and instead enters the body of the if
statement. In the children, the conditional statement evaluates as thus:
- first child:
if (true || <last two forks not executed>)
- second child:
if (false || true || <last fork not executed>)
- third child:
if (false || false || true) <nothing left to execute in conditional>
- parent:
if (false || false || false) <if body not executed>
In the former example you give, all forks()
are executed unconditionally, and so seven descendent processes are created (not all of which are direct children of the parent).
本文标签: cWhy does if (fork()fork()fork()) behave differentlyStack Overflow
版权声明:本文标题:c - Why does if (fork() || fork() || fork()) behave differently? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301736a1931281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
printf("msg: pid = %d; parent = %d\n", getpid(), getppid());
... then draw the "hereditary tree" – pmg Commented Nov 22, 2024 at 18:14fork
has a godawful horrible API and it's to be avoided in any context save for immediately calling a parent/child function directly after it. Everything else is obfuscated code which I believe is off-topic here. – Lundin Commented Nov 26, 2024 at 15:21