简体   繁体   English

检测linux中与命名管道的断开连接

[英]Detect disconnect from named pipe in linux

I work on an C++ project in Linux where two programs communicate over a named pipe. 我在Linux上从事一个C ++项目,其中两个程序通过命名管道进行通信。

Now I want to detect in one program if the other disconnects from the named pipe. 现在我想在一个程序中检测另一个程序是否与命名管道断开连接。

Is there a way to detect the disconnect? 有没有办法检测断开?

Edit 编辑

I opened the fifo in O_RDWR mode - that was the reason that select didn't react on the disconnect. 我在O_RDWR模式下打开了fifo - 这就是select没有对断开连接做出反应的原因。 Now I open the fifo with (O_RDONLY | O_NDELAY) and all works fine. 现在我用(O_RDONLY | O_NDELAY)打开fifo,一切正常。

如果一端read返回0字节,则管道断开连接。

This is same as in TCP/IP. 这与TCP / IP相同。 You need to attempt to read data, if that fails with 0, pipe is closed. 您需要尝试读取数据,如果失败为0,则管道已关闭。

read and recv: 阅读和回忆:

These calls return the number of bytes received, or -1 if an error occurred. 这些调用返回接收的字节数,如果发生错误则返回-1。 The return value will be 0 when the peer has performed an orderly shutdown . 当对等体执行有序关闭时,返回值将为0

There is also SIGPIPE signal. 还有SIGPIPE信号。 It'll be sent when you try write to a broken pipe - pipe with no readers. 当你尝试写一个没有读者的破损管道时会发送它。

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

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