当前位置:网站首页>系统编程之高级文件IO(十三)——IO多路复用-select
系统编程之高级文件IO(十三)——IO多路复用-select
2022-04-23 11:15:00 【光追雨】
一、IO多路复用
IO多路复用就是IO被阻塞状态下,做其他事情
二、select
实现功能:监听;可以监听很多的文件描述符
原型:int select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
fd_set:文件描述符集合,一个容器,可以保存很数据(文件描述符)的数组
maxfd:文件描述符的范围,比待检的最大文件描述符大1
readfds:被读监控的文件描述符集
writefds:被写监控的文件描述符集
exceptfds:被异常监控的文件描述符集
timeval:系统定义时间
返回值,成功返回变化的个数、失败返回-1
一般用来监听读
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
int main(int argc, char const *argv[])
{
struct timeval time;
fd_set r_set;
fd_set all_set;
time.tv_sec = 3;
time.tv_usec = 0;
int fd;
if((fd = open("/dev/input/mouse0", O_RDWR | O_CREAT, 0655)) < 0)
{
perror("open file error!");
exit(1);
}
FD_SET(fd, &r_set);
FD_SET(0, &r_set);
all_set = r_set; //暂存如all_set
while (1)
{
r_set = all_set; //每次都进行置位,不然之后每次只会监听变化的那个
int ret = select(fd + 1, &r_set, NULL, NULL, NULL );
if (FD_ISSET(fd, &r_set) > 0)
{
int cor;
read(fd, &cor, sizeof(cor));
printf("cor = %d\n", cor);
}
if (FD_ISSET(0, &r_set) > 0)
{
char buffer[1024];
read(0, buffer, sizeof(buffer));
printf("buffer = %s\n", buffer);
}
}
return 0;
}
附、一些补充
fd_set……
timeval
int select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
(百度解释)
参数一:最大的文件描述符加1。
参数二:用于检查可读性,
参数三:用于检查可写性,
参数四:用于检查带外数据,
参数五:一个指向timeval结构的指针,用于决定select等待I/o的最长时间,等完直接回来。如果为空将一直等待(NULL)。
timeval结构的定义:
struct timeval
{
long tv_sec; // seconds秒
long tv_usec; // microseconds毫秒
}
版权声明
本文为[光追雨]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_52592798/article/details/124348977
边栏推荐
- 语雀文档编辑器将开源:始于但不止于Markdown
- Database management software sqlpro for SQLite for Mac 2022.30
- 活动进行时! 点击链接加入直播间参与“AI真的能节能吗?”的讨论吧!
- MySQL interview questions explain how to set hash index
- CUMCM 2021-B:乙醇偶合制备C4烯烃(2)
- 防止web项目中的SQL注入
- mysql插入datetime类型字段不加单引号插入不成功
- 小程序 支付
- Detailed explanation of MySQL creation stored procedure and function
- Gets the current time in character format
猜你喜欢
Visualization Road (10) detailed explanation of segmentation canvas function
STM32接电机驱动,杜邦线供电,然后反烧问题
GO接口使用
VM set up static virtual machine
初探 Lambda Powertools TypeScript
More reliable model art than deep learning
Visualization Road (11) detailed explanation of Matplotlib color
VIM + ctags + cscope development environment construction guide
Visual common drawing (IV) histogram
Upgrade the functions available for cpolar intranet penetration
随机推荐
关于JUC三大常用辅助类
When the activity is in progress! Click the link to join the live studio to participate in "can AI really save energy?" Let's have a discussion!
VM set up static virtual machine
闹钟场景识别
Excel · VBA custom function to obtain multiple cell values
MBA - day5 mathématiques - Questions d'application - Questions d'ingénierie
软件测试人员,如何优秀的提Bug?
FileProvider 路径配置策略的理解
Visual common drawing (I) stacking diagram
Detailed explanation of integer data type tinyint in MySQL
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
得物技术网络优化-CDN资源请求优化实践
Mba-day5 Mathematics - application problems - engineering problems
ConstraintLayout布局
Prevent SQL injection in web projects
PDMS软光刻加工过程
MySQL数据库10秒内插入百万条数据的实现
进程间通信 -- 消息队列
Visual common drawing (III) area map
Gets the current time in character format