防止FIFO关闭/重用已关闭的FIFO [英] Prevent FIFO from closing / reuse closed FIFO

查看:125
本文介绍了防止FIFO关闭/重用已关闭的FIFO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下情形:

创建一个名为test的FIFO.在一个终端窗口(A)中,我运行cat <test,在另一个终端窗口(B)中,运行cat >test.现在可以在窗口B中写入并在窗口A中获得输出.还可以终止进程A并重新启动它,并且仍然可以根据需要使用此设置.但是,如果您在窗口B中终止该进程,据我所知,B将通过FIFO发送一个EOF到进程A并终止它.

a FIFO named test is created. In one terminal window (A) I run cat <test and in another (B) cat >test. It is now possible to write in window B and get the output in window A. It is also possible to terminate the process A and relaunch it and still be able to use this setup as suspected. However if you terminate the process in window B, B will (as far as I know) send an EOF through the FIFO to process A and terminate that as well.

实际上,如果您运行的进程未在EOF上终止,则仍然无法使用重定向到该进程的FIFO.我认为是因为该FIFO被认为是关闭的.

In fact, if you run a process that does not terminate on EOF, you'll still not be able to use your FIFO you redirected to the process. Which I think is because this FIFO is considered closed.

反正有解决此问题的方法吗?

Is there anyway to work around this problem?

之所以会遇到这个问题,是因为我想将命令发送到在屏幕会话中运行的我的Minecraft服务器.例如:echo "command" >FIFO_to_server.单独使用屏幕可能可以做到这一点,但我对屏幕不太满意,我认为仅使用管道的解决方案会更简单,更干净.

The reason to why I ran into this problem is because I'd like to send commands to my minecraft server running in a screen session. For example: echo "command" >FIFO_to_server. This is problably possible to do by using screen by itself but I'm not very comfortable with screen I think a solution using only pipes would be a simpler and cleaner one.

推荐答案

A正在读取文件.当到达文件末尾时,它将停止读取.即使文件碰巧是fifo,这也是正常现象.您现在有四种方法.

A is reading from a file. When it reaches the end of the file, it stops reading. This is normal behavior, even if the file happens to be a fifo. You now have four approaches.

  1. 更改阅读器的代码,以使其在文件结束后继续阅读.这就是说输入文件是无限的,到达文件末尾只是一种幻想.对您而言不切实际,因为您必须更改Minecraft服务器代码.
  2. 应用unix哲学.您有一个不同意协议的作家和一个读者,因此您插入了一个将它们连接起来的工具.碰巧,unix工具箱中有一个这样的工具:tail -f. tail -f即使看到文件的末尾,也会继续从其输入文件读取数据.让您的所有客户与管道进行对话,然后将tail -f连接到minecraft服务器:

  1. Change the code of the reader to make it keep reading after the end of the file. That's saying the input file is infinite, and reaching the end of the file is just an illusion. Not practical for you, because you'd have to change the minecraft server code.
  2. Apply unix philosophy. You have a writer and a reader who don't agree on protocol, so you interpose a tool that connects them. As it happens, there is such a tool in the unix toolbox: tail -f. tail -f keeps reading from its input file even after it sees the end of the file. Make all your clients talk to the pipe, and connect tail -f to the minecraft server:

tail -n +1 -f client_pipe | minecraft_server &

  • jilles提到的 ,请使用技巧:管道支持多个编写器,并且仅在最后一个编写器消失时才关闭.因此,请确保有一个永远不会消失的客户.

  • As mentioned by jilles, use a trick: pipes support multiple writers, and only become closed when the last writer goes away. So make sure there's a client that never goes away.

    while true; do sleep 999999999; done >client_pipe &
    

  • 问题是服务器从根本上被设计为处理单个客户端.要处理多个客户端,应更改为使用套接字.将套接字视为元管道":连接到套接字会创建管道,一旦客户端断开连接,该特定管道就会关闭,但是服务器可以接受更多连接.这是干净的方法,因为它还可以确保在两个客户端碰巧同时连接时(使用管道,它们的命令可以散布),您不会混淆数据.但是,这需要更改Minecraft服务器.

  • The problem is that the server is fundamentally designed to handle a single client. To handle multiple clients, you should change to using a socket. Think of sockets as "meta-pipes": connecting to a socket creates a pipe, and once the client disconnects, that particular pipe is closed, but the server can accept more connections. This is the clean approach, because it also ensures that you won't have mixed up data if two clients happen to connect at the same time (using pipes, their commands could be interspersed). However, it require changing the minecraft server.

    这篇关于防止FIFO关闭/重用已关闭的FIFO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