FIFO:一个进程从不从管道读取

FIFO: One process never reads from pipe

我正在遵循 THIS TutorialsPoint 指南 Linux 管道,我特别需要使用 FIFO。

但是,代码对服务器端根本不起作用。

服务器文件要么无限期挂起,要么不读取任何内容,而客户端则写入 FIFO 并立即读取刚刚写入的文件。

如果您不想浏览 TutorialsPoint,这里有两个文件的完整代码:

fifoserver_twoway.cpp

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

#define FIFO_FILE "/tmp/fifo_twoway"

void reverse_string(char *);
int main() {
   int fd;
   char readbuf[80];
   char end[10];
   int to_end;
   int read_bytes;

   /* Create the FIFO if it does not exist */
   mkfifo(FIFO_FILE, S_IFIFO|0640);
   strcpy(end, "end");
   fd = open(FIFO_FILE, O_RDWR);
   while(1) {
      read_bytes = read(fd, readbuf, sizeof(readbuf));
      readbuf[read_bytes] = '[=10=]';
      printf("FIFOSERVER: Received string: \"%s\" and length is %d\n", readbuf, (int)strlen(readbuf));
      to_end = strcmp(readbuf, end);

      if (to_end == 0) {
         close(fd);
         break;
      }
      reverse_string(readbuf);
      printf("FIFOSERVER: Sending Reversed String: \"%s\" and length is %d\n", readbuf, (int) strlen(readbuf));
      write(fd, readbuf, strlen(readbuf));
      /*
      sleep - This is to make sure other process reads this, otherwise this
      process would retrieve the message
      */
      sleep(2);
   }
   return 0;
}

void reverse_string(char *str) {
   int last, limit, first;
   char temp;
   last = strlen(str) - 1;
   limit = last/2;
   first = 0;

   while (first < last) {
      temp = str[first];
      str[first] = str[last];
      str[last] = temp;
      first++;
      last--;
   }
   return;
}

fifoclient_twoway.cpp

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

#define FIFO_FILE "/tmp/fifo_twoway"
int main() {
   int fd;
   int end_process;
   int stringlen;
   int read_bytes;
   char readbuf[80];
   char end_str[5];
   printf("FIFO_CLIENT: Send messages, infinitely, to end enter \"end\"\n");
   fd = open(FIFO_FILE, O_CREAT|O_RDWR);
   strcpy(end_str, "end");

   while (1) {
      printf("Enter string: ");
      fgets(readbuf, sizeof(readbuf), stdin);
      stringlen = strlen(readbuf);
      readbuf[stringlen - 1] = '[=11=]';
      end_process = strcmp(readbuf, end_str);

      //printf("end_process is %d\n", end_process);
      if (end_process != 0) {
         write(fd, readbuf, strlen(readbuf));
         printf("FIFOCLIENT: Sent string: \"%s\" and string length is %d\n", readbuf, (int)strlen(readbuf));
         read_bytes = read(fd, readbuf, sizeof(readbuf));
         readbuf[read_bytes] = '[=11=]';
         printf("FIFOCLIENT: Received string: \"%s\" and length is %d\n", readbuf, (int)strlen(readbuf));
      } else {
         write(fd, readbuf, strlen(readbuf));
         printf("FIFOCLIENT: Sent string: \"%s\" and string length is %d\n", readbuf, (int)strlen(readbuf));
         close(fd);
         break;
      }
   }
   return 0;
}

当我 运行 这两个过程时,这就是我得到的:

./fifoserver_twoway

FIFOSERVER: Received string: "" and length is 0
FIFOSERVER: Sending Reversed String: "" and length is 0
FIFOSERVER: Received string: "" and length is 0
FIFOSERVER: Sending Reversed String: "" and length is 0

./fifoclient_twoway

FIFOCLIENT: Sent string: "ciao" and string length is 4
FIFOCLIENT: Received string: "ciao" and length is 4
Enter string: why won't you reverse?
FIFOCLIENT: Sent string: "why won't you reverse?" and string length is 29
FIFOCLIENT: Received string: "why won't you reverse?" and length is 29

同样值得注意的是,在开始写这个问题之前,服务器的行为是完全不同的:不像你在这里看到的那样什么都没有接收并打印,它会在 "read" 之后无限期地挂起(我没有' 稍微更改了代码,但更改了 FIFO_FILE 路径)

您让服务器在写入后休眠 – 而不是客户端。这样,客户端 still 可能会在服务器获取它之前读回自己的输出。所以至少你应该在两次写入之后添加一个睡眠,让服务器休眠更长的时间以确保客户端首先醒来读取服务器输出。

同时访问未命名管道(通过 pipe functions 创建)的同一端是未定义的行为。虽然不确定命名管道,但我认为那里也差不多。通过简单的延迟(sleepusleep)同步对这些端点的并发访问也许可以解决问题,但这是一种非常不安全的方法。

我宁愿推荐两个单独的管道(正如 Tony Tannous 已经提议的那样),每个方向一个(根据需要打开相应的末​​端 RDONLYWRONLY),然后您获得全双工通信而不是半双工通信,并且您也不需要进一步同步(最简单变体中的延迟):

// server
int fd_cs = open(FIFO_FILE_CS, O_RDONLY);
int fd_sc = open(FIFO_FILE_SC, O_WRONLY);
    read(fd_cs, ...);
    write(fd_sc, ...);

// client
int fd_cs = open(FIFO_FILE_CS, O_WRONLY);
int fd_sc = open(FIFO_FILE_SC, O_RDONLY);
    write(fd_cs, ...);
    read(fd_sc, ...);