命名管道创建

发布于 2024-11-14 12:40:53 字数 633 浏览 7 评论 0原文

我正在尝试使用命名管道。我有一个读取信息的进程和另一个将信息写入管道的进程。

这是我的阅读器进程的简化代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼忆七猫命九 2024-11-21 12:40:53

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 you open(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.

挽你眉间 2024-11-21 12:40:53

代码如下:

读者:

#include <stdio.h>
#include <unistd.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

readline(int fd, char *str) {
int n;
do {
    n = read(fd, str, 1);
    if(n == -1){
        perror("Error reading:");
    }
}
while(n > 0 && (str++) != NULL);

return(n > 0);

}

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");
while(readline(fd,message))
    printf("%s\n", message);
close(fd);
}

作者:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

main (int argc, char *argv[]) {
int fd, messagelen,i;
char message[100];

sprintf(message, "Hello from PID %d", getpid());

messagelen = strlen(message) + 1;
do {
    fd = open("aPipe", O_WRONLY|O_NDELAY);
    if (fd == -1) {
        perror("opening aPipe:");
        sleep(1);
    }
}
while(fd == -1);

for (i = 1; i < 4; i++) {
    if(write(fd, message, messagelen) == -1) {
        perror("Error writing:");
    }
    sleep(3);
}
close(fd);
} 

我也必须学习makefifo,但是在我理解了这一点之后。

非常感谢您的宝贵帮助!

Here is the code:

Reader:

#include <stdio.h>
#include <unistd.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

readline(int fd, char *str) {
int n;
do {
    n = read(fd, str, 1);
    if(n == -1){
        perror("Error reading:");
    }
}
while(n > 0 && (str++) != NULL);

return(n > 0);

}

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");
while(readline(fd,message))
    printf("%s\n", message);
close(fd);
}

The writer:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

main (int argc, char *argv[]) {
int fd, messagelen,i;
char message[100];

sprintf(message, "Hello from PID %d", getpid());

messagelen = strlen(message) + 1;
do {
    fd = open("aPipe", O_WRONLY|O_NDELAY);
    if (fd == -1) {
        perror("opening aPipe:");
        sleep(1);
    }
}
while(fd == -1);

for (i = 1; i < 4; i++) {
    if(write(fd, message, messagelen) == -1) {
        perror("Error writing:");
    }
    sleep(3);
}
close(fd);
} 

I have to learn makefifo too, but after I understand this.

Thank you very much for your valuable help!

不知在何时 2024-11-21 12:40:53

是否有进程写入 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文