当前位置:网站首页>Blocking, non blocking, IO multiplexing select\poll\epoll
Blocking, non blocking, IO multiplexing select\poll\epoll
2022-06-28 04:49:00 【Lzg_ n】
Preface
linux There are five kinds of io Model , Respectively :
1. Blocking
2. Non blocking
3.io Multiplexing
4. Event driven
5. asynchronous
One 、 Blocking IO
The first mode is blocking , It means that the user performs IO Blocked during operation ,

The server sends out read request ,read Is to see if there is client The user made a data request . issue read Then the operation is handed over to the kernel , Will be waiting for the requested data to return , The server can continue to perform the following operations .
Two 、 Non blocking
In order to solve the problem that the blocking mode has been waiting , Can be socket Set to NONBLOCK,
When the user level thread of the server sends a request, it will not be blocked even if there is no data , It's about going back to , But it needs to be felt constantly read request , Know that the data is read , The advantage of this pattern over non blocking is simply that you can immediately go back and do something else , But for the data read It needs to be sent all the time . Consume a lot CPU resources .
3、 ... and 、 IO Multiplexing
What are the main disadvantages of the above model , High concurrency is not possible , Because querying a network IO when , If there is no data, it will be blocked here all the time , Or use the polling method to query all the time , When there are other network requests , How can the server discover and handle it in time ?
This requires IO Multiplexing technology , The three methods should be introduced one by one :
select
sockfd=socket(AF_INET,SOCK_STREAM,0);
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr,sin_port=htons(2000);
addr.sin_addr.s_addr=INADDR_ANY;
bind(sockfd,(struct sockaddr*)&addr,sizeof(addr));
listen(sockfd,5);
for(int i=0;i<5;++i){
memset(&client,0,sizeof(client));
addrlen=sizeof(client);
fds[i]=accept(sockfd,(struct sockfd,(struct sockaddr*)&client,&addrlen));
if(fds[i]>max)
max=fds[i];
}
//--------------------------------------------------------------------------------------//
while(1){
FD_ZERO(&rset);
for(int i=0;i<5;++i){
FD_SET(fds[i],&rset);
}
puts("round again");
select(max+1,&rest,NULL,NULL,NULL);// Inquire about IO
// Reading data
for(int i=0;i<5;++i){
if(FD_ISSET(fds[i],&rset)){
memset(buffer,0,MAXBUF);
read(fds[i],buffer,MAXBUF);
puts(buffer);
}
}
} Above the dotted line are all networks scoket Set up ,fds Arrays are what we store 5 individual client Port number ,rset It's a bitmap Data of type , If client The port numbers of are :1、2、5、7、9, that FD_SET Is to put the corresponding rset Set up :
0110010101000...The point is select function , How does he work ? In fact, threads will block select here , But different from the above blocking ,select Cycle pair 5 Access ports to see if there are requests ,
1、 If not, it will be blocked all the time select here , Always query ;
2、 If you have any , This time f The query loop has the requested corresponding flag bit Set up , Then return ;
shortcoming :
1、 Passed in rset Parameters need to be copied from user mode into kernel mode ;
2、rset This type of bitmap yes 1024 position ;
3、rset Assign values again after each use ;
4、select After returning or O(n) Time complexity traversal rset, See which has a request ;
poll
poll Instead of using bitmap The way of setting ,
struct pollfd{
int fd;
short events;
short revents;
};for(int i=0;i<5;++i){
memset(&client,0,sizeof(client));
addrlen=sizeof(client);
pollfds[i]=accept(sockfd,(struct sockfd,(struct sockaddr*)&client,&addrlen));
pollsfds[i].events=POLLIN;
}
sleep(1);
while(1){
puts("round again");
poll(pollfds,5,5000);
// Reading data
for(int i=0;i<5;++i){
if(FD_ISSET(fds[i],&rset)){
memset(buffer,0,MAXBUF);
read(fds[i],buffer,MAXBUF);
puts(buffer);
}
}
} pollfd Structure of the fd Used to store port number ,events Used to set whether to read or output ,revents It is used to record whether the port has data requests ;
This method is similar to select, Just use structure instead of bitmap, take revents Set to judge , It's just solved bitmap Can only 1024 A place , And the operations that need to be re assigned after each return .
epoll
for(int i=0;i<5;++i){
memset(&client,0,sizeof(client));
addrlen=sizeof(client);
pollfds[i]=accept(sockfd,(struct sockfd,(struct sockaddr*)&client,&addrlen));
pollsfds[i].events=POLLIN;
}
sleep(1);
while(1){
puts("round again");
poll(pollfds,5,5000);
// Reading data
for(int i=0;i<5;++i){
if(FD_ISSET(fds[i],&rset)){
memset(buffer,0,MAXBUF);
read(fds[i],buffer,MAXBUF);
puts(buffer);
}
}
} epfd Is a blank area of memory requested ,epoll_event The structure array of is used to represent the status of each port , All port statuses will be recorded in epfd in ;
When there is a data request , It is still the setting of the corresponding structure , But after setting, it will be rearranged and placed at the head of the team , So from epoll There is no need to read data after returning O(N) The traversal ;
In addition, the epfd It is shared in user mode and kernel mode , No copy operation is required .
边栏推荐
- Necessary skills for test and development: actual combat of security test vulnerability shooting range
- CUPTI error: CUPTI could not be loaded or symbol could not be found.
- OracleData安装问题
- Web3来临时的风口浪尖
- Matlab exercises -- basic data processing
- 政策利好,20多省市开启元宇宙发展规划
- Database garbled
- Simple factory mode
- [csp-j2020] excellent splitting
- 为什么大厂不让使用undefined
猜你喜欢

100+ data science interview questions and answers Summary - machine learning and deep learning
![[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]](/img/73/1e4c605991189acc674d85618cf0ef.png)
[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]

Huawei's 9-year experience as a software testing director

With favorable policies, more than 20 provinces and cities have launched the yuanuniverse development plan

多线程实现 重写run(),怎么注入使用mapper文件操作数据库

Mise en place d'un cadre d'essai d'automatisation de l'interface utilisateur - - rédaction d'une application d'automatisation
![[csp-j2020] excellent splitting](/img/05/90f9cf71791b3cdc37073eaf5db989.png)
[csp-j2020] excellent splitting

华为9年经验的软件测试总监工作感悟—写给还在迷茫的朋友

Function and working principle of controller

Multi thread implementation rewrites run (), how to inject and use mapper file to operate database
随机推荐
LeetCode 88:合并两个有序数组
测试开发必备技能:安全测试漏洞靶场实战
Multithreading and high concurrency six: source code analysis of thread pool
Detailed reading of the thesis: implementing volume models for handowriting text recognition
几百行代码实现一个脚本解释器
A bit of knowledge - online resources on Chinese Learning
Play with double pointer
在线直播源码,JS动态效果之,侧边栏滚动固定效果
Live online source code, JS dynamic effect, sidebar scrolling fixed effect
S32ds jump to defaultisr
[matlab traffic light identification] traffic light identification [including GUI source code 1908]
Database garbled
JS逆向之巨量星图sign签名
Source code of live video system, countdown display, countdown of commodity spike
云厂商为什么都在冲这个KPI?
请问下,mysqlcdc设置多并行度的话,增量数据是不是会重复?
Aspnetcoreratelimit rate limit interface access limit current limit control
What to do when MySQL changes the password and reports an error
Severe tire damage: the first rock band in the world to broadcast live on the Internet
Severe tire damage: the first rock band in the world to broadcast live on the Internet