When a process calls the epoll_create method, the Linux kernel creates an eventpoll struct, which has two Members closely related to epoll usage.
Struct eventpoll
{
Struct rb_root RBR; // root node of the red/black tree, which stores all the events added to epoll, that is, the epoll monitoring event.
Struct list_head rdllist; // two-way linked list rdllist stores the events that meet the conditions to be returned to the user through epoll_wait
};
Each epoll object has an independent eventpoll structure. This structure creates an independent memory in the kernel space to store events added to the epoll object using the epoll_ctl method. These events are all mounted to the RBR red/black tree. In this way, repeated events can be efficiently identified by the red/black tree.
When an event occurs, the callback method is called here. This callback method is called ep_poll_callback in the kernel. It stores such an event in the preceding rdllist bidirectional linked list. When epoll_wait is called to check whether there is an event connection, it only checks whether the rdllist bidirectional linked list in the eventepoll object has an epitem element. If the rdllist linked list is not empty, copy the event to the user memory and return the event count to the user.