How can select() wait on regular file descriptors (non-sockets)?

前端 未结 1 1913
野性不改
野性不改 2020-12-17 20:13

This is the code sample from \"man select\" plus a few lines to read an actual file which is being written to. I suspected that when the ./myfile.txt is writte

相关标签:
1条回答
  • 2020-12-17 21:02

    Disk files are always ready to read (but the read might return 0 bytes if you're already at the end of the file), so you can't use select() on a disk file to find out when new data is added to the file.

    POSIX says:

    File descriptors associated with regular files shall always select true for ready to read, ready to write, and error conditions.

    Also, as cnicutar pointed out in a now-deleted post, in general, you have to initialize the FD_SET on each iteration. In your code, you are monitoring one fd, and that fd is always ready, so the FD_SET is not in fact changing. However, if you have 5 decriptors to monitor, and select detects that only one is ready, then on the next iteration, only that one descriptor would be monitored (unless you reset the FD_SET). This makes using select tricky.

    0 讨论(0)
提交回复
热议问题