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);
}
相关推荐
strongwyy1 小时前
9、nRF52xx蓝牙学习(pca10056.h学习)
单片机·嵌入式硬件·学习
每天题库1 小时前
2025 年江苏保安员职业资格考试经验分享
学习·安全·考试·题库·考证
Starry_hello world4 小时前
Linux 的准备工作
linux·笔记·有问必答
Rverdoser5 小时前
服务器(一种管理计算资源的计算机)
运维·服务器
_考不上研究生不改名5 小时前
【完美解决】VSCode连接HPC节点,已配置密钥却还是提示需要输入密码
linux·服务器·vscode·远程连接·hpc·超算集群
_长银6 小时前
Vim搜索和替换
linux·编辑器·vim
viperrrrrrrrrr76 小时前
大数据学习(105)-Hbase
大数据·学习·hbase
IT _oA6 小时前
Active Directory 域服务
运维·服务器·网络·windows·笔记
·云扬·7 小时前
【BUG】阿里云服务器数据库远程连接报错
服务器·阿里云·bug
MXsoft6187 小时前
云原生运维在 2025 年的发展蓝图
运维·服务器·数据库