我在 Linux 中从事一个 C++ 项目,其中两个程序通过命名管道进行通信。
现在我想在一个程序中检测另一个程序是否与命名管道断开连接。
有没有办法检测断开连接?
编辑
我在 O_RDWR 模式下打开了 fifo - 这就是 select 在断开连接时没有反应的原因。现在我用 (O_RDONLY | O_NDELAY) 打开 fifo,一切正常。
最佳答案
这与 TCP/IP 中的相同。您需要尝试读取数据,如果失败并返回 0,管道将关闭。
阅读和接收:
These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.
还有 SIGPIPE
信号。当您尝试写入损坏的管道 - 没有读者的管道时,它会被发送。
关于c++ - 在 Linux 中检测与命名管道的断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14222314/