【发布时间】:2015-04-09 14:51:31
【问题描述】:
这里我遇到了关于管道的问题。
如果我像这样在父管道中写入管道并从子管道中读取:
if(pid == 0){
char str1[100];
close(mypipe[1]);
read(mypipe[0], str1, 6);
close(mypipe[0]);
exit(0);
}
else{
while(wait(&state) != pid);
char str[] = "hello!";
close(mypipe[0]);
write(mypipe[1], str, strlen(str)+1);
close(mypipe[1]);
printf("pipe: %s\n", str);
}
然后我可以得到打印“你好!”。
但是如果我像这样在孩子中写作并在父母中阅读:
if(pid == 0){
char str[] = "hello!";
close(mypipe[0]);
write(mypipe[1], str, strlen(str)+1);
close(mypipe[1]);
exit(0);
}
else{
while(wait(&state) != pid);
char str1[100];
close(mypipe[1]);
read(mypipe[0], str1, 6);
close(mypipe[0]);
printf("pipe: %s\n", str);
}
然后它什么也不打印。
我真的不知道为什么......
【问题讨论】:
-
在第一个示例中,读取端没有打印任何内容,因此您无法知道读取端是否真的收到了一些东西。您的 ptintf 在写入端。
-
另外,第二个例子包含一个错误:
stris not defined in this scope, butstr1