如果我使用wait(null)
,并且我确定子进程在我们到达父进程中的wait(null)
之前完成(退出),那么wait(null)
会阻塞父进程吗?
我的意思是,wait()
不会收到任何信号,对吧?
int main() {
int pipe_descs[2];
int i, n, p;
srand(time(NULL(;
pipe(pipe_descs);
for (i = 0; i < 2; i++) {
pid_t status = fork();
if (status == 0) {
n = rand() % 100;
p = (int) getpid();
write(pipe_descs[1], &n, sizeof(int));
write(pipe_descs[1], &p, sizeof(int));
exit(0);
}
else {
read(pipe_descs[0], &n, sizeof(int));
read(pipe_descs[0], &p, sizeof(int));
printf(" %d %d\n", n, p);
wait(NULL); // (1)
}
}
return 0;
}
SIGCHLD
。特别地,在您的示例代码中,不能安全地假设总是会发生这种情况。 - John Bollingernull
? - too honest for this site