对应的sample文件中提供了event_test.c,里面就是关于事件的简单示例,具体如下:
1 /* 2 * Compile with: 3 * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent 4 */ 5 6 #ifdef HAVE_CONFIG_H 7 #include "config.h" 8 #endif 9 10 #include <sys/types.h> 11 #include <sys/stat.h> 12 #include <sys/queue.h> 13 #include <unistd.h> 14 #include <sys/time.h> 15 #include <fcntl.h> 16 #include <stdlib.h> 17 #include <stdio.h> 18 #include <string.h> 19 #include <errno.h> 20 21 #include <event.h> 22 23 static void 24 fifo_read(int fd, short event, void *arg) 25 { 26 char buf[255]; 27 int len; 28 struct event *ev = arg; 29 30 /* Reschedule this event */ 31 event_add(ev, NULL); 32 33 fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p ", 34 fd, event, arg); 35 36 len = read(fd, buf, sizeof(buf) - 1); 37 38 if (len == -1) { 39 perror("read"); 40 return; 41 } else if (len == 0) { 42 fprintf(stderr, "Connection closed "); 43 return; 44 } 45 46 buf[len] = '