[英]Why the son process “read” function don't return (I think it will return -1), and the son process 'printf' function does not work
为什么子进程“读取”功能不返回(我认为它将返回-1),并且子进程“ printf”功能不起作用。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main() {
int fd[2];
pipe(fd);
pid_t pid = fork();
if(pid == 0) {
char buf[128] = {0};
int ret = read(fd[0], buf, sizeof buf);
printf("Son ret is %d\n", ret);
write(STDOUT_FILENO, buf, ret);
}
else if(pid > 0){
char buf[128] = "hello\n\0";
write(fd[1], buf, sizeof buf);
memset(buf, 0, sizeof buf);
int ret = read(fd[0], buf, sizeof buf);
printf("Dad ret is %d\n", ret);
write(STDOUT_FILENO, buf, ret);
}
return 0;
}
close(fd[1])
。 \\0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.