Created
March 15, 2012 03:23
-
-
Save evandrix/2041676 to your computer and use it in GitHub Desktop.
Linux epoll / BSD kqueue sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#include <sys/types.h> | |
#include <sys/event.h> | |
#include <sys/time.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <inttypes.h> | |
int main(void) | |
{ | |
int f, kq, nev; | |
struct kevent change; | |
struct kevent event; | |
kq = kqueue(); | |
if (kq == -1) | |
perror("kqueue"); | |
f = open("/tmp/foo", O_RDONLY | O_CREAT); | |
if (f == -1) | |
{ | |
perror("open"); | |
} | |
EV_SET(&change, f, EVFILT_VNODE, | |
EV_ADD | EV_ENABLE | EV_ONESHOT, | |
NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB, | |
0, 0); | |
for (;;) { | |
nev = kevent(kq, &change, 1, &event, 1, NULL); | |
if (nev == -1) | |
perror("kevent"); | |
else if (nev > 0) | |
{ | |
if (event.fflags & NOTE_DELETE) | |
{ | |
printf("File deleted\n"); //break; | |
} | |
if (event.fflags & NOTE_EXTEND || event.fflags & NOTE_WRITE) | |
printf("File modified\n"); | |
if (event.fflags & NOTE_ATTRIB) | |
printf("File attributes modified\n"); | |
} | |
} | |
close(kq); | |
close(f); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment