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 poll Omuri-multiplexing

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

Share

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

A slightly improved Multiplexing-poll

Poll is a slightly improved multiplexing version of select, which is soon replaced by the fully functional epoll. The difference between select and select is that the return of select is returned by input parameters, so that each time the file descriptor of interest is added to its file descriptor set, and poll sets a structure pointer parameter, through which he can know the state change, but it is inevitable that the entire buf still needs to be traversed. Its size can be customized by users, but too large collection will greatly increase the burden on the server, and too small collection because most of the socket are waiting for data to arrive, the efficiency will decline a lot, so poll is very embarrassing.

The function of poll

# include int poll (struct pollfd * fds, nfds_t nfds, int timeout); struct pollfd {int fd; / * file descriptor * / short events; / * requested events * / short revents; / * returned events * /}

What distinguishes poll's function from select is its fds parameter. It is a pointer to a structure, and the definition of a structure is the following.

Struct pollfd, where events is the time you should care about when setting. Revents is the output that indicates that the situation set by event has occurred when it is greater than zero. Revents does not need to be emptied manually, it will be emptied automatically.

Because it's similar to select, let's just look at the code.

Here is a simple poll version of the tcp server implemented

Because the previous setting listening socket is the same as the previous one, it is ignored.

43 void PollServer (int sock) 44 {45 int itemo; 46 int max_fd=-1; 47 struct pollfd fds [_ MAX_FDS_]; 48 for (; i0) 104 {105 buf [size] ='\ 0potential printf ("client::%s\ n", buf) Else if (size==0) {108 close (fd); 109 FDS [I] .fd =-1 × 110 FDS [I] .events =-1 11112112perror ("read"); 113continue;114} 115116117118} 119,120} 121l} else

There was not much difference between poll and select, so it was quickly eliminated by epoll.

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

Wechat

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

12
Report