当前位置:网站首页>系统编程之高级文件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
边栏推荐
- Interprocess communication -- message queue
- C语言之结构体(进阶篇)
- remote: Support for password authentication was removed on August 13, 2021.
- Database management software sqlpro for SQLite for Mac 2022.30
- Prevent SQL injection in web projects
- Mysql中有关Datetime和Timestamp的使用总结
- MySQL8. 0 upgraded stepping on the pit Adventure
- Microsoft Access database using PHP PDO ODBC sample
- Visualization Road (11) detailed explanation of Matplotlib color
- 第六站神京门户-------手机号码的转换
猜你喜欢

Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)

精彩回顾|「源」来如此 第六期 - 开源经济与产业投资

Introduction to neo4j authoritative guide, recommended by Qiu Bojun, Zhou Hongxiang, Hu Xiaofeng, Zhou Tao and other celebrities

Constraintlayout layout

Canvas详解

vm设置静态虚拟机

Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)

Interprocess communication -- message queue

R-drop: a more powerful dropout regularization method

使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
随机推荐
About the three commonly used auxiliary classes of JUC
得物技术网络优化-CDN资源请求优化实践
软件测试人员,如何优秀的提Bug?
Interprocess communication -- message queue
Mysql系列SQL查询语句书写顺序及执行顺序详解
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
Visual solutions to common problems (VIII) mathematical formulas
MySQL对数据表已有表进行分区表的实现
Typora operation skill description (I) md
Which company is good for opening futures accounts? Who can recommend several safe and reliable futures companies?
Visual common drawing (V) scatter diagram
Implementation of partition table of existing data table by MySQL
MySQL数据库事务transaction示例讲解教程
remote: Support for password authentication was removed on August 13, 2021.
MySQL failed to insert the datetime type field without single quotation marks
妊娠箱和分娩箱的区别
Detailed introduction to paging exploration of MySQL index optimization
讯飞2021年营收183亿:同比增41% 净利为15.56亿
Mba-day5 Mathematics - application problems - engineering problems