Search code examples
cpipenamed-pipessystems-programming

c - understand if pipe/fifo is full


I have a fifo opened as RDWR(for communicate process-process) and pipes(process-thread), how can I understand when I reach pipes or fifos limit? When I try to write more than 64 KB it just wait in write().


Solution

  • You need to use non-blocking mode:

    pipe2(fds, O_NONBLOCK);
    

    Or if you need to do it after the pipe was created:

    int flags = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    

    Now when you read or write and the operation cannot complete immediately, it will return. You can then use select() or poll() to find out when reading or writing is possible again (or you can just busy-wait).