2012-07-19 66 views

回答

13

是的,這是C.可能你應該尋找內核隊列CPP版本。

這裏是一個小樣本看目錄:

#include <errno.h>  // for errno 
#include <fcntl.h>  // for O_RDONLY 
#include <stdio.h>  // for fprintf() 
#include <stdlib.h>  // for EXIT_SUCCESS 
#include <string.h>  // for strerror() 
#include <sys/event.h> // for kqueue() etc. 
#include <unistd.h>  // for close() 

int main (int argc, const char *argv[]) 
{ 
    int kq = kqueue(); 
    // dir name is in argv[1], NO checks for errors here 
    int dirfd = open (argv[1], O_RDONLY); 

    struct kevent direvent; 
    EV_SET (&direvent, dirfd, EVFILT_VNODE, EV_ADD | EV_CLEAR | EV_ENABLE, 
      NOTE_WRITE, 0, (void *)dirname); 

    kevent(kq, &direvent, 1, NULL, 0, NULL); 

    // Register interest in SIGINT with the queue. The user data 
    // is NULL, which is how we'll differentiate between 
    // a directory-modification event and a SIGINT-received event. 
    struct kevent sigevent; 
    EV_SET (&sigevent, SIGINT, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0, NULL); 
    // kqueue event handling happens after the legacy API, so make 
    // sure it doesn eat the signal before the kqueue can see it. 
    signal (SIGINT, SIG_IGN); 

    // Register the signal event. 
    kevent(kq, &sigevent, 1, NULL, 0, NULL); 

    while (1) { 
     // camp on kevent() until something interesting happens 
     struct kevent change; 
     if (kevent(kq, NULL, 0, &change, 1, NULL) == -1) { exit(1); } 
     // The signal event has NULL in the user data. Check for that first. 
     if (change.udata == NULL) { 
      break; 
     } else { 
     // udata is non-null, so it's the name of the directory 
     printf ("%s\n", (char*)change.udata); 
     } 
    } 
    close (kq); 
    return 0; 
} 

的細節可以在通道上找到。 Mark Dalrymple的「高級Mac OSX編程」的16(kqueues和FSEvents)。其他信息可以在* kdk的BSD文檔中找到。

或者從FSEvents使用這個API(它主要是基於C語言的)。

FSEventStreamRef FSEventStreamCreate (CFAllocatorRef allocator, 
            FSEventStreamCallback callback, 
            FSEventStreamContext *context, 
            CFArrayRef pathsToWatch, 
            FSEventStreamEventId sinceWhen, 
            CFTimeInterval latency, 
            FSEventStreamCreateFlags flags); 

使用純C回調創建FSEvents事件流。通過使用-getCFRunLoop得到一個NSRunLoop的CFRunLoop:

然後使用

void FSEventStreamScheduleWithRunLoop (FSEventStreamRef streamRef, 
            CFRunLoopRef runLoop, 
            CFStringRef runLoopMode); 

是的,在這裏你應該使用一個線的OBJ-C的獲得RunLoop處理此事件流附加到您runloop

CFRunLoop* loopRef = [[NSRunLoop currentRunLoop] getCFRunLoop]; 

,或者使用純C呼叫

CFRunLoop* loopRef = CFRunLoopGetCurrent(); 

啓動事件與

Boolean FSEventStreamStart (FSEventStreamRef streamRef); 

流與

void FSEventStreamStop (FSEventStreamRef streamRef); 

從runloop與此停止事件流,然後取消調度它:

void FSEventStreamUnscheduleFromRunLoop (FSEventStreamRef streamRef, 
            CFRunLoopRef runLoop, 
            CFStringRef runLoopMode); 

無效流(清理):

void FSEventStreamInvalidate (FSEventStreamRef streamRef); 

希望這會讓你tarted。

+0

在我的實驗中,kqueue在功能上並不等同於FSEvents。感謝您概述CFRunLoop位! – berkus 2014-01-07 10:52:24