当前位置:网站首页>Advanced file IO of system programming (13) -- IO multiplexing - Select
Advanced file IO of system programming (13) -- IO multiplexing - Select
2022-04-23 11:19:00 【Light chasing rain】
List of articles
One 、IO Multiplexing
IO Multiplexing is IO Blocked state , Do something else
Two 、select
Realization function : monitor ; Can listen to many file descriptors
Prototype :int select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
fd_set: File descriptor set , A container , It can save a lot of data ( File descriptor ) Array of
maxfd: Range of file descriptors , Larger than the maximum file descriptor to be checked 1
readfds: Read monitored file descriptor set
writefds: The set of file descriptors that are written to monitor
exceptfds: Set of file descriptors monitored by exceptions
timeval: System defined time
Return value , The number of changes returned successfully 、 Failure to return -1
Generally used to monitor reading
#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; // Temporary deposit all_set
while (1)
{
r_set = all_set; // Set every time , Otherwise, the one who will only monitor the change every time
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;
}
attach 、 Some additions
fd_set……

timeval
int select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
( Baidu explains )
Parameter one : Maximum file descriptor plus 1.
Parameter two : Used to check readability ,
Parameter 3 : Used to check Writeability ,
Parameter 4 : Used to check out of band data ,
Parameter 5 : A pointer to a timeval Pointer to structure , Used to decide select wait for I/o The longest time , When you're done, come straight back . If it is empty, it will wait (NULL).
timeval Definition of structure :
struct timeval
{
long tv_sec; // seconds second
long tv_usec; // microseconds millisecond
}

版权声明
本文为[Light chasing rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231115179178.html
边栏推荐
猜你喜欢

采用百度飞桨EasyDL完成指定目标识别

Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)

分享两个实用的shell脚本

MIT: label every pixel in the world with unsupervised! Humans: no more 800 hours for an hour of video

Using Baidu PaddlePaddle EasyDL to accomplish specified target recognition

Excel·VBA自定义函数获取单元格多数值

年度最尴尬的社死瞬间,是Siri给的

ConstraintLayout布局

About the three commonly used auxiliary classes of JUC

赛微微电科创板上市破发:跌幅达26% 公司市值44亿
随机推荐
详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
MySQL面试题讲解之如何设置Hash索引
Interprocess communication -- message queue
Excel·VBA自定义函数获取单元格多数值
How to quickly query 10 million pieces of data in MySQL
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
mysql插入datetime类型字段不加单引号插入不成功
Mba-day5 Mathematics - application problems - engineering problems
学习 Go 语言 0x05:《Go 语言之旅》中映射(map)的练习题代码
MySQL8. 0 upgraded stepping on the pit Adventure
GPU, CUDA,cuDNN三者的关系总结
MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了
Mysql8. 0 installation guide
Alarm scene recognition
学习 Go 语言 0x07:《Go 语言之旅》中 Stringer 练习题代码
Jupyter lab top ten high productivity plug-ins
Share two practical shell scripts
得物技术网络优化-CDN资源请求优化实践
QT 怎么把QWigdet变成QDialog
Cygwin 中的 rename 用法