Linux-计算机网络-Libevent入门

1.libevent概述

libevent是一个开源的轻量级的IO框架库,它底层封装了select,poll,epoll三种方法,为用户提供了一个便于使用的接口

2.libevent特点

(1)跨平台支持。 Libevent 支持 Linux、 Unix 和 Windows.

(2)统一事件源。 Libevent 对 I/0 事件、信号和定时事件提供统一的处理。

(3)线程安全。 Libevent 使用 libevent pthreads 库来提供线程安全支持。

(4)基于 Reactor 模式的实现。

简单来讲,就是主线程只负责事件的一个检测,主要看有没有产生事件,然后由工作线程对具体的事

件进行处理;

3.libevent安装

可以进行源码安装,但是那么源码安装就有可能会出现多个error,需要百度安装多个库,每个学生的系统的版本可能不同,依赖的库也有可能是不同的,那么缺什么库下载什么库就可以了;(平台的原因)所以这里只讲述Ubun下的安装

(1)sudo apt install libevent-2.1-7(后面是版本号)
(2)sudo apt install libevent-dev(安装有关开发的库)

(3)检验是否安装成功:

Is -al /usr/lib |grep libevent(看看是否安装成功,或者ls /usr/liblgrep event find /-name event

或者这样查找:

Is -al /usr/include grep event

4.libevent示例

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
#include<event.h>
#include<assert.h>

void signal_cb(int fd,short event,void *argc)
{
    
    
         printf("sig=%d\n",fd);

    

}

void timeout_cb(int fd,short event,void *argc)
{
  
    printf("time out\n");
    
}

int main()
{
    struct event_base *base=event_init();
    assert(base!=NULL);

    struct event *sig_ev=evsignal_new(base,SIGINT,signal_cb,NULL);
    //struct event *sig_ev=event_new(base,SIGINT,EV_SIGNAL|EV_PERSIST,signal_cb,NULL);
    assert(sig_ev!=NULL);
    event_add(sig_ev,NULL);

     struct event *timeout_ev=evtimer_new(base,timeout_cb,NULL);
    //struct event* timeout_ev=event_new(base,-1,EV_TIMEOUT|EV_PERSIST,timeout_cb,NULL);
    struct timeval tv={3,0};

    event_add(timeout_ev,&tv);

    event_base_dispatch(base);

    event_free(sig_ev);
    event_free(timeout_ev);
    event_base_free(base);
    
    exit(0);
}

5.libevent使用模型,事件类型及框架结构

(1)libevent使用模型

(2)libevent支持的事件类型

Libevent 支持的事件类型 :有定时事件有读事件,写事件,信号事件,永久事件,当IO方法支持ET模式

时,Libevent也可以使用ET模式:

(3)libevent的框架结构

没有设置永久事件,那么只响应一次,以后就不再响应了,
设置了永久事件,底层的I0复用方法会反复检测,而不是只检测一次,当然也就不会只响应一次了;

6.事件类型的应用

引自高性能服务器编程239页:

cpp 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
#include<event.h>
#include<assert.h>

//回调函数改为
void signal_cb(int fd,short event,void *argc)
{
    if(event & EV_SIGNAL)
    {
         printf("sig=%d\n",fd);

    }

}

void timeout_cb(int fd,short event,void *argc)
{
    if(event & EV_TIMEOUT)
    {
    printf("time out\n");
    }
}

int main()
{
    struct event_base *base=event_init();
    assert(base!=NULL);

    //struct event *sig_ev=evsignal_new(base,SIGINT,signal_cb,NULL);改为:
    struct event *sig_ev=event_new(base,SIGINT,EV_SIGNAL|EV_PERSIST,signal_cb,NULL);
    
    assert(sig_ev!=NULL);
    event_add(sig_ev,NULL);

    // struct event *timeout_ev=evtimer_new(base,timeout_cb,NULL);改为:
     struct event* timeout_ev=event_new(base,-1,EV_TIMEOUT|EV_PERSIST,timeout_cb,NULL);
    
    struct timeval tv={3,0};

    event_add(timeout_ev,&tv);

    event_base_dispatch(base);

    event_free(sig_ev);
    event_free(timeout_ev);
    event_base_free(base);
    
    exit(0);
}
相关推荐
Maple_land13 分钟前
Linux进程第八讲——进程状态全景解析(二):从阻塞到消亡的完整生命周期
linux·运维·服务器·c++·centos
嵌入式分享15 分钟前
嵌入式分享#41:RK3576改UART波特率【精简版】
linux·嵌入式硬件·ubuntu·嵌入式
爱吃生蚝的于勒17 分钟前
【Linux】零基础学会Linux之权限
linux·运维·服务器·数据结构·git·算法·github
量子物理学19 分钟前
Eclipse Mosquitto 在小内存下怎么修改配置文件
java·服务器·eclipse
惜.己25 分钟前
linux中jenkins正常启动外部无法访问
linux·servlet·jenkins
Cyan_RA936 分钟前
Linux 远程Ubuntu服务器本地部署大模型 EmoLLM 中常见的问题及解决方案 万字详解
linux·运维·服务器·ubuntu·大模型·远程部署·emollm
数字冰雹38 分钟前
图观 流渲染打包服务器
服务器·前端·github·数据可视化
minji...1 小时前
Linux相关工具vim/gcc/g++/gdb/cgdb的使用详解
linux·运维·服务器·c++·git·自动化·vim
web安全工具库1 小时前
Linux 高手进阶:Vim 核心模式与分屏操作详解
linux·运维·服务器·前端·数据库
egoist20231 小时前
[linux仓库]信号产生[进程信号·贰]
linux·键盘·系统调用·信号产生·软件条件