Search code examples
cipcpipe

How to find the error returned by read()?


I am reading data sent by the parent process using pipe. Parent process close the read end and write data on the write end of pipe. Similarly, child closes write end and read data from read end.

But in my case, read returned the "-1" which is error value. How should I find that, which error(like EAGAIN, EBADF, EIO ) has been occurred in read call? Thanks


Solution

  • How should i found that, which error(like EAGAIN, EBADF, EIO ) has been occurred in read call?

    Print errno. An even better option is to do a perror, right after the call.

    if (read(...) < 0)
        perror("read");
    

    Or use strerror if you need to get the message yourself:

    printf("%s\n", strerror(errno));
    

    Note you'll need to #include <errno.h> if you use errno directly.