阻塞, 非阻塞,多路复用, epoll
1 bio
2
The problem is too man thread will make too much swap between kernal space and userspace which is not efficient.
3 nio
The problem is kernal needs go through all the fds in every loop; and read function need to be called all the time;
4
problem: every loop, it pass all fds to kernel and the kernal run select need to go through all files with O(N). System need check all fds which of them are flgged
select can do max 1024 socket by defailt it is becase fe_set itis a bitmap struct and it default lenght is 1024 bit.
select
5
a more advanced version of select is epoll
it use call-back function to understand if a fd has been modified rather than go though the all fds ;
epoll_creat retrun fd id of epoll;
epoll_ctl (int epfd; int fd) // epfd is the epoll fd, fd is the one you want to monitor
epoll_wait (int epdf; epoll_evet *event) // collect those dfs which has state change
Diff between select and poll . Poll slove the problem of 1024 limit of select.
Diff between select and epoll
A typical server might be dealing with, say, 200 connections. It will service every connection that needs to have data written or read and then it will need to wait until there's more work to do. While it's waiting, it needs to be interrupted if data is received on any of those 200 connections.
With select
, the kernel has to add the process to 200 wait lists, one for each connection. To do this, it needs a "thunk" to attach the process to the wait list. When the process finally does wake up, it needs to be removed from all 200 wait lists and all those thunks need to be freed.
By contrast, with epoll
, the epoll
socket itself has a wait list. The process needs to be put on only that one wait list using only one thunk. When the process wakes up, it needs to be removed from only one wait list and only one thunk needs to be freed.
To be clear, with epoll
, the epoll
socket itself has to be attached to each of those 200 connections. But this is done once, for each connection, when it is accepted in the first place. And this is torn down once, for each connection, when it is removed. By contrast, each call to select
that blocks must add the process to every wait queue for every socket being monitored.
Ironically, with select
, the largest cost comes from checking if sockets that have had no activity have had any activity. With epoll
, there is no need to check sockets that have had no activity because if they did have activity, they would have informed the epoll
socket when that activity happened. In a sense, select
polls each socket each time you call select
to see if there's any activity while epoll
rigs it so that the socket activity itself notifies the process.