6

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

Matt Fichman
  • 5,458
  • 4
  • 39
  • 59

2 Answers2

2

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.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
1

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.

  • How do you know regular files are always writable? – Matt Fichman Jan 16 '13 at 15:05
  • Files don't become temporarily nonwritable (from being "full") in the same sort of way that sockets, pipes, and whatnot can. –  Jan 16 '13 at 17:59
  • @duskwuff no, but they definitely block. They can cause application lag/hang ups. The whole point of using these mechanisms is signal an event on these resources and when accessing these resources will not block in relation to these events. "full" concept is irrelevant. – Rahly Jun 18 '17 at 23:34