3 * gcc kqtimer.c -o kqtimer -Wall -W -Wextra -ansi -pedantic
5 * The following code will setup a timer that will trigger a
6 * kevent every 5 seconds. Once it does, the process will fork
7 * and the child will execute the date(1) command.
10 #include <sys/event.h>
14 #include <string.h> /* for strerror() */
17 /* Function prototypes */
18 void diep(const char *s
);
22 struct kevent change
; /* event we want to monitor */
23 struct kevent event
; /* event that was triggered */
27 /* Create a new kernel event queue */
28 if ((kq
= kqueue()) == -1)
31 /* Initalise kevent structure */
32 EV_SET(&change
, 1, EVFILT_TIMER
, EV_ADD
| EV_ENABLE
, 0, 5000, 0);
36 nev
= kevent(kq
, &change
, 1, &event
, 1, NULL
);
42 if (event
.flags
& EV_ERROR
) { /* report any error */
43 fprintf(stderr
, "EV_ERROR: %s\n", strerror(event
.data
));
47 if ((pid
= fork()) < 0) /* fork error */
50 else if (pid
== 0) /* child */
51 if (execlp("date", "date", (char *)0) < 0)
63 void diep(const char *s
)