当前位置:网站首页>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完成指定目标识别
- R-drop: a more powerful dropout regularization method
- Mysql database transaction example tutorial
- MIT: label every pixel in the world with unsupervised! Humans: no more 800 hours for an hour of video
- 学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
- 微型机器人的认知和研发技术
- Summary of the relationship among GPU, CUDA and cudnn
- Laravel admin time range selector daterange default value problem
- Prevent SQL injection in web projects
- Jupyter Lab 十大高生产力插件
猜你喜欢

Résumé de la relation entre GPU, cuda et cudnn

Promise详解

After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before

MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了

Share two practical shell scripts

Canvas详解

nacos基础(8):登录管理

laravel编写Console脚本

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

qt 64位静态版本显示gif
随机推荐
SVN的使用:
妊娠箱和分娩箱的区别
Redis optimization series (II) redis master-slave principle and master-slave common configuration
实践数据湖iceberg 第三十课 mysql->iceberg,不同客户端有时区问题
微型机器人的认知和研发技术
Software testers, how to mention bugs?
MySQL interview questions explain how to set hash index
Pytorch neural network trainer
Detailed explanation of integer data type tinyint in MySQL
Understanding of fileprovider path configuration strategy
QT 怎么把QWigdet变成QDialog
Excel·VBA数组冒泡排序函数
C语言之结构体(进阶篇)
Detailed introduction to paging exploration of MySQL index optimization
Learning go language 0x02: understanding slice
nacos基础(9):nacos配置管理之从单体架构到微服务
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!
GPU, CUDA,cuDNN三者的關系總結
Explain in detail the pitfalls encountered in DTS due to the time zone problems of timestamp and datetime in MySQL
学习 Go 语言 0x06:《Go 语言之旅》中 斐波纳契闭包 练习题代码