2 gcc kqtimer.c -o kqtimer -Wall -W -Wextra -ansi -pedantic
4 The following code will setup a timer that will trigger a
5 kevent every 5 seconds. Once it does, the process will fork
6 and the child will execute the date(1) command.
13 #include <string.h> /* for strerror() */
16 /* function prototypes */
17 void diep(const char *s
);
21 struct kevent change
; /* event we want to monitor */
22 struct kevent event
; /* event that was triggered */
26 /* create a new kernel event queue */
27 if ((kq
= kqueue()) == -1)
30 /* initalise kevent structure */
31 EV_SET(&change
, 1, EVFILT_TIMER
, EV_ADD
| EV_ENABLE
, 0, 5000, 0);
35 nev
= kevent(kq
, &change
, 1, &event
, 1, NULL
);
41 if (event
.flags
& EV_ERROR
) { /* report any error */
42 fprintf(stderr
, "EV_ERROR: %s\n", strerror(event
.data
));
46 if ((pid
= fork()) < 0) /* fork error */
49 else if (pid
== 0) /* child */
50 if (execlp("date", "date", (char *)0) < 0)
59 void diep(const char *s
)