1.创建 eevent_base

  struct event_base *base = event_base_new();

2.创建 事件event

  struct event *ev;

  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:绑定到event上的文件描述符

  what:对应的事件(r、w、e)

    EV_READ           一次 读事件

    EV_WRITE         一次 写事件

    EV_PERSIST     持续触发。结合 event_base_dispatch 函数使用,生效。

    cb:一旦事件满足监听条件,回调的函数。

    typedef void (*event_callback_fn)(evutil_socket_t fd,  short,  void *)

    arg:回调的函数的参数。

    返回值:成功创建的event

    

 3.添加事件到 event_base

   int event_add(struct event *ev, const struct timeval *tv);

   ev: event_new() 的返回值。

   tv:NULL

   从event_base上摘下事件 【了解】

   int event_del(struct event *ev);

   ev: event_new() 的返回值。

 4.销毁事件

  int event_free(struct event *ev);

  ev: event_new() 的返回值。

操作管道代码:

read管道代码

 1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <event2/event.h>
9
10 // 对操作处理函数
11 void read_cb(evutil_socket_t fd, short what, void *arg)
12 {
13 // 读管道
14 char buf[1024] = {0};
15
16 int len = read(fd, buf, sizeof(buf));
17
18 printf("read event: %s \n", what & EV_READ ? "Yes" : "No");
19 printf("data len = %d, buf = %s\n", len, buf);
20
21 sleep(1);
22 }
23
24
25 // 读管道
26 int main(int argc, const char* argv[])
27 {
28 unlink("myfifo");
29
30 //创建有名管道
31 mkfifo("myfifo", 0664);
32
33 // open file
34 //int fd = open("myfifo", O_RDONLY | O_NONBLOCK);
35 int fd = open("myfifo", O_RDONLY);
36 if(fd == -1)
37 {
38 perror("open error");
39 exit(1);
40 }
41
42 // 创建个event_base
43 struct event_base* base = NULL;
44 base = event_base_new();
45
46 // 创建事件
47 struct event* ev = NULL;
48 ev = event_new(base, fd, EV_READ | EV_PERSIST, read_cb, NULL);
49
50 // 添加事件
51 event_add(ev, NULL);
52
53 // 事件循环
54 event_base_dispatch(base); // while(1) { epoll();}
55
56 // 释放资源
57 event_free(ev);
58 event_base_free(base);
59 close(fd);
60
61 return 0;
62 }

write管道代码

 1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <event2/event.h>
9
10 // 对操作处理函数
11 void write_cb(evutil_socket_t fd, short what, void *arg)
12 {
13 // write管道
14 char buf[1024] = {0};
15
16 static int num = 0;
17 sprintf(buf, "hello,world-%d\n", num++);
18 write(fd, buf, strlen(buf)+1);
19
20 sleep(1);
21 }
22
23
24 // 写管道
25 int main(int argc, const char* argv[])
26 {
27 // open file
28 //int fd = open("myfifo", O_WRONLY | O_NONBLOCK);
29 int fd = open("myfifo", O_WRONLY);
30 if(fd == -1)
31 {
32 perror("open error");
33 exit(1);
34 }
35
36 // 写管道
37 struct event_base* base = NULL;
38 base = event_base_new();
39
40 // 创建事件
41 struct event* ev = NULL;
42 // 检测的写缓冲区是否有空间写
43 //ev = event_new(base, fd, EV_WRITE , write_cb, NULL);
44 ev = event_new(base, fd, EV_WRITE | EV_PERSIST, write_cb, NULL);
45
46 // 添加事件
47 event_add(ev, NULL);
48
49 // 事件循环
50 event_base_dispatch(base);
51
52 // 释放资源
53 event_free(ev);
54 event_base_free(base);
55 close(fd);
56
57 return 0;
58 }

未决和非未决:

  非未决: 没有资格被处理

  未决: 有资格被处理,但尚未被处理

  event_new --> event ---> 非未决 --> event_add --> 未决 --> dispatch() && 监听事件被触发 --> 激活态

  --> 执行回调函数 --> 处理态 --> 非未决 event_add && EV_PERSIST --> 未决 --> event_del --> 非未决

Libevent库基础(1)的更多相关文章

  1. Libevent库基础(2)

    带缓冲区的事件 bufferevent #include <event2/bufferevent.h> read/write 两个缓冲. 借助 队列. 创建.销毁bufferevent: ...

  2. Libevent库学习笔记

    Libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,Libevent在底层select.pool.kqueue和epoll等机制基础上,封装出一致的事件接口.可 ...

  3. libevent库介绍--事件和数据缓冲

    首先在学习libevent库的使用前,我们还要从基本的了解开始,已经熟悉了epoll以及reactor,然后从event_base学习,依次学习事件event.数据缓冲Bufferevent和数据封装 ...

  4. Windows平台下libevent库的使用

    1     引子 手头上有一个使用了4个年头的HttpClient库,自己封装的,对于集成了IE浏览器的应用程序很友好.但最近想把产品扩展到Chrome和FireFox阵营,萌发了重构HttpClie ...

  5. C10K问题和Libevent库介绍

    http://blog.chinaunix.net/uid-20761674-id-75056.html 一.C10K的问题 C10K的问题在上个世纪90年代就被提出来了.大概的意思是当用户数超过1万 ...

  6. R1:创建Libevent库

    原文链接:http://www.wangafu.net/~nickm/libevent-book/Ref1_libsetup.html Setting up the Libevent library ...

  7. python linecache标准库基础学习

    #python标准库基础之:linecacge:高效读取文本文件#说明与作用"""可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件 ...

  8. C 标准库基础 IO 操作总结

    其实输入与输出对于不管什么系统的设计都是异常重要的,比如设计 C 接口函数,首先要设计好输入参数.输出参数和返回值,接下来才能开始设计具体的实现过程.C 语言标准库提供的接口功能很有限,不像 Pyth ...

  9. libevent库简单使用

    一.libevent库简介 Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络库.Libevent有几个显著的亮点: (1)事件驱动(event-dr ...

随机推荐

  1. 从watevrCTF-2019:Pickle Store中学习python之pickle序列化漏洞

    从watevrCTF-2019:Pickle Store中学习python之pickle序列化漏洞 pickle提供了一个简单的持久化功能.可以将对象以文件的形式存放在磁盘上. 其本质是Picklin ...

  2. 阿里内部推出Spring响应式微服务Boot2Cloud文档

    今天要给大家推荐的是Spring响应式微服务SpringBoot2+Spring5+SpringCloud实战的内容,将从目录.主要内容和面向的读者三部分给大家介绍,希望大家能够喜欢!!!(本文整理自 ...

  3. 我给VSCode报了个bug,微软工程师竟然凌晨回复了...

    柠檬哥整理了50本计算机相关的电子书,关注公众号「后端技术学堂」,回复「1024」即可获取,回复「进群」拉你进读者技术交流群. 本文首发个人微信公众号,欢迎围观点击阅读原文 最近遇到一个有意思的bug ...

  4. java调用matlab生成exe文件

    一.Matlab生成Java Package 1.在MATLAB的Command Window输入deploytool命令,选择Library Compiler. 2.在弹出的窗口选择Java Pac ...

  5. Centos-当前登录用户信息- w who

    w who 显示当前登录系统的用户,但w显示的更为详细 who 相关参数 # 默认输出 用户名.登录终端.登录时间 -a 列出所有信息 -b    系统最近启动日期 -m   当前终端信息,相当于 w ...

  6. PCIE_DMA实例五:基于XILINX XDMA的PCIE高速采集卡

    PCIE_DMA实例五:基于XILINX XDMA的PCIE高速采集卡 一:前言 这一年关于PCIE高速采集卡的业务量激增,究其原因,发现百度"xilinx pcie dma",出 ...

  7. MATLAB中conv2的详细用法 (以及【matlab知识补充】conv2、filter2、imfilter函数原理)

    转载: 1.https://blog.csdn.net/jinv5/article/details/52874880 2.https://blog.csdn.net/majinlei121/artic ...

  8. VS2013中带命令行参数的调试方法---C++

    今天先记录一下(也是传说中大神喜欢装逼的comment line)c++中向主函数int main(int argc,char** argv )传递4中方法,欢迎添加新方法, 然后可以参考别人写的很好 ...

  9. I2C的库函数应用示例

    I2C  Arduino 简单应用举例   例1  多机通信 主机代码:(从编译器串口监视器发送数字1,2,3,4来控制从机的LED亮与灭) 1 #include <Wire.h> 2 v ...

  10. js日志输出还是只会console.log么,那你就out了

    几乎所有的javascript开发者最常使用的日志打印调试api都是console.log(),其实还有很多的选项供我们选择,笔者下面就为大家一一介绍. 一.console.table() conso ...