tschust
tschust

Reputation: 15

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.

Upvotes: 0

Views: 2393

Answers (2)

user823738
user823738

Reputation: 17521

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.

Upvotes: 1

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29529

If read on one end returns with 0 bytes, the pipe is disconnected.

Upvotes: 1

Related Questions