Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Advanced epoll Omuri-multiplexing

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Epoll for multiplexing

As the most efficient way of multiplexing, I _ Universe _ Epoll has many capabilities that select and poll do not have.

Unlike poll and select,epoll, it uses three functions to implement multiplexing.

# include int epoll_create (int size); / / is used to create a storage space for epoll mode. The return value is a file descriptor, which is used later in the function and / / in the function. Int epoll_ctl (int epfd, int op, int fd, struct epoll_event * event); / / epoll_ctl is used to add an event to epfd. Op is represented by EPOLL_ADD,EPOLL_DEL / / EPOLL_MOD, fd indicates the file descriptor you want to add, followed by a structure pointer, and / / the structure will be described below. Int epoll_wait (int epfd, struct epoll_event * events,int maxevents, int timeout); / / epoll_wait is used to wait for the occurrence of the event. This structure pointer stores the returned fd,maxevents / / indicating the maximum number of fd that can be received. Note that the number of fd that can be received is large (look up some data this data / / there can be about 100000 on a machine with 1G memory). Typedef union epoll_data {void * ptr; int fd; _ _ uint32_t U32; _ _ uint64_t U64;} epoll_data_t; struct epoll_event {_ _ uint32_t events; / * Epoll events * / epoll_data_t data / * User data variable * /}; / / this structure contains a union and an events,events is used to describe the trigger state, / / can be set to EPOLLIN,EPOLLOUT,EPOLLD and so on. / / We focus on fd and * ptr in the consortium because sometimes bug is generated in the consortium. If we use str to / / store the read data, we can make the ptr point to a structure in which the / / fd and * buf parameters are set.

There are several main reasons why epoll is more efficient than previous multiplexing.

1 >: the organization of epoll, which is a red-black tree with two very efficient structures, list linked list

The red-black tree is used to store fd. Once an event occurs, it can be found with O (1) time complexity and

Put it in list so that the return value is the pointer to the event structure, which is stored in the

That is, the fd of the event, which is reduced from the previous multiplexed rotation O (N) to O (1).

Where it's efficient.

2 >: trigger mode: epoll can use two trigger methods to obtain events; the level described below

Trigger and edge trigger. Using edge triggering can make epoll more efficient.

Edge trigger and horizontal trigger

Horizontal trigger PT:epoll_ wait once a state change occurs in the fd, it is assumed that the state change is read, and if it is not finished, it will be reminded again next time until the data in the buffer fd is read.

Edge trigger ET:epoll_wait when the state changes in the fd, assuming that the state change is read, it will remind you for the first time, but will not remind you later if you haven't finished reading it, and will not read it back until new data arrives.

Under epoll_wait, if you want to set the socket to non-blocking with the fcntl function, why set it to non-blocking? After thinking about it for a long time, I finally found that in an ET, because the data in the buffer is not guaranteed to be read thoroughly, while the sock in blocking mode is guaranteed to read the data in the fd, the contradiction between the two will lead to the rejection of the new fd.

What ET needs to use is a custom read function, as shown below:

57 int read_fd (int sock,char * buf,int size) 58 {59 int _ size=-1; 60 int index=0; 61 while ((_ size=read (sock,buf+index,size-index) 62 {63 if (_ size)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report