简体   繁体   中英

Detect disconnect from named pipe in linux

I work on an C++ project in Linux where two programs communicate over a named pipe.

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. Now I open the fifo with (O_RDONLY | O_NDELAY) and all works fine.

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

This is same as in TCP/IP. You need to attempt to read data, if that fails with 0, pipe is closed.

read and recv:

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 .

There is also SIGPIPE signal. It'll be sent when you try write to a broken pipe - pipe with no readers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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