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. pwnable.kr-shellshock-witeup

    思路是:发现文件执行没什么好反馈显示结果的,于是看文件和权限,通过bash文件猜测可能存在破壳漏洞(CVE-2014-6271)漏洞,于是利用它并结合文件权限成功获得flag. 通过scp下载文件至本 ...

  2. a标签包裹div的问题

    示例代码 1 <a href="#"> 2 <div> 3 <a href="#"></a> 4 </di ...

  3. spring怎么避免循环依赖

    1.循环依赖 (1)概念 对象依赖分为强依赖和弱依赖: 强依赖指的是一个对象包含了另外一个对象的引用,例如:学生类中包含了课程类,在学生类中存在课程类的引用 创建课程类: @Data public c ...

  4. NMAP类型题目 (escapeshellarg,escapeshellcmd使用不当)

    [BUUCTF 2018]Online Tool 给出了源码 审计 <?php if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $_SERVER[' ...

  5. 用ThreadLocal来优化下代码吧

    最近接手了一个老项目,看到一个很有意思的现象. 这个项目中大量的方法入参都会带上user信息,比如这样 它的意图是希望在方法内使用user的信息,但是如此大范围的传递用户信息,第一感觉就是不优雅.那有 ...

  6. 18-SE-你说的都队

    文章目录 前言 建设银行app分析 招商银行app分析 中国银行app分析 工商银行app分析 总结 团队成员分工与评分 前言 18-SE-你说的都队所选项目题目为"村镇银行储蓄业务系统开发 ...

  7. mac操作liunx

    mkdir demo //创建一个文件夹 touch index.html // 创建一个html文件 rm rouch index.html //删除找个index.html文件 rmdir dem ...

  8. CentOS7 系统安全的设置

    一.禁止root远程直接登录 # 创建普通用户,并设置密码 useradd bluceli #新建账户 passwd bluceli #设置密码 # 不允许root远程直接登录 vim /etc/ss ...

  9. 用 shell 脚本做 restful api 接口监控

    问题的提出 基于历史原因,公司有一个"三无"采集服务--无人员.无运维.无监控--有能力做的部门不想接.接了的部门没能力.于是就一直这样裸奔,直到前几天一个依赖于这个采集服务的大数 ...

  10. devops-jenkins-Pipeline基础语法

    1. jenkins-Pipeline基础语法  1) jenkins-Pipeline总体介绍 • Pipeline,简而言之,就是一套运行与jenkins上的工作流框架,将原本独立运行于单个或多个 ...