我写了这段代码实现子进程和父进程之间的pipe()通信。我需要确认一下这段代码是否正确。顺便说一句,它会给出应该看到的答案!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]){
pid_t pid;
int fd[2];
char buf[20];
pipe(fd);
switch(pid = fork()){
case -1:
perror("pipe");
exit(1);
case 0:
/*child process*/
close(fd[1]);
read(fd[0], buf, 20);
printf("Child read message from parent: %s\n", buf);
exit(1);
break;
default:
/*parent process*/
close(fd[0]);
write(fd[1], "Hello from parent\n", 17);
break;
}
return 0;
}