select、poll和epoll形象化理解與對比
- 一、引入問題
- 二、形象化解釋select、poll、epoll
- 三、select、poll、epoll“監控”步驟
- 四、圖解select、poll、epoll多路復用實作并發服務器
- 五、select、poll、epoll使用的頭檔案和函式
- 1)select
- 2)poll
- 3)epoll
- 六、用epoll寫一個簡單的并發服務器程式
- 七、對比總結select、poll、epoll
一、引入問題
現在有一個服務器程式和一個客戶端程式,客戶端程式連接上服務器程式后向服務器發送“Hello server,I am client!”,服務器收到客戶端的連接后給客戶端發送“Hello client,I am server!”,如果是迭代服務器,那么它運行時一旦與一個客戶端程式建立連接,只有處理完這個客戶端的請求(上述程式中是給客戶端發完“Hello client,I am server!”)后才能與下一個客戶端建立連接,也就是說,迭代服務器一次只能處理一個客戶端的請求,
與迭代服務器不同的是,并發服務器一次能處理多個客戶端請求,在上述程式中是服務器可以在非常短的時間內讀取到多個客戶端給它發送的“Hello server,I am client!”,并給多個客戶端發送“Hello client,I am server!”,
我們可以用多行程或多執行緒的方式來實作并發服務器,父行程或父執行緒負責接聽客戶端的連接,然后交給子行程或子執行緒來處理該客戶端的請求,而父行程繼續接聽客戶端連接,但是在非常多的客戶端請求需要服務器處理的情況下,多行程和多執行緒并發服務器非常消耗資源,于是又引入了多路復用來解決這個問題,
二、形象化解釋select、poll、epoll
多路復用有三種方式select、poll、epoll,下面用一個生活中的例子來形象化解釋這三種多路復用的原理,
假如你是便利店老板,需要看顧客是否進店了,要給顧客結賬,還要防止偷竊,這時你只能一直看著門等待顧客進店,然后一直監視顧客的行為,直到顧客結賬出店,你又開始等待下一個顧客,
select相當于一個監控,會替你一直監視進來的每一個顧客(select可以替你監視1024個人),顧客進店、發生偷竊行為或者顧客要結賬時它都會通知你有顧客發生了事件,但是它不會通知你是哪個顧客發生了哪種事件,在沒接到select的通知期間,你就可以摸魚做其他的事,接到select的通知后,你需要一個個地問select是不是XXX人發生了XXX事件,問到以后再處理事件,
poll也是一個監控,它的功能和select相同,但是性能更好,它替你監控的人數沒有限制,但是當你接到poll的通知時仍然需要一個個地問poll是不是XXX人發生了XXX事件,問到以后再處理事件,
epoll也是一個監控,它的功能更強,它不僅監視的人數沒有限制,而且一旦有人進店、結賬或偷竊它就會告訴你是哪個人發生了哪種事件,你就可以直接去處理事件,
三、select、poll、epoll“監控”步驟
無論你是想要select、poll還是epoll幫你監視顧客,讓你可以及時知道店里現在發生了什么事然后去處理,你都需要做三步:
- 一旦有顧客進店,你就要告訴它們要監視這個顧客,要不然它們可不會主動幫你監視
- 你要準備好接收發生事件的通知
- 問它們具體發生了什么事件
接下來就是你去處理對應的事件了,注意:select和poll只會通知你有事件發生,想知道是誰發生了什么事件你還得一個個地問,epoll會主動告訴你是誰發生了什么事件,
換句話說,select和poll會給你一份所有監視顧客的名單,你得對著這個名單問select或poll是不是XXX人發生了XXX事件,然后去處理對應事件;epoll也會給你一份顧客名單,不過這份顧客名單上全部是發生事件的顧客,你拿到名單以后直接按著名單去處理相應事件,
四、圖解select、poll、epoll多路復用實作并發服務器
select、poll、epoll用它們的”監控“功能可以實作多路復用(只一個服務器程式就可以在極短時間內處理多個客戶端請求)優化服務器性能,實作服務器高并發,那它們又是如何應用到服務器上的呢?我們用并發服務器的流程圖和select、poll、epoll實作的迭代服務器流程圖對比來說明這個問題,


