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

What is the difference between fork, select, poll and epoll in the server

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail what is the difference between fork, select, poll and epoll in the server. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Three socket server working modes of fork, select and epoll are used. The client sends any data to the server, and the server returns to the client as is. The purpose of this paper is to deepen my memory.

Fork: after each accept to a socket, start a child process to take charge of the transceiver processing. Select

Select, which first appeared in 4.2BSD in 1983, monitors an array of multiple file descriptors through a select () system call. When select () returns, the ready file descriptors in the array are modified by the kernel so that the process can obtain these file descriptors for subsequent read and write operations.

Select is currently supported on almost all platforms, and its good cross-platform support is also one of its advantages, which is actually one of its few remaining advantages.

One drawback of select is that there is a maximum limit on the number of file descriptors that a single process can monitor, typically 1024 on Linux, although this limit can be raised by modifying macro definitions or even recompiling the kernel.

In addition, the data structure maintained by select () stores a large number of file descriptors, and its replication overhead increases linearly with the increase in the number of file descriptors. At the same time, due to the delay of network response time, a large number of TCP connections are inactive, but calling select () will do a linear scan of all socket, so this also wastes some overhead.

Poll

Poll, which was born in System V Release 3 in 1986, is not much different from select in nature, but poll has no limit on the maximum number of file descriptors.

Poll and select also have the disadvantage that an array containing a large number of file descriptors is copied as a whole between the user state and the kernel address space, and its overhead increases linearly with the number of file descriptors, regardless of whether these file descriptors are ready or not.

In addition, after select () and poll () tell the process the ready file descriptor, if the process does not IO it, these file descriptors will be reported again the next time select () and poll () are called, so they generally do not lose the ready message, which is called Level Triggered.

Epoll

It is not until Linux2.6 that there is an implementation method directly supported by the kernel, that is, epoll, which has almost all the advantages mentioned before, and is recognized as the best multi-channel I _ ready notification method under Linux2.6.

Epoll can support both horizontal and edge triggering (Edge Triggered, which only tells the process which file descriptors have just become ready, and it only says once that if we don't take action, it won't tell us again, this way is called edge triggering). In theory, edge triggering is more performance, but the code implementation is quite complex.

Epoll also tells only those ready file descriptors, and when we call epoll_wait () to get the ready file descriptor, we return not the actual descriptor, but a value that represents the number of ready descriptors. You only need to go to an array specified by epoll to get the corresponding number of file descriptors in turn. Memory mapping (mmap) technology is also used here. This completely saves the overhead of copying these file descriptors during system calls.

Another essential improvement is that epoll adopts event-based readiness notification. In select/poll, the kernel scans all monitored file descriptors only after the process calls a certain method, and epoll registers a file descriptor through epoll_ctl () in advance. Once the file descriptor is ready based on a file descriptor, the kernel uses a callback mechanism similar to callback to activate the file descriptor quickly and is notified when the process calls epoll_wait ().

This is the end of the article on "what's the difference between fork, select, poll and epoll in the server". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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

Servers

Wechat

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

12
Report