Wednesday 2 March 2011

Libevent

Libevent is an asynchronous event notification library. It provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. It also supports callbacks due to signals or regular timeouts.

The great advantage of Libevent is the replacement of the event loops commonly found on event driver network servers and applications.

An application just needs to call event_dispatch() and then add or remove event dynamically without having to change the event loop.

Important information about event notification mechanisms can be found on Dan Kegel's web page: The C10K problem

More information can be found on he oficial Libevent web page: http://monkey.org/~provos/libevent/

I had to use libevent on some parts of my MsC work and open-source project. So here goes a simple starter example written in C to handle a SIGINT signal:

1:  #include <stdio.h>
2: #include <stdlib.h>
3: #include <unistd.h>
4: #include <signal.h>
5: #include <event.h>
6:
7: void signalhandler(int fd, short event, void *arg) {
8:
9: struct event *ev = arg;
10: printf("SIGINT triggered!\n");
11: }
12:
13: int main (int argc, char *argv[]) {
14:
15: struct event ev;
16:
17: /* event API needs to be initialized with before it can be used. */
18: event_init();
19:
20: /* set the event SIGINT with event type EV_SIGNAL for the hook signalhandler /*
21: event_set(&ev, SIGINT, EV_SIGNAL | EV_PERSIST, signalhandler, &ev);
22:
23: /* add the event */
24: event_add(&ev, NULL);
25:
26: /* process the event */
27: event_dispatch();
28:
29: return 0;
30: }


Later I will post more examples and further explanation.

No comments:

Post a Comment