所以我在修改过程中看到了以下代码。我知道 wait() 会导致父级等待子级停止,但我对此有几个问题。
首先,当创建子级时,我的假设是否得到纠正,即父级继续,更改 x 值,然后在 if 语句之后等待?
其次,当子进程继续执行并进入 wait() 时,会发生什么?是否因为没有什么可等待而被忽略?
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main() {
int x = 1;
pid_t pid = fork();
if (pid == 0) {
x = x * 2;
} else if (pid > 0) {
x = 3;
}
wait();
// Print the value of x to the console
printf("%d\n",x);
}
最佳答案
您可以尝试在子执行开始时调用 wait()。一旦这个过程中没有子进程,调用就会被简单地忽略。
关于c - wait() 系统调用 - child 会忽略它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16115321/