当前位置:网站首页>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
边栏推荐
- Mysql8. 0 installation guide
- An interesting interview question
- CUMCM 2021-B:乙醇偶合制备C4烯烃(2)
- MySQL sorting feature details
- The songbird document editor will be open source: starting with but not limited to markdown
- MySQL partition table can be classified by month
- Résumé de la relation entre GPU, cuda et cudnn
- Detailed explanation of integer data type tinyint in MySQL
- Jupyter lab top ten high productivity plug-ins
- Mysql系列SQL查询语句书写顺序及执行顺序详解
猜你喜欢
系统编程之高级文件IO(十三)——IO多路复用-select
Canvas详解
Structure of C language (Advanced)
nacos基础(9):nacos配置管理之从单体架构到微服务
Using Baidu PaddlePaddle EasyDL to accomplish specified target recognition
初探 Lambda Powertools TypeScript
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
SVN的使用:
redis优化系列(二)Redis主从原理、主从常用配置
Mysql8. 0 installation guide
随机推荐
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
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!
remote: Support for password authentication was removed on August 13, 2021.
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
Go interface usage
My creation anniversary
Get things technology network optimization - CDN resource request Optimization Practice
初探 Lambda Powertools TypeScript
学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
Excel · VBA custom function to obtain multiple cell values
MySQL partition table can be classified by month
Analysis on the characteristics of the official game economic model launched by platoffarm
分享两个实用的shell脚本
Cygwin 中的 rename 用法
实践数据湖iceberg 第三十课 mysql->iceberg,不同客户端有时区问题
Learning go language 0x08: practice using error in go language journey
26. Delete duplicates in ordered array
C#的学习笔记【八】SQL【一】
CUMCM 2021-B:乙醇偶合制備C4烯烴(2)
MySQL分区表实现按月份归类