五、select、poll、epoll使用的頭檔案和函式
為了方便理解,這里我們仍用“便利店老板看店”的例子來解釋select、poll、epoll相關函式的功能
1)select
#include <sys/select.h>
#include <sys/time.h>
FD_ZERO(fd_set* fds) //清空監視顧客的集合
FD_SET(int fd, fd_set* fds) //讓select監視指定的顧客
FD_ISSET(int fd, fd_set* fds) //用這個函式詢問是不是XXX顧客發生了XXX事件
FD_CLR(int fd, fd_set* fds) //用這個函式可以讓select不再監視指定顧客
int select(int max_fd, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout);//select的通知由這個函式發送
2)poll
#include <poll.h>
struct pollfd
{
int fd;//相當于顧客的身份證,用于標識顧客
short events;//你關心的事件(進店、結賬、偷竊)
short revents;//poll監視到實際發生的事件
} ;
int poll(struct pollfd *fds, nfds_t nfds, int timeout);//poll的通知由這個函式發送
3)epoll
#include <sys/epoll.h>
struct epoll_event
{
uint32_t events; //你關心的事件(進店、結賬、偷竊)
epoll_data_t data; //相當于顧客的身份證,用于標識顧客
};
int epoll_create(int size);//創建一個epoll物件來監視顧客
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev);//用這個函式可以讓epoll監視指定顧客,修改你關心的事件以及讓epoll不再監視某個顧客
int epoll_wait(int epfd, struct epoll_event *evlist, int maxevents, int timeout);//epoll的通知由這個函式發送
六、用epoll寫一個簡單的并發服務器程式
現在我們用epoll多路復用來實作我們最開始提到的服務器程式:客戶端程式連接上服務器程式后向服務器發送“Hello server,I am client!”,服務器收到客戶端的訊息后給客戶端發送“Hello client,I am server!”
服務器端:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/epoll.h>
#define SERVER_PORT 9999
#define MAX_EVENTS 100//epoll回傳發生事件的客戶端的最大數量
int main(char argc, char *argv[])
{
int serv_fd;
int clie_fd;
struct sockaddr_in serv_addr;
struct sockaddr_in clie_addr;
socklen_t cliaddr_len = sizeof(struct sockaddr_in);
int rv = -1;
char buf[1024];
int on = 1;
int epollfd;//epoll物件,用來監視所有服務器指定的客戶端
struct epoll_event event;//用來設定單個客戶端的資訊,包括fd的值和對這個客戶端關心的事件
struct epoll_event event_array[MAX_EVENTS];//用來存放所有發生事件的客戶端的資訊
int events;//發生事件的客戶端的數目
int i;//用來遍歷event_array陣列處理對應事件
/*服務器的初始化*/
serv_fd = socket(AF_INET, SOCK_STREAM, 0);
if(serv_fd < 0)
{
printf("accept socket failure: %s\n", strerror(errno));
rv = -2;
goto cleanup;
}
printf("create socket[%d] successfully!\n",serv_fd);
setsockopt(serv_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVER_PORT);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(serv_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("bind socket[%d] failure: %s\n", serv_fd, strerror(errno));
rv = -3;
goto cleanup;
}
listen(serv_fd, 13);
/*創建epoll物件,來監視所有服務器指定的客戶端*/
epollfd = epoll_create(MAX_EVENTS);
if(epollfd < 0)
{
printf("epoll_create() failure: %s\n", strerror(errno));
rv = -4;
goto cleanup;
}
/*將服務器用于和客戶端建立連接的socket檔案描述符加入epoll物件的監視中,如果它發生事件說明有新的客戶端正在請求建立連接*/
event.data.fd = serv_fd;
event.events = EPOLLIN;//設定對serv_fd關心的事件為是否可讀
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, serv_fd, &event) < 0)//將服務器用于和客戶端建立連接的socket檔案描述符加入epoll物件的監視中
{
printf("epoll add listen socket failure: %s\n", strerror(errno));
rv = -5;
goto cleanup;
}
while(1)
{
printf("\nStart waiting epoll inform...\n");
events = epoll_wait(epollfd, event_array, MAX_EVENTS, -1);//接收epoll監視通知,如果監視的socket檔案描述符有事件發生,函式就會回傳,否則一直堵塞
if(events < 0)
{
printf("epoll failure: %s\n", strerror(errno));
break;
}
else if(events == 0)
{
printf("epoll get timeout\n");
continue;
}
for(i=0; i<events; i++)
{
if((event_array[i].events&EPOLLERR) || (event_array[i].events&EPOLLHUP))
{
printf("epoll_wait get error on fd[%d]: %s\n", event_array[i].data.fd, strerror(errno));
epoll_ctl(epollfd, EPOLL_CTL_DEL, event_array[i].data.fd, NULL);
close(event_array[i].data.fd);
}
if(event_array[i].data.fd == serv_fd)//有新客戶端連接
{
clie_fd = accept(serv_fd, (struct sockaddr*)NULL, NULL);
if(clie_fd < 0)
{
printf("accept new client failure: %s\n", strerror(errno));
continue;
}
event.data.fd = clie_fd;
event.events = EPOLLIN;
if(epoll_ctl(epollfd, EPOLL_CTL_ADD, clie_fd, &event) < 0)
{
printf("epoll add client socket failure: %s\n", strerror(errno));
close(event_array[i].data.fd);
continue;
}
}
else
{//處理已經建立連接的客戶端的事件
rv = read(event_array[i].data.fd, buf, sizeof(buf));
if(rv <= 0)
{
printf("socket[%d] read failure or get disconncet and will be removed.\n",event_array[i].data.fd);
epoll_ctl(epollfd, EPOLL_CTL_DEL, event_array[i].data.fd, NULL);//從epoll物件的監視中移除
close(event_array[i].data.fd);
continue;
}
else
{
printf("socket[%d] read get %d bytes data:%s\n", event_array[i].data.fd, rv, buf);
if(write(event_array[i].data.fd, "Hello client, I am cloud server!", sizeof("Hello client, I am cloud server!")) < 0)
{
printf("socket[%d] write failure: %s\n", event_array[i].data.fd, strerror(errno));
epoll_ctl(epollfd, EPOLL_CTL_DEL, event_array[i].data.fd, NULL);
close(event_array[i].data.fd);
}
}
}
}
}
cleanup:
if(serv_fd > 0)
close(serv_fd);
if(clie_fd > 0)
close(clie_fd);
return rv;
}
客戶端:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <getopt.h>
#define SERVER_PORT 9999
#define SERVER_IP "127.0.0.1"
#define MSG_STR "Hello server, I am client!"
int main(char argc, char *argv[])
{
int conn_fd = -1;
int rv = 0;
char buf[1024];
struct sockaddr_in serv_addr;
conn_fd = socket(AF_INET, SOCK_STREAM, 0);
if (conn_fd < 0)
{
printf("create socket failure: %s\n", strerror(errno));
rv = -2;
goto cleanup;
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVER_PORT);
inet_aton( SERVER_IP, &serv_addr.sin_addr );
if( connect(conn_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("connect to server[%s:%d] failure: %s\n", serv_ip, port, strerror(errno));
rv = -3;
goto cleanup;
}
rv = write(conn_fd, MSG_STR, sizeof(MSG_STR));
if(rv < 0)
{
printf("Write data to server [%s:%d] failure: %s\n", serv_ip, port, strerror(errno));
rv = -4;
goto cleanup;
}
memset(buf, 0, sizeof(buf));
rv = read(conn_fd, buf, sizeof(buf));
if(rv < 0)
{
printf("Read data to server [%s:%d] failure: %s\n", serv_ip, port, strerror(errno));
rv = -5;
goto cleanup;
}
printf("Read %d bytes data from Server: %s\n", rv, buf);
cleanup:
if(conn_fd > 0)
close(conn_fd);
return rv;
}
七、對比總結select、poll、epoll
首先說明,select和poll的用處越來越有限,epoll已經成為了目前實作高性能網路服務器的必備技術,
select的缺點:
- 最多只能監視1024個檔案描述符,雖然可以更改,但select采用輪詢的方式掃描檔案描述符,檔案描述符數量越多,select性能越差,
- 內核/用戶空間記憶體拷貝問題,會產生巨大的開銷,
- 需要遍歷整個檔案描述符陣列才能發現哪些檔案描述符發生了那些事件,
- 如果程式沒有完成對一個就緒檔案描述符事件的處理,那么每次呼叫select都還是會將那些正在處理的檔案描述符通知給行程,
而poll除了沒有最大檔案描述符限制,以上的缺點都存在,
epoll與select、poll的實作機制完全不同,它沒有以上的缺點,可以直接回傳就緒檔案描述符陣列,能顯著提高程式在大量并發連接中只有少量活躍情況下的系統CPU利用率,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/338206.html
標籤:java
上一篇:如何更改日歷的語言環境?
