0

Suppose you have the following bits of code:

int fd[2]; 
pipe(fd);
close(fd[0]);

//I'd like some code that takes in
//fd and tells me that the read end has been closed but the write end is
//still open, without actually writing or reading in C.

What's the simplest way to accomplish this?

Snowball
  • 1,138
  • 2
  • 14
  • 30
  • 1
    This is not a duplicate of "How to check if *this* end of a pipe has been closed", but rather "How to check that the *other* end of a pipe has been closed". When writing to your pipe, you already must to have some logic to detect and handle EOF condition. So the simplest way to do what you want, is not doing it. Just wait until you have something to write and handle it then. If this is not good enough, you could implement some "heartbeat message" in your protocol. – HAL9000 Mar 22 '21 at 21:37
  • If your program ignores SIGPIPE, an attempt to write on the write end of a pipe will fail with an error if there is no process with the read end of the pipe open. You can't check the status of the read end of the pipe given only the write end without attempting to write. AFAIK, there's no guarantee that a write of zero bytes will check the file descriptor before checking that there is data to write, so that is at best unreliable and non-portable. It's easier to ask forgiveness than to get permission. – Jonathan Leffler Mar 23 '21 at 00:15

0 Answers0