当前位置: 首页 > news >正文

Linux:基于libevent读写管道代码,改进一下上一篇变成可以接收键盘输入

对上一篇进行改进,变成可以接收键盘输入,然后写入管道:

读端代码:

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

void read_cb(evutil_socket_t fd, short what, void* arg) {
//把管道中的数据读出到buf中,然后打印出来
char buf[1024] = {0};
int len = read(fd,buf,sizeof(buf));
printf(“data len= %d , buf= %s\n”,len,buf);
printf(“read event: %s\n”, what & EV_READ ? “YES” : “NO”);
}

int main(int argc, const char* argv[])
{
//先删除同名管道,再创建管道
unlink(“myfifo”);
mkfifo(“myfifo”, 0664);

int fd = open("myfifo", O_RDONLY | O_NONBLOCK);
if(-1 == fd) {perror("create fifo pipe fail");exit(1);
}//创建事件框架,事件的根结点
struct event_base* base = NULL;
base = event_base_new();//创建事件
struct event* ev = NULL;
ev = event_new(base, fd, EV_READ | EV_PERSIST, read_cb, NULL);//往事件框架中添加事件ev
event_add(ev,NULL);//分发事件
event_base_dispatch(base);//释放资源
event_free(ev);
event_base_free(base);
close(fd);
return 0;

}

写端代码:

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

void read_terminal(int fdd, short what, void * arg)
{
//先读终端fdd的内容到buf中,在输出到pipe中
char buf[1024] = {0};
int len = read(fdd, buf, sizeof(buf));

int fd = *(int *)arg;
write(fd, buf, strlen(buf)+1);

}

void write_cb(evutil_socket_t fd, short what, void* arg) {
//把数据写到buf中,然后写到管道中,然后打印出来
// char buf[1023] = {0};
// static int num = 0;
//sprintf(buf, “hello world == %d \n”, num++);
//write(fd, buf, strlen(buf)+1);
//printf(“pipe can be write\n”);

}

int main(int argc, const char* argv[])
{
//管道已经在read_fifo.c中创建管道

int fd = open("myfifo", O_WRONLY | O_NONBLOCK);
if(-1 == fd) {perror("create fifo pipe fail");exit(1);
}//创建事件框架,事件的根结点
struct event_base* base = NULL;
base = event_base_new();//创建事件
struct event* ev = NULL;
ev = event_new(base, fd, EV_WRITE | EV_PERSIST, write_cb, base);//往事件框架中添加事件ev
event_add(ev,NULL);//pipe可写,创造终端事件,把它加入到事件框架
struct event* stdin_ev = event_new(base, STDIN_FILENO, EV_READ | EV_PERSIST, read_terminal,&fd);
event_add(stdin_ev, NULL);	//分发事件
event_base_dispatch(base);//释放资源
event_free(ev);
event_base_free(base);
close(fd);
return 0;

}

写管道的结果:

在这里插入图片描述

读管道的结果:

在这里插入图片描述

http://www.lryc.cn/news/16257.html

相关文章:

  • C语言格式化输出总结:%d,%c,%s,%f, %lf,%m.nd,%m.nf,%m.ns 以及sprintf函数
  • Nginx之反向代理、负载均衡、动静分离。
  • 0401不定积分的概念和性质-不定积分
  • 数组中的各种迭代API方法手写
  • 详解量子计算:相位反冲与相位反转
  • C++——C++11第三篇
  • 180 2 22222
  • 成人高考初中毕业能报名吗 需要什么条件
  • ChatGPT初体验
  • ChatGPT概念狂飙!究竟魅力何在?
  • 如何下载阅读Spring源码-全过程详解
  • 学了两个月的Java,最后自己什么也不会,该怎么办?
  • 前端vue实现获取七天时间和星期几功能
  • zookeeper单机部署
  • 单片机输入输出模式
  • 数据结构_ 堆结构与堆排序(c++ 实现 + 完整代码 )
  • 【MySQL】sql中explain解释和应用
  • 从零实现深度学习框架:Seq2Seq从理论到实战【实战篇】
  • 【数据结构入门】-链表之单链表(1)
  • Docker竟如此简单!
  • 在外包干了几年,感觉自己都快费了
  • Java实现多线程有几种方式(满分回答)
  • 实例4:树莓派GPIO控制舵机转动
  • 【音视频处理】为什么MP3不是无损音乐?音频参数详解,码率、采样率、音频帧、位深度、声道、编码格式的关系
  • Linux 环境变量
  • 从功能测试(点点点)到进阶自动化测试,实现薪资翻倍我只用了3个月时间
  • aspnetcore 原生 DI 实现基于 key 的服务获取
  • 华为OD机试 -最大子矩阵和(Python) | 机试题+算法思路+考点+代码解析 【2023】
  • C2驾照科一学习资料(1)
  • 4576: 移动数组元素