简体   繁体   English

尝试将StreamWriter打开到命名管道时Mono挂起

[英]Mono hangs when trying to open a StreamWriter to a named pipe

The program I'm writing is using FIFO pipes in linux for inter-process communication. 我正在编写的程序是在linux中使用FIFO管道进行进程间通信。 It's rather hacky at best, but regardless I'm having issues. 它充其量只是hacky,但无论我遇到什么问题。

        if (!File.Exists(Path.GetTempPath() + "gminput.pipe"))
        {
            ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/usr/bin/mkfifo", Arguments = Path.GetTempPath() + "gminput.pipe", };
            Process proc = new Process() { StartInfo = startInfo, };
            proc.Start();
            proc.WaitForExit();
        }
        if (!File.Exists(Path.GetTempPath() + "gmoutput.pipe"))
        {
            ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/usr/bin/mkfifo", Arguments = Path.GetTempPath() + "gmoutput.pipe", };
            Process proc = new Process() { StartInfo = startInfo, };
            proc.Start();
            proc.WaitForExit();
        }

        using (StreamWriter outputPipe = new StreamWriter(Path.GetTempPath() + "gmoutput.pipe"))
        using (StreamReader inputPipe = new StreamReader(Path.GetTempPath() + "gminput.pipe"))
        {
            Console.WriteLine("This code is never reached!");
        }

All I'm doing is checking if the pipe already exists, and if not, call mkfifo to create it. 我正在做的就是检查管道是否已经存在,如果没有,请调用mkfifo来创建它。 This part seems to work fine, the named pipes are created correctly. 这部分似乎工作正常,命名管道是正确创建的。 Whenever I try to open them (either with the StreamWriter, StreamReader, or both), the program just hangs. 每当我尝试打开它们时(使用StreamWriter,StreamReader或两者),程序就会挂起。 No errors or anything. 没有错误或任何东西。 It hangs in the debugger too. 它也挂在调试器中。

The best part is...it used to work. 最好的部分是...它曾经工作过。 I had the inter-process communication working and then it just inexplicably stopped. 我进行了进程间通信,然后它莫名其妙地停止了。 I commented out everything except what you see here, restarted my system, recreated the pipes, etc, to no avail. 除了你在这里看到的东西,重新启动我的系统,重新创建管道等,我注释掉了所有的东西,但没有用。 What gives? 是什么赋予了? Is there something wrong with my code or is something else on the system interfering? 我的代码有问题还是系统上的其他东西干扰了?

That's by design. 这是设计的。 Try the following: open 2 bash terminals, create a pipe, then read it in one of the terminals and write to it in the other. 请尝试以下操作:打开2个bash终端,创建一个管道,然后在其中一个终端中读取它并在另一个终端中写入。 For example 例如

>mkfifo test.fifo
>echo "test" > test.fifo

>cat test.fifo

You'll see that no matter the order, each side blocks waiting for the other side. 你会看到,无论顺序如何,每一方都在等待另一方。

Process 1's input pipe is Process 2's output pipe and visa versa. 过程1的输入管道是过程2的输出管道,反之亦然。 If both processes use the same code to access the pipe, process 1 reads its input pipe and blocks waiting for process 2 to write something. 如果两个进程使用相同的代码来访问管道,则进程1读取其输入管道并阻塞进程2等待进程写入。 Process 2 also reads it's input pipe and waits for process 1 to write, but process 1 is waiting and hasn't even opened the other pipe. 进程2还读取它的输入管道并等待进程1写入,但进程1正在等待,甚至没有打开另一个管道。 Gridlock. 僵局。

One way to get around this is to run the reader or the writer in a separate thread. 解决此问题的一种方法是在单独的线程中运行阅读器或编写器。 That way process 1 & 2 open both pipes and the gridlock is resolved. 这样,进程1和2打开​​两个管道并解决了僵局。

The other option, is to open the pipe asynchronously. 另一种选择是异步打开管道。 My C# is rusty, but there are plenty of examples on stackoverflow: 我的C#生锈了,但stackoverflow上有很多例子:

How to do a non-waiting write on a named pipe (c#)? 如何在命名管道(c#)上进行非等待写入?

NamedPipeServerStream in Mono Mono中的NamedPipeServerStream

Basically pass a NamedPipeServerStream to the reader/writer. 基本上将NamedPipeServerStream传递给读取器/写入器。

I suspect it worked before because P1 opened Reader, then Writer, while P2 opened Writer, then Reader thus unblocking P1. 我怀疑它之前有效,因为P1打开了Reader,然后是Writer,而P2打开了Writer,然后Reader解锁了P1。

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

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