IO Multiplexing of Redis
1. IO reuse function of linux
IO operations with multiple descriptors in the same thread can be performed concurrently and sequentially.
Epoll provides only three functions:
Int epoll_create (int size); # create an epoll handle
Int epoll_ctl (int epfd, int op, int fd, struct epoll_event * event)
Associate listening events with epool handles (you can add, delete, and modify associations through the op parameter)
The first parameter, epfd, is the return value of epoll_create ()
The second parameter, op, indicates the association: EPOLL_CTL_ADD (add) EPOLL_CTL_MOD (change) EPOLL_CTL_DEL (delete)
The third parameter, fd, is the fd to be listened to.
The fourth parameter, event, tells the kernel what specific events to listen for: EPOLLIN (readable) EPOLLOUT (writable) EPOLLPRI (emergency readable) EPOLLERR (error) EPOLLHUP (hang up) EPOLLET (trigger mode ET or LT) EPOLLONESHOT (listen only once)
You can set the callback function corresponding to the event here.
Int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout)
If no listening event is generated in epoll_ctl, it will block here until the listening event occurs, or until the timeout occurs.
If there is an event listening in the epoll_ctl, the specific event handling callback function is executed.
Epoll ensures that each fd is copied only once throughout the process, from kernel space to user space.
2. IO Multiplexing of redis
Redis uses epoll to achieve IO multiplexing, putting connection information and events into a queue, then putting them into a file event dispatcher, which distributes events to event handlers.
Event handlers handle different events.
Main-> aeMain-> while {aeProcessEvents-> aeApiPoll-> epoll_wait}