【发布时间】:2011-08-01 23:54:57
【问题描述】:
来自http://pubs.opengroup.org/onlinepubs/009604599/functions/pipe.html:
pipe() 函数将创建一个管道并放置两个文件 描述符,每个进入参数 fildes[0] 和 fildes[1],即 请参阅打开的文件描述的读写端 管道。
有一个例子,父级向其子级写入数据:
int fildes[2];
const int BSIZE = 100;
char buf[BSIZE];
ssize_t nbytes;
int status;
status = pipe(fildes);
if (status == -1 ) {
/* an error occurred */
...
}
switch (fork()) {
case -1: /* Handle error */
break;
case 0: /* Child - reads from pipe */
close(fildes[1]); /* Write end is unused */
nbytes = read(fildes[0], buf, BSIZE); /* Get data from pipe */
/* At this point, a further read would see end of file ... */
close(fildes[0]); /* Finished with pipe */
exit(EXIT_SUCCESS);
default: /* Parent - writes to pipe */
close(fildes[0]); /* Read end is unused */
write(fildes[1], "Hello world\n", 12); /* Write data on pipe */
close(fildes[1]); /* Child will see EOF */
exit(EXIT_SUCCESS);
}
我想知道使用管道与其孩子进行通信的父母如何确保在父母运行 write 之前孩子不会运行 read(),而孩子 在家长写完之前读完?
-
我想知道在父级中创建的管道是否可以用于 父母和孩子之间的双向沟通,或者只是一种方式 父母对孩子不是相反吗?
如果孩子可以通过管道向父母发送数据,那么 程序长什么样?
-
是一个像真正的管道一样有两端的管道。 fildes[0] 和 fildes[1] 分别用来表示两端?
如果管道是双向的,读端和写端是什么意思 关于,即哪个读(写),父母还是孩子?
感谢和问候!
【问题讨论】: