Linux: Checking if a socket/pipe is broken without doing a read()/write()

前端 未结 3 642
暖寄归人
暖寄归人 2021-02-09 10:45

I have a simple piece of code that periodically writes data to a fd that\'s passed to it. The fd will most likely be a pipe or socket but could potentially be anything. I can de

3条回答
  •  甜味超标
    2021-02-09 11:31

    struct pollfd pfd = {.fd = yourfd, .events = POLLERR};
    if (poll(&pfd, 1, whatever) < 0) abort();
    if (pfd.revents & POLLERR) printf("pipe is broken\n");
    

    This does work for me. Note that sockets are not exactly pipes and thus show different behavior (-> use POLLRDHUP).

提交回复
热议问题