简体   繁体   English

命名为 pipe 创建

[英]Named pipe creation

I'm trying to use named pipes.我正在尝试使用命名管道。 I have a process which reads info and another which writes info into the pipe.我有一个读取信息的进程和另一个将信息写入 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(fd = open("aPipe", O_RDONLY) < 0)永远,我真的不明白为什么。

If you know which man page says what is happening here, please tell me:)如果您知道哪个手册页说明了这里发生的事情,请告诉我:)

FIFOs are a bit strange; FIFO 有点奇怪。 open() as a writer will block until there's a reader, and vice versa. open()作为作家将阻塞,直到有读者,反之亦然。 Worse, just like a real pipe, when the writer closes its end the read end will return EOF forever;更糟糕的是,就像真正的 pipe 一样,当写入器关闭其末尾时,读取端将永远返回 EOF; 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.或者你open(fifo, O_RDWR)然后你需要一些方法来知道编写器何时完成,例如让它只使用一条线或有一个带内 EOF 数据包。

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.我也必须学习makefifo,但是在我理解了这一点之后。

Thank you very much for your valuable help!非常感谢您的宝贵帮助!

Is there any process writing to the FIFO?是否有任何进程写入 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.如果不是,这是预期的行为,因为您在阻塞模式下打开了 FIFO RDONLY,当前进程将不会继续,直到有进程实际写入 FIFO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM