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:

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


Creating a CFFileDescriptor


Creates a new CFFileDescriptor.

Getting Information About a File Descriptor


Returns the native file descriptor for a given CFFileDescriptor.


Returns a Boolean value that indicates whether the native file descriptor for a given CFFileDescriptor is valid.


Gets the context for a given CFFileDescriptor.

Invalidating a File Descriptor


Invalidates a CFFileDescriptor object.

Managing Callbacks


Enables callbacks for a given CFFileDescriptor.


Disables callbacks for a given CFFileDescriptor.

Creating a Run Loop Source


Creates a new runloop source for a given CFFileDescriptor.

Getting the CFFileDescriptor Type ID


Returns the type identifier for the CFFileDescriptor opaque type.

Data Types


Defines a type for the native file descriptor.


Defines a structure for a callback for a CFFileDescriptor.


Defines a structure for the context of a CFFileDescriptor.


A reference to an CFFileDescriptor object.


Callback Identifiers

Constants that identify the read and write callbacks.

See Also

Opaque Types


CFStringTokenizer allows you to tokenize strings into words, sentences or paragraphs in a language-neutral way. It supports languages such as Japanese and Chinese that do not delimit words by spaces, as well as de-compounding German compounds. You can obtain Latin transcription for tokens. It also provides language identification API.