Mac Developer Library


Core Foundation Framework Reference CFFileDescriptor Reference

Deployment Target:

On This Page

CFFileDescriptor Reference

The CFFileDescriptor provides an opaque type to monitor file descriptors for read and write activity via CFRunLoop.

You use CFFileDescriptor to monitor file descriptors for read and write activity via CFRunLoop using callbacks. Each call back is one-shot, and must be re-enabled if you want to get another one.

You can re-enable the callback in the callback function itself, but you must completely service the file descriptor before doing so. For example, if you create a CFFileDescriptor for a pipe and get a callback because there are bytes to be read, then if you don't read all of the bytes but nevertheless re-enable the CFFileDescriptor for read activity, you'll get called back again immediately.

You can monitor kqueue file descriptors for read activity to find out when an event the kqueue is filtering for has occurred. You are responsible for understanding the use of the kevent() API and inserting and removing filters from the kqueue file descriptor yourself.

The following example takes a UNIX process ID as argument, and watches up to 20 seconds, and reports if the process terminates in that time:

  1. // cc test.c -framework CoreFoundation -O
  2. #include <CoreFoundation/CoreFoundation.h>
  3. #include <unistd.h>
  4. #include <sys/event.h>
  5. static void noteProcDeath(CFFileDescriptorRef fdref, CFOptionFlags callBackTypes, void *info) {
  6. struct kevent kev;
  7. int fd = CFFileDescriptorGetNativeDescriptor(fdref);
  8. kevent(fd, NULL, 0, &kev, 1, NULL);
  9. // take action on death of process here
  10. printf("process with pid '%u' died\n", (unsigned int)kev.ident);
  11. CFFileDescriptorInvalidate(fdref);
  12. CFRelease(fdref); // the CFFileDescriptorRef is no longer of any use in this example
  13. }
  14. // one argument, an integer pid to watch, required
  15. int main(int argc, char *argv[]) {
  16. if (argc < 2) exit(1);
  17. int fd = kqueue();
  18. struct kevent kev;
  19. EV_SET(&kev, atoi(argv[1]), EVFILT_PROC, EV_ADD|EV_ENABLE, NOTE_EXIT, 0, NULL);
  20. kevent(fd, &kev, 1, NULL, 0, NULL);
  21. CFFileDescriptorRef fdref = CFFileDescriptorCreate(kCFAllocatorDefault, fd, true, noteProcDeath, NULL);
  22. CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack);
  23. CFRunLoopSourceRef source = CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, fdref, 0);
  24. CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopDefaultMode);
  25. CFRelease(source);
  26. // run the run loop for 20 seconds
  27. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 20.0, false);
  28. return 0;
  29. }


Data Types


  • Constants that identify the read and write callbacks.



    var kCFFileDescriptorReadCallBack: CFOptionFlags { get } var kCFFileDescriptorWriteCallBack: CFOptionFlags { get }


    enum { kCFFileDescriptorReadCallBack = 1 << 0, kCFFileDescriptorWriteCallBack = 1 << 1 };


    • kCFFileDescriptorReadCallBack


      Identifies the read callback.

      Available in OS X v10.5 and later.

    • kCFFileDescriptorWriteCallBack


      Identifies the write callback.

      Available in OS X v10.5 and later.