命名管道创建
我正在尝试使用命名管道。我有一个读取信息的进程和另一个将信息写入管道的进程。
这是我的阅读器进程的简化代码:
main (int argc, char *argv[]) {
int fd, mkn;
char message[100];
if(unlink("aPipe") == -1) {
perror("Error unlinking:");
}
if((mkn = mknod("aPipe", S_IFIFO, 0)) < 0){
perror("Error mknod:");
}
if(chmod("aPipe", 0660)) {
perror("Error chmod:");
}
if(fd = open("aPipe", O_RDONLY) < 0) {
perror("Error abriendo el PIPE");
}
printf("going to read..\n");
close(fd);
}
但它永远卡在这一行: if(fd = open("aPipe", O_RDONLY) <0)
,我真的不明白为什么。
如果您知道哪个手册页说明了这里发生的情况,请告诉我:)
I'm trying to use named pipes. I have a process which reads info and another which writes info into the pipe.
This is the reduced code of my reader process:
main (int argc, char *argv[]) {
int fd, mkn;
char message[100];
if(unlink("aPipe") == -1) {
perror("Error unlinking:");
}
if((mkn = mknod("aPipe", S_IFIFO, 0)) < 0){
perror("Error mknod:");
}
if(chmod("aPipe", 0660)) {
perror("Error chmod:");
}
if(fd = open("aPipe", O_RDONLY) < 0) {
perror("Error abriendo el PIPE");
}
printf("going to read..\n");
close(fd);
}
but it gets stuck in this line: if(fd = open("aPipe", O_RDONLY) < 0)
forever, and I really dont understand why.
If you know which man page says what is happening here, please tell me :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
FIFO 有点奇怪;
open()
作为写入者将阻塞,直到有读取者为止,反之亦然。更糟糕的是,就像真正的管道一样,当写入器关闭其末端时,读取端将永远返回 EOF;您必须关闭并重新打开(阻止下一个读者)。或者您open(fifo, O_RDWR)
,然后您需要某种方法来知道编写器何时完成,例如让它仅使用单行或具有带内 EOF 数据包。FIFOs are a bit strange;
open()
as a writer will block until there's a reader, and vice versa. Worse, just like a real pipe, when the writer closes its end the read end will return EOF forever; you have to close and reopen (blocking for the next reader). Or youopen(fifo, O_RDWR)
and then you need some way to know when the writer is done such as having it use only a single line or having an in-band EOF packet.代码如下:
读者:
}
作者:
我也必须学习makefifo,但是在我理解了这一点之后。
非常感谢您的宝贵帮助!
Here is the code:
Reader:
}
The writer:
I have to learn makefifo too, but after I understand this.
Thank you very much for your valuable help!
是否有进程写入 FIFO?
如果不是,这是预期的行为,因为您在阻塞模式下打开了 FIFO RDONLY,当前进程将不会继续,直到有进程实际写入 FIFO。
Is there any process writing to the FIFO?
If no, this is the expected behaviour since you opened the FIFO RDONLY in blocking mode, the current process will not proceed until there is a process actually write to the FIFO.