Libevent Learning notes I. Basic knowledge

Source: Internet
Author: User
Tags epoll

Welcome Reprint, Reproduced please specify the original address: http://blog.csdn.net/majianfei1023/article/details/46485705
first, what is Libevent?Libevent is a lightweight, open-source, high-performance event-triggered network library for Windows, Linux, BSD and other platforms that use system invoke management event mechanisms such as SELECT, Epoll, and Kqueue. It is used by many open source projects, such as the famous memcached and so on. Features:event-driven, high performance;lightweight, focused on the network (relative to ACE);Open Source, the code is quite refined, easy to read;Cross-platform support for Windows, Linux, BSD and Mac OS;support multiple I/O multiplexing Technologies (Epoll, poll, Dev/poll, select, Kqueue, etc.), the abstraction of a multiplexed model under different operating systems, and the option to use different models to provide services through event functions;supports events such as I/O, timers and signals;adopt reactor mode;
Second, downloadlibevent1.4 code is relatively small, the structure is relatively simple, but also suitable for beginners to learn, 1.4 have seen long ago, we want to learn can also learn 1.4. The study here is libevent2.0 and above. The code volume is also much higher than 1.4, plus a lot of features.
libevent Download: http://libevent.org/
i downloaded the libevent-2.0.22-stable.tar.gz .
after downloading, unzip and then go to the directory to install.
[[email protected] libevent-2.0.22-stable]$./configure--prefix=/home/mjf/lib (prefix is my configuration directory, default can not add)[[email protected] libevent-2.0.22-stable]$ make[[email protected] libevent-2.0.22-stable]$ sudo make install
Note:/configure--prefix=/home/mjf/lib because if I do not add prefix, the following error occurs when executing the sample program: Error while loading shared libraries: Libevent-2.0.so.5:cannot open Shared object file:no such file or directory, so I customized the path, there is no problem, of course, you may not have a problem, then do not add, after the install straight You can see the libevent*.so in the/usr/lib or/usr/local/lib.
[email protected] libevent-2.0.22-stable]$ Whereis libevent-2.0.so.5
[email protected] ~]$ ls-al/usr/lib | grep libeventlrwxrwxrwx. 1 root root June 22:15 libevent-2.0.so.5-libevent-2.0.so.5.1.9-rwxr-xr-x. 1 root root 971951 June 22:15 libevent-2.0.so.5.1.9-rw-r--r--. 1 root root 1575744 June 22:15 libevent.alrwxrwxrwx. 1 root root 22:15 libevent_core-2.0.so.5-libevent_core-2.0.so.5.1.9-rwxr-xr-x. 1 root root 588276 June 22:15 libevent_core-2.0.so.5.1.9-rw-r--r--. 1 root root 982040 June 22:15 Libevent_core.a-rwxr-xr-x. 1 root root 970 June 22:15 libevent_core.lalrwxrwxrwx. 1 root root 22:15 libevent_core.so-libevent_core-2.0.so.5.1.9lrwxrwxrwx. 1 root root June 22:15 libevent_extra-2.0.so.5-libevent_extra-2.0.so.5.1.9-rwxr-xr-x. 1 root root 405038 June 22:15 libevent_extra-2.0.so.5.1.9-rw-r--r--. 1 root root 593776 June 22:15 libevent_extra.a-rwxr-xr-x. 1 root root 977 June 22:15 libevent_extra.lalrwxrwxrwx. 1 root root June 22:15 libevent_extra.so-libevent_extra-2.0.so.5.1.9-rwxr-xr-x. 1 root root 935 June 22:15 Libevent.lalrwxrwxrwx. 1 root root 22:15 libevent_pthreads-2.0.so.5-libevent_pthreads-2.0.so.5.1.9-rwxr-xr-x. 1 root root 18446 June 22:15 libevent_pthreads-2.0.so.5.1.9-rw-r--r--. 1 root root 18686 June 22:15 Libevent_pthreads.a-rwxr-xr-x. 1 root root 998 June 22:15 libevent_pthreads.lalrwxrwxrwx. 1 root root 22:15 libevent_pthreads.so-libevent_pthreads-2.0.so.5.1.9lrwxrwxrwx. 1 root root June 22:15 libevent.so-libevent-2.0.so.5.1.9

we can see some of the following libraries. Libevent_core: All the core events and buffering functions, including all event_base, Evbuffer, bufferevent, and tool functions.

Libevent_extra: Defines protocol-specific features that a program may or may not require, including HTTP, DNS, and RPC.

Libevent: This library exists for historical reasons, and it contains the contents of Libevent_core and Libevent_extra. This library should not be used, and future versions of Libevent may remove the library.

Libevent_pthreads: Add thread and lock implementations based on Pthread portable line libraries. It is independent of Libevent_core, so the program does not need to link to pthread when using libevent, unless libevent is used in a multithreaded manner.

third, the function of libevent. Libevent provides event notifications, IO cache events, timers, timeouts, asynchronous parsing of DNS, event-driven HTTP server, and an RPC framework.
Event Notification: The callback function executes when the file descriptor is readable and writable.
IO cache: The cache event provides input and output caching, automatic read-in and write, and the user does not have to manipulate IO directly.
Timers: Libevent provides a mechanism for timers to invoke callback functions after a certain interval of time.
signal: Trigger Signal, execute callback.
Asynchronous DNS resolution: Libevent provides a set of DNS resolver functions that resolve DNS servers asynchronously.
Event-Driven HTTP server: Libevent provides a simple HTTP server that can be integrated into an application.
RPC Client Server framework: Libevent creates an RPC framework for creating RPC servers and clients that automatically encapsulates and wraps data structures.
Iv. Reactor (reactor) modeLibevent is a typical implementation of the reactor pattern. Here is a brief introduction:we know that the normal function call mechanism is as follows: The program calls a function, the function executes, the program waits, and the function returns the result to the calling program (if it contains a function return value), which is executed sequentially. the basic flow of the reactor mode is as follows: The application needs to provide the appropriate interface and register to the reactor reactor, if the corresponding event occurs, then reactor will automatically invoke the corresponding registered interface function (similar to the callback function) to notify you, So Libevent is an event-triggered network library.
Iv. compile and run a small example
#include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <event.h>//Timer event callback function void OnTime (int sock, short event, void *arg) {     printf ("hello,world!\n");    struct Timeval TV;     Tv.tv_sec = 1;     tv.tv_usec = 0;     Re-add timed events (default auto-Deletion after timed event triggering)     event_add (struct event*) arg, &TV);}    int main () {     //Initialize     event_init ();        struct event ev_time;     Set timed Event     Evtimer_set (&ev_time, OnTime, &ev_time);        struct Timeval TV;     Tv.tv_sec = 1;     tv.tv_usec = 0;     Add timed Event     Event_add (&ev_time, &TV);        Event Loop     Event_dispatch ();        return 0; }

gcc example1.c-o example1-levent
you can see the output "hello,world!" every two seconds at the terminal. The

 

Libevent Learning notes I. Basic knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.