0

When reading process quits, how do i determine it from writing process before write call blocks ? Normally when read side closes, write call on the write side should return an error right?

client

while(!timeout)
{
    read(fd, message, BUFFER_SIZE);
}

server

while(1)
{
    length = write(fd, message, strlen(message));
    if(length <= 0)
    {
        break;
    }
}
user3048641
  • 105
  • 6

1 Answers1

2

Read carefully fifo(7):

When a process tries to write to a FIFO that is not opened for read on the other side, the process is sent a SIGPIPE signal.

You could -and probably should- use poll(2) to test dynamic readability or writability of a fifo or pipe or socket file descriptor (see this answer about a simplistic event loop using poll). See also write(2) & Advanced Linux Programming.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I am very surprised by the number of times people are asking here what the `man` pages give immediately and quickly. They have forgotten what [RTFM](http://en.wikipedia.org/wiki/RTFM) means - and also STFW. I'm old enough to have learn Unix by reading the man pages on paper (at SunOS3.2 time, probably 1987). – Basile Starynkevitch Apr 13 '14 at 18:33
  • My problem is after writing / reading some data between processes when reading program ends writing one still tries to write and blocking. – user3048641 Apr 13 '14 at 19:01
  • Use `poll` before attempting the `write`; very probably you need to build your programs around an *event loop*. – Basile Starynkevitch Apr 13 '14 at 19:02