Linux c libevent库监听管道读写(mkfifo)

1、libevent库使用基本流程

安装: Linux c libevent库安装(简单使用)

1、创建 底座 
struct event_base* event_base_new(void);

2、创建常规事件
回调函数类型:
	typedef void(*event_callback_fn)(evutil_socket_t fd,short,void*)

struct event* event_new(struct event_base* base,evutil_socket_t fd,short what,event_callback_fn cb,void *arg);
参数:
	base:event_base_new返回值
	fd:需要绑定的文件描述符
	what:对应事件(r、w、e)
        EV_READ 一次读事件
        EV_WRTIE 一次写事件
        EV_PERSIST  持续触发,结合event_base_dispatch函数使用生效
    cd:一旦事件满足监听条件、回调的函数
	arg:回调函数参数

3、添加事件
int event_add(struct event* ev,const struct timeval* tv);
参数:
    ev:event_new()的返回值
    tv:
    	NULL不会超时(一直等到事件被触发,回调函数会被调用)
        为非0等待期间,检查事件没有被触发,时间到,回调函数依旧会被调用

4、循环监听事件满足
int event_base_dispatch(struct event_base* base);
参数:
	base:event_base_new 返回值

5、释放
int event_free(struct event* ev);
参数:
	ev:event_new 事件
	
void event_base_free(struct event_base* base);
参数:
	base:event_base_new

2、管道通信代码

管道读端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <event2/event.h>

void sys_err(const char* str)
{
    perror(str);
    exit(1);
}


// 回调函数
void read_cb(evutil_socket_t fd,short what,void* arg)
{
    char buf[1024] = {0};
    int len = read(fd,buf,sizeof(buf));
    printf("read event:%s\n",what&EV_READ?"yes":"NO");
    printf("data len = %d,buf=%s\n",len,buf);
    sleep(1);
}

int main(int argc,char* argv[])
{
    unlink("myfifo");
    mkfifo("myfifo",0644);  // 创建管道
    int fd = open("./myfifo",O_RDONLY|O_NONBLOCK);
    if(fd == -1)
    {
        sys_err("open error");
    }
    struct event_base* base = event_base_new();

    // 创建事件 持续读事件
    struct event* ev = event_new(base,fd,EV_READ|EV_PERSIST,read_cb,NULL);

    // 添加事件
    event_add(ev,NULL);

    // 事件循环
    event_base_dispatch(base);  // while(1){epoll}

    // 释放
    event_free(ev);
    event_base_free(base);
    close(fd);
    return 0;
}

管道写端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <event2/event.h>

void sys_err(const char* str)
{
    perror(str);
    exit(1);
}


// 回调函数
void write_cb(evutil_socket_t fd,short what,void* arg)
{
    char buf[1024];
    static int num = 0;
    sprintf(buf,"hello %d\n",num++);
    int len = write(fd,buf,strlen(buf)+1);

    sleep(1);
}

int main(int argc,char* argv[])
{
    // unlink("myfifo");
    // mkfifo("myfifo",0644);  // 创建管道
    int fd = open("./myfifo",O_WRONLY|O_NONBLOCK);
    if(fd == -1)
    {
        sys_err("open error");
    }
    struct event_base* base = event_base_new();

    // 创建事件
    struct event* ev = event_new(base,fd,EV_WRITE|EV_PERSIST,write_cb,NULL);

    // 添加事件
    event_add(ev,NULL);

    // 事件循环
    event_base_dispatch(base);  // while(1){epoll}

    // 释放
    event_free(ev);
    event_base_free(base);
    close(fd);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讳疾忌医丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值