/*
* libevent自带的例子,因为不考虑在windows平台下使用,删去了一点跨平台的内容 * Compile with: * cc -I/usr/local/include -o signal-test signal-test.c -L/usr/local/lib -levent */
#include <sys/types.h>
#ifdef HAVE_CONFIG_H #include "config.h" #endif
#include <sys/stat.h> #include <sys/queue.h> #include <unistd.h> #include <sys/time.h> #include <signal.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h>
#include <event.h>
int called = 0;
static void signal_cb(int fd, short event, void *arg) { struct event *signal = arg;
printf("%s: got signal %d\n", __func__, EVENT_SIGNAL(signal));
if (called >= 2) event_del(signal);//事件删除 called++; }
int main (int argc, char **argv) { struct event signal_int; /* Initalize the event library */ event_init();//初始化
//可有 event_base main_base = event_init();
//另外,有专用于http、dns、rpc等的初始化函数
/* Initalize one event */ event_set(&signal_int, SIGINT, EV_SIGNAL|EV_PERSIST, signal_cb, &signal_int);//设置事件
//事件类型包括EV_READ、EV_WRITE、EV_PERSIST等
//另外有一个设置时间的函数evtimer_set(...);
event_add(&signal_int, NULL);//加入事件队列
//第二个参数为超时值,可以为空
event_dispatch();//启动事件循环
//另一种常用方式为event_base_loop(event_base类型变量/*event_base的返回值*/,0)
return (0); }
|