Search code examples
cmacossocketsoperating-systemkqueue

Kqueue on regular files


Is kqueue (on OS X) useful for reading/writing regular files? I know that epoll is not useful for regular files on Linux, so I'm wondering if the same is true for kqueue.

EDIT: I don't mean reading/writing files, obviously read() and write() are for that. I meant, "is kqueue actually useful for detecting when a file is readable/writable?"


Solution

  • Yes, kqueue can be used to watch files for readability. From the man page:

     EVFILT_READ      Takes a file descriptor as the identifier, and returns
                      whenever there is data available to read.  The behavior
                      of the filter is slightly different depending on the
                      descriptor type.
    
     [...]
    
                      Vnodes
                          Returns when the file pointer is not at the end of
                          file.  data contains the offset from current posi-
                          tion to end of file, and may be negative.
    

    ("vnodes", in this context, are regular files.)

    As regular files are always writable, it makes no sense to apply EVFILT_WRITE to them.