Question

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?"

Was it helpful?

Solution 2

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.

OTHER TIPS

Kernel Queues are mechanisms which "allow you to intercept kernel-level events to receive notifications about changes to sockets, processes, the file system and other aspects of the system."

I've used them in the past to detect when when actions happen on a file (or within a hot folder). I don't believe they can be used for "reading" and "writing" files, though. You can use MacOS native functions or regular UN*X style "fopen", "fwrite" and "fread" calls for this too, if you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top