Add stress test case
[eleutheria.git] / kqueue / kqtimer.c
blobf406854647d9d79d7cf02e7e5095d8152ff94730
1 /* compile with:
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.
7 */
9 #include <sys/event.h>
10 #include <sys/time.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h> /* for strerror() */
14 #include <unistd.h>
16 /* function prototypes */
17 void diep(const char *s);
19 int main(void)
21 struct kevent change; /* event we want to monitor */
22 struct kevent event; /* event that was triggered */
23 pid_t pid;
24 int kq, nev;
26 /* create a new kernel event queue */
27 if ((kq = kqueue()) == -1)
28 diep("kqueue()");
30 /* initalise kevent structure */
31 EV_SET(&change, 1, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, 5000, 0);
33 /* loop forever */
34 for (;;) {
35 nev = kevent(kq, &change, 1, &event, 1, NULL);
37 if (nev < 0)
38 diep("kevent()");
40 else if (nev > 0) {
41 if (event.flags & EV_ERROR) { /* report any error */
42 fprintf(stderr, "EV_ERROR: %s\n", strerror(event.data));
43 exit(EXIT_FAILURE);
46 if ((pid = fork()) < 0) /* fork error */
47 diep("fork()");
49 else if (pid == 0) /* child */
50 if (execlp("date", "date", (char *)0) < 0)
51 diep("execlp()");
55 close(kq);
56 return EXIT_SUCCESS;
59 void diep(const char *s)
61 perror(s);
62 exit(EXIT_FAILURE);