What does select(2) do if you close(2) a file descriptor in a separate thread?

前端 未结 4 1545
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 09:53

What is the behavior of the select(2) function when a file descriptor it is watching for reading is closed by another thread?

From some cursory testing,

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 10:36

    From some additional investigation, it appears that both dwc and bothie are right.

    bothie's answer to the question boils down to: it's undefined behavior. That doesn't mean that it's unpredictable necessarily, but that different OSes do it differently. It would appear that systems like Solaris and HP-UX return from select(2) in this case, but Linux does not based on this post to the linux-kernel mailing list from 2001.

    The argument on the linux-kernel mailing list is essentially that it is undefined (and broken) behavior to rely upon. In Linux's case, calling close(2) on the file descriptor effectively decrements a reference count on it. Since there is a select(2) call also with a reference to it, the fd will remain open and waiting for input until the select(2) returns. This is basically dwc's answer. You will get an event on the file descriptor and then it'll be closed. Trying to read from it will result in a EBADF, assuming the fd hasn't been recycled. (A concern that MarkR made in his answer, although I think it's probably avoidable in most cases with proper synchronization.)

    So thank you all for the help.

提交回复
热议问题