网站改版的seo注意事项,网络公司官网,c 做网站 知乎,深圳餐饮设计公司排名一、libevent是什么libevent是一个轻量级的开源的高性能的事件触发的网络库#xff0c;适用于windows、linux、bsd等多种平台#xff0c;内部使用select、epoll、kqueue等系统调用管理事件机制。它被众多的开源项目使用#xff0c;例如大名鼎鼎的memcached等。特点#xff… 一、libevent是什么libevent是一个轻量级的开源的高性能的事件触发的网络库适用于windows、linux、bsd等多种平台内部使用select、epoll、kqueue等系统调用管理事件机制。它被众多的开源项目使用例如大名鼎鼎的memcached等。特点事件驱动高性能;轻量级专注于网络(相对于ACE);开放源码代码相当精炼、易读;跨平台支持Windows、Linux、BSD和Mac OS;支持多种I/O多路复用技术epoll、poll、dev/poll、select和kqueue等在不同的操作系统下做了多路复用模型的抽象可以选择使用不同的模型通过事件函数提供服务;支持I/O定时器和信号等事件;采用Reactor模式;二、下载libevent1.4代码量比较少,结构比较简单,也适合入门学习,1.4很早以前看过了,大家想学习也可以学习1.4。这里学习的是libevent2.0以上版本。代码量也比1.4高很多也加了很多功能。libevent下载:http://libevent.org/我下载的是 libevent-2.0.22-stable.tar.gz下载之后解压然后进入目录就可以安装了。[mjflocalhost libevent-2.0.22-stable]$ ./configure --prefix/home/mjf/lib (prefix是我配置目录默认可以不加)[mjflocalhost libevent-2.0.22-stable]$ make[mjflocalhost libevent-2.0.22-stable]$ sudo make install注./configure --prefix/home/mjf/lib 因为如果我不加prefix后面执行示例程序的时候会如下错误error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory 所以我自己定制了路径就没问题了当然你的可能没问题那就不用加install之后直接在/usr/lib或者/usr/local/lib里面就能看到了libevent*.so了。[mjflocalhost libevent-2.0.22-stable]$ whereis libevent-2.0.so.5[mjflocalhost ~]$ ls -al /usr/lib | grep libevent lrwxrwxrwx. 1 root root 21 Jun 13 22:15 libevent-2.0.so.5 - libevent-2.0.so.5.1.9-rwxr-xr-x. 1 root root 971951 Jun 13 22:15 libevent-2.0.so.5.1.9-rw-r--r--. 1 root root 1575744 Jun 13 22:15 libevent.alrwxrwxrwx. 1 root root 26 Jun 13 22:15 libevent_core-2.0.so.5 - libevent_core-2.0.so.5.1.9-rwxr-xr-x. 1 root root 588276 Jun 13 22:15 libevent_core-2.0.so.5.1.9-rw-r--r--. 1 root root 982040 Jun 13 22:15 libevent_core.a-rwxr-xr-x. 1 root root 970 Jun 13 22:15 libevent_core.lalrwxrwxrwx. 1 root root 26 Jun 13 22:15 libevent_core.so - libevent_core-2.0.so.5.1.9lrwxrwxrwx. 1 root root 27 Jun 13 22:15 libevent_extra-2.0.so.5 - libevent_extra-2.0.so.5.1.9-rwxr-xr-x. 1 root root 405038 Jun 13 22:15 libevent_extra-2.0.so.5.1.9-rw-r--r--. 1 root root 593776 Jun 13 22:15 libevent_extra.a-rwxr-xr-x. 1 root root 977 Jun 13 22:15 libevent_extra.lalrwxrwxrwx. 1 root root 27 Jun 13 22:15 libevent_extra.so - libevent_extra-2.0.so.5.1.9-rwxr-xr-x. 1 root root 935 Jun 13 22:15 libevent.lalrwxrwxrwx. 1 root root 30 Jun 13 22:15 libevent_pthreads-2.0.so.5 - libevent_pthreads-2.0.so.5.1.9-rwxr-xr-x. 1 root root 18446 Jun 13 22:15 libevent_pthreads-2.0.so.5.1.9-rw-r--r--. 1 root root 18686 Jun 13 22:15 libevent_pthreads.a-rwxr-xr-x. 1 root root 998 Jun 13 22:15 libevent_pthreads.lalrwxrwxrwx. 1 root root 30 Jun 13 22:15 libevent_pthreads.so - libevent_pthreads-2.0.so.5.1.9lrwxrwxrwx. 1 root root 21 Jun 13 22:15 libevent.so - libevent-2.0.so.5.1.9我们可以看到有以下一些库。libevent_core所有核心的事件和缓冲功能包含了所有的event_base、evbuffer、bufferevent和工具函数。 libevent_extra定义了程序可能需要也可能不需要的协议特定功能包括HTTP、DNS和RPC。 libevent这个库因为历史原因而存在它包含libevent_core和libevent_extra的内容。不应该使用这个库未来版本的libevent可能去掉这个库。 libevent_pthreads添加基于pthread可移植线程库的线程和锁定实现。它独立于libevent_core这样程序使用libevent时就不需要链接到pthread除非是以多线程方式使用libevent。 三、libevent的功能。Libevent提供了事件通知io缓存事件定时器超时异步解析dns事件驱动的http server以及一个rpc框架。事件通知当文件描述符可读可写时将执行回调函数。Io缓存缓存事件提供了输入输出缓存能自动的读入和写入用户不必直接操作io。定时器libevent提供了定时器的机制能够在一定的时间间隔之后调用回调函数。信号触发信号执行回调。异步的dns解析libevent提供了异步解析dns服务器的dns解析函数集。事件驱动的http服务器libevent提供了一个简单的可集成到应用程序中的HTTP服务器。RPC客户端服务器框架libevent为创建RPC服务器和客户端创建了一个RPC框架能自动的封装和解封数据结构。四、Reactor(反应器)模式libevent是一个典型的reactor模式的实现。这里做一下简单介绍我们知道普通的函数调用机制如下程序调用某个函数函数执行程序等待函数将结果返回给调用程序如果含有函数返回值的话也就是顺序执行的。而Reactor模式的基本流程如下应用程序需要提供相应的接口并且注册到reactor反应器上如果相应的事件发生的话那么reactor将自动调用相应的注册的接口函数类似于回调函数通知你所以libevent是事件触发的网络库。四、编译运行一个小例子[cpp] view plain copy #include stdlib.h #include stdio.h #include sys/time.h #include event.h // 定时事件回调函数 void onTime(int sock, short event, void *arg) { printf(Hello,World!\n); struct timeval tv; tv.tv_sec 1; tv.tv_usec 0; // 重新添加定时事件定时事件触发后默认自动删除 event_add((struct event*)arg, tv); } int main() { // 初始化 event_init(); struct event ev_time; // 设置定时事件 evtimer_set(ev_time, onTime, ev_time); struct timeval tv; tv.tv_sec 1; tv.tv_usec 0; // 添加定时事件 event_add(ev_time, tv); // 事件循环 event_dispatch(); return 0; } gcc example1.c -o example1 -levent大功告成就可以在终端看到每隔两秒输出Hello,World!了。