简体   繁体   English

双向FIFO

[英]Bidirectional FIFO

I would like to implement a bidirectional fifo. 我想实现双向fifo。 The code below is functioning but it is not using bidirectional fifo. 下面的代码正在运行,但它没有使用双向fifo。 I have searched all over the internet, but haven't found any good example... 我在互联网上搜索过,但没有找到任何好的例子......

How can I do that? 我怎样才能做到这一点?

Thanks, 谢谢,

WRITER.c: WRITER.c:

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/wait.h>
#include <fcntl.h>



#define MAXLINE 4096

#define READ 0

#define WRITE 1


int main (int argc, char** argv)
{
 int a, b, fd;

 do {
   fd=open("/tmp/myfifo",O_WRONLY);
   if (fd==-1) sleep(1);
  } while (fd==-1);

  while (1) {
   scanf("%d", &a);
   scanf("%d", &b);

   write(fd,&a,sizeof(int));
   write(fd,&b,sizeof(int));

   if (a == 0 && b == 0)
   {
    break;
   }

  }

  close(fd);
  return 0;
}

READER.c: READER.c:

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>




#define MAXLINE 4096

#define READ 0

#define WRITE 1



int main(void)

{
  int n1, n2;
  int fd;

  mkfifo("/tmp/myfifo",0660);
  fd=open("/tmp/myfifo",O_RDONLY);

  while(read(fd, &n1, sizeof(int) ))
  {
 read(fd, &n2, sizeof(int));

 if (n1 == 0 && n2 == 0)
 {
  break;
 }

 printf("soma: %d\n",n1+n2);

  printf("diferenca: %d\n", n1-n2);

   printf("divisao: %f\n", n1/(double)n2);

   printf("multiplicacao: %d\n", n1*n2); 
  }

  close(fd);

  return 0;
}

FIFOs tend to be one way. FIFO往往是单向的。 If you want a FIFO you can both read from and write to, chances are what you really want is either a pair of FIFOs (one in each direction) or a UNIX socket. 如果你想要一个FIFO,你可以读取和写入,你真正想要的可能是一对FIFO(每个方向一个)或UNIX套接字。

FIFOs (also known as named pipe) provide a unidirectional interprocess communication channel. FIFO(也称为命名管道)提供单向进程间通信通道。 A FIFO has a read end and a write end. FIFO具有读端和写端。 Data written to the write end of a FIFO can be read from the read end of the FIFO. 写入FIFO写入端的数据可以从FIFO的读端读取。 Because they are unidirectional, a pair of FIFOs is required for bi-directional communication. 因为它们是单向的,所以双向通信需要一对FIFO。

As cHao suggested , another option would be to use a Unix socket. 正如cHao建议的那样 ,另一种选择是使用Unix套接字。 Unix domain sockets take a bit more overhead to setup (socket creation, initialization and connection) than a FIFO, but are more flexible and offer bidirectional communication. Unix域套接字比FIFO设置(套接字创建,初始化和连接)需要更多的开销,但更灵活并提供双向通信。

Another option is to use a psudo-terminal (ptty). 另一种选择是使用psudo-terminal(ptty)。 You can also use TCP sockets, which have higher overhead than UNIX sockets, but would work. 您也可以使用TCP套接字,它比UNIX套接字具有更高的开销,但可以使用。

Bidirectional pipes are often discouraged because of the possibility of deadlock (prog1 is waiting on data from prog2 which is waiting on data from prog1 which is waiting on data from prog2 ...), but this can occur with any of the workarounds as well, and can occur with commonly used protocols like SMTP (simple mail transport protocol) as each side plays a role in a conversation. 通常不鼓励双向管道,因为可能会出现死锁(prog1正在等待来自prog2的数据,等待来自prog1的数据,等待来自prog2的数据...),但这也可能发生在任何变通方法中,并且可以使用常见的协议,如SMTP(简单邮件传输协议),因为每一方都在对话中发挥作用。

If you think that a deadlock could occur you may want to make at least one side has a timeout, which you can do by one of the poll functions (including poll, select, pselect, and epoll_*) or by arranging for an SIGALM to be delivered (with alarm or a few other functions which allow for shorter times and more control) so that your program can get kicked out of deadlock. 如果您认为可能发生死锁,您可能希望至少有一方有超时,您可以通过其中一个轮询功能(包括poll,select,pselect和epoll_ *)或通过安排SIGALM来执行交付(带有警报或一些其他功能,允许更短的时间和更多的控制),以便您的程序可以摆脱僵局。

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

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