当前位置:网站首页>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
边栏推荐
- 学习 Go 语言 0x05:《Go 语言之旅》中映射(map)的练习题代码
- Mysql database transaction example tutorial
- qt5.8 64 位静态库中想使用sqlite但静态库没有编译支持库的方法
- 详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
- mysql创建存储过程及函数详解
- Jupyter Lab 十大高生产力插件
- PDMS软光刻加工过程
- 语雀文档编辑器将开源:始于但不止于Markdown
- Detailed explanation of MySQL creation stored procedure and function
- Learn go language 0x04: Code of exercises sliced in go language journey
猜你喜欢
讯飞2021年营收183亿:同比增41% 净利为15.56亿
Introduction to neo4j authoritative guide, recommended by Qiu Bojun, Zhou Hongxiang, Hu Xiaofeng, Zhou Tao and other celebrities
CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
R-drop: a more powerful dropout regularization method
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Excel·VBA自定义函数获取单元格多数值
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Typora operation skill description (I) md
nacos基础(5):nacos配置入门
Summary of the relationship among GPU, CUDA and cudnn
随机推荐
MySQL索引优化之分页探索详细介绍
nacos基础(9):nacos配置管理之从单体架构到微服务
mysql中整数数据类型tinyint详解
Learn go language 0x07: stringer exercise code in go language journey
@valid,@Validated 的学习笔记
My creation anniversary
ConstraintLayout布局
学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
oh-my-lotto
Mba-day6 logic - hypothetical reasoning exercises
Mba-day5 Mathematics - application problems - engineering problems
Excel·VBA数组冒泡排序函数
Mysql database transaction example tutorial
Learn go language 0x05: the exercise code of map in go language journey
Learning go language 0x02: understanding slice
Applet payment
解决 『SunCertPathBuilderException:unable to find valid certification path to requested target』 问题
解读机器人创造出来的艺术
More reliable model art than deep learning