Table of contents
int epoll_ctl(int epfd, int op, struct epoll_event *event)
int epoll_wait (int epfd, struct epoll_event *events, int maxevents , int timeout);
3. The working principle of epoll
1. Historical positioning
So far, the IO multiplexing model with the best performance on the linux platform. none of them.
2. Interface
1. Create
int epoll_create(int size);
size : It is currently deprecated, but for backward compatibility, you need to pass a number greater than 0 to size
What is the meaning of epoll_create?
Return value: epoll operation handle
With the operation handle, you can manipulate the created structure eventpoll
2. Operation:
int epoll_ctl(int epfd, int op, struct epoll_event *event)
parameter:
epfd : epoll operation handle
op: options, through different ops, tell the epoll_ctl function what to do
- EPOLL_CTL_ADD
- Add an event structure corresponding to a file descriptor to epoll
- EPOLL_CTL_MOD
- Modify the event structure of a file descriptor
- EPOLL_CTL_DEL
- Delete the event structure corresponding to a file descriptor from epoll
fd: the file descriptor to be processed (add, modify, delete), this fd is to tell the epoll_ctl function
event: event structure in epoll
The events in the event structure event corresponding to the file descriptor are the events we want to care about. If there are multiple events at the same time, we also use or to connect.
return value:
0 is normal.
-1 for failure.
3. Wait:
int epoll_wait (int epfd, struct epoll_event *events, int maxevents , int timeout);
parameter:
epfd :
The operation handle of epoll
events :
Event structure array (collection), get the ready event structure from epoll
maxevents :
Maximum number of event structures to fetch at one time
timeout :
- >0 : with timeout event
- ==O: non-blocking
- <0: blocking
3. The working principle of epoll
Four, epoll simple server multi-channel transfer server
Here we can first encapsulate a tcp class for the server to provide tcp services for the server.
Then let's implement an epoll monitoring
Let's verify it:
When we run the code, we can see that multiple clients connect to the server and send messages to it, and the server can receive it.