【发布时间】:2013-11-06 11:49:57
【问题描述】:
我想使用管道实现子进程和它的父进程之间的通信。代码如下:
#include <stdio.h>
int main() {
int pipe_dsc[2];
if (pipe(pipe_dsc) == -1) {
printf("error\n");
}
switch (fork()) {
case -1:
printf("error\n");
return 0;
case 0: //children
close(0);
dup(pipe_dsc[0]);
close(pipe_dsc[1]);
int x;
scanf("%d", &x);
printf("succes: %d\n", x);
return 0;
default: //parent
close(1);
dup(pipe_dsc[1]);
close(pipe_dsc[0]);
printf("127");
wait(0);
return 0;
}
return 0;
}
父母应该写一个数字 127,孩子应该读它,但它没有。孩子一直在 scanf 等待。怎么了?
【问题讨论】:
-
您知道,“children”是复数形式。单数形式是“child”。