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. Java基础系列-Optional

    原创文章,转载请标注出处:https://www.cnblogs.com/V1haoge/p/10755368.html 一.概述 Optional的引入是为了解决null的问题,那么到底是解决nul ...

  2. 听说这四个概念,很多 Java 老手都说不清

    Java 是很多人一直在用的编程语言,但是有些 Java 概念是非常难以理解的,哪怕是一些多年的老手,对某些 Java 概念也存在一些混淆和困惑. 所以,在这篇文章里,会介绍四个 Java 中最难理解 ...

  3. Spring学习(六)--Spring的IOC

    1.autowiring(自动依赖装配)的实现 自动装配中不需要对Bean属性做显示的依赖管理方式,只需要配置好autowiring的属性就可以,IOC容器会自动根据这个属性的配置通过反射自动找到属性 ...

  4. Python-如何在一个for循环中迭代多个可迭代对象?

    案例: 某班学生期末考试成绩,语文.数学.英语分别存储在3个列表中,同时迭代三个列表.,计算每个学生的总分(并行) 某年级有4个班,某次英语成绩分别记录在4个列表中,依次迭代每个列表,统计全年级高于9 ...

  5. 依赖注入在 dotnet core 中实现与使用:4. 集成 Autofac

    本示例使用 .net core 5 rc-1 实现. 1. 添加 Nuget 包引用 使用 Autofac 当然要添加 Autofac 的 Nuget 包,主要涉及到两个: Autofac.Exten ...

  6. Python基本数据类型详细介绍

    Python提供的基本数据类型主要有:布尔类型.整型.浮点型.字符串.列表.元组.集合.字典等等 1.空(None)表示该值是一个空对象,空值是Python里一个特殊的值,用None表示.None不能 ...

  7. matlab中nargin函数输入参数数目

    来源:https://ww2.mathworks.cn/help/matlab/ref/nargin.html?searchHighlight=nargin&s_tid=doc_srchtit ...

  8. USB口,串口,以太网口简介

    USB口 一.什么是USB? USB是英文Universal Serial Bus的缩写,中文含义是"通用串行总线".它是一种应用在PC领域的新型接口技术.早在1995年,就已经有 ...

  9. day63:Linux:nginx基础知识&nginx基础模块

    目录 1.nginx基础知识 1.1 什么是nginx 1.2 nginx应用场景 1.3 nginx组成结构 1.4 nginx安装部署 1.5 nginx目录结构 1.6 nginx配置文件 1. ...

  10. Java变量命名前俩个字母仅含有一个大写字母的坑

    背景 前几周在做项目fetch切换,即将HttpUtils调用改成使用Feign调用.大概代码如下: // 原代码 String resultJson = HttpUtil.get(url + &qu ...