学习《UNIX网络编程2 进程间通信》遇到一个管道间全双工通信的例子:
int main(int argc, char **argv) { int fd[2], n; char c; pid_t childpid; Pipe(fd); /* assumes a full-duplex pipe (e.g., svr4) */ if ( (childpid = Fork()) == 0) { /* child */ sleep(3); if ( (n = Read(fd[0], &c, 1)) != 1){ printf("child: read returned %d", n); exit(-1); } printf("child read %c\n", c); Write(fd[0], "c", 1); //子进程为什么没有退出?? exit(0); } /* 4parent */ Write(fd[1], "p", 1); if ( (n = Read(fd[1], &c, 1)) != 1){ printf("parent: read returned %d", n); exit(-1); } printf("parent read %c\n", c); exit(0); }
操作系统仅支持半双工,应在输出:
read error: Bad file descriptor
child read p
write error: Bad file descriptor
后退出。但是,此时子进程并没有退出(注:在包裹函数Write中有 exit(1)),不知道是什么原因?求解答?