Address: http://blog.endlesscode.com/2010/03/27/select-poll-epoll-intro/by Stephen posted
On 2010/03/27
I do not know much about select, poll, and epoll. The following is an introduction from building a high-performance web site, and so on.Select
Select first appeared in. It monitors arrays of multiple file descriptors through a select () system call. When select () returns, the ready file descriptor in the
Linux network protocol stack is very independent, up and down through two interfaces, respectively, and the user state and device connected, can also be considered as the North and South interface ... North through the socket interface, south through the Qdisc interface (you can think of the upper Netdev queue, for the receiving interface, NAPI poll queue is another example), whether the socket or QDISC, is based on the queue to manage, that is, Three
copying data from the kernel to the process (copying the data from the kernel to the)The LInux IO mode is divided into 5 types:Blocking io (blocking IO)Non-blocking IO (non-blocking io)IO multiplexing (IO multiplexing)Signal-driven IO (signal driven IO) (not commonly used)Asynchronous IO (Asynchronous IO)Blocking io (blocking IO):In Linux, by default, all sockets are blocking. This is illustrated below:There are two stages mentioned above, and in blocking IO, both phases are blocked.Non-blockin
Using the select function can handle the problem of multi-socket connections (for the usage of select, see: http://blog.csdn.net/zhandoushi1982/article/details/5070107), the same function can be implemented using poll, and the call method is simpler. The prototype is:
Struct pollfd {int FD; // file descriptor short events; // The event mask short revents that requires query; // The returned event mask}; int poll
On this topic, the online has been introduced more, here is just a flowchart to make a simple and clear comparison, convenient to distinguish.First, select/poll realization mechanismCharacteristics:1.select/poll each time it needs to pass all the listening fd in, involving the user space and the direct data copy of the kernel.2.FD Event callback function is Pollwake, just wake up the process, this will need
21122123124125126
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "read_write.h"#include "sysutil.h" typedef std::vector /* The biggest advantage over select and Poll,epoll is that it will not reduce efficiency with the increase in the number of FD concerns.int main (void){int count = 0;int LISTENFD;if ((LISTENFD = socket (pf_inet, Sock_stream,
, readset );If (select (maxfd + 1, readset, null, null) =-1) (errno! = Eintr ))/* Deal with Error */Else {If (fd_isset (fd1, readset)/* fd_isset (2) check whether the specified descriptor is set *//* Get and process data * // * the problem here is that when processing the data, the process will be blocked here, and there is no way to overlap the processing of data on other ports */If (fd_isset (fd2, readset ))/* Get and process */}}
Poll
Due to the limitations of Poll () and select (), the 2.6 kernel introduces the event poll (epoll) mechanism. Although a little complicated, epoll solves their common basic performance problems and adds some new features.
Each call of Poll () and select () requires all the file descriptors to be listened on. The kernel must traverse all monitored file descri
Event Poll epoll details, pollepoll
Due to the limitations of poll () and select (), the 2.6 kernel introduces the event poll (epoll) mechanism. Although a little complicated, epoll solves their common basic performance problems and adds some new features.
Each call of poll () and select () requires all the fil
The poll function is similar to the Select function. The following is a simple echo server#include using namespacestd; #include#include#defineOpen_max 64intMain () {intI, Maxi, LISTENFD, CONNFD, SOCKFD; intNready; ssize_t N; Socklen_t Clilen; structPOLLFD Client[open_max]; structsockaddr_in cliaddr, servaddr; LISTENFD= Socket (Af_inet, Sock_stream,0); Servaddr.sin_family=af_inet; Servaddr.sin_port= Htons (10002); Servaddr.sin_addr.s_addr= Inet_a
In this article, I'll use three asynchronous Conferencing--select, poll and Epoll on serial port to transmit data betwee n PC and Raspberry Pi.
Outline
Character device file of serial port
Naive Serial Communication
Asynchronous conferencing
Select
Poll
Epoll
Character Device of Serial portMy device is Raspberry pi with the Debian system and PC with ubuntu12.
Practice Topics:Solving:Method One:1. Poll Main Page:2. Processing the polling page:3. Establish a class to access the database, encapsulated for reference: Method Two:1. Poll Main Page:2. Processing the polling page:3. View the poll results page: Page Display results: PHP Query Poll exercise (0508)
AJAX PollsIn the following example, we will demonstrate a voting program through which the poll results are displayed without refreshing the page.Example explanation-HTML pageWhen the user selects an option above, a function named "Getvote ()" is executed. This function is triggered by an "onclick" event:
Id="Poll"> Do I like PHP and AJAX so far? Yes: Type="Radio" Name="Vote" Value="0" OnClick="G
Offer,add differences:Some queues have a size limit, so if you want to add a new item to a full queue, the extra items will be rejected.At this point the new offer method will work. It does not throw a unchecked exception to the call to the Add () method, but only gets the false returned by the offer ().Poll,remove differences:Both the Remove () and poll () methods remove the first element from the queue. R
structure, which is defined as follows:struct Timeval {Long tv_sec;Seconds long tv_usec;microseconds};If this parameter is not NULL, the Select () call will return after tv_sec seconds and tv_usec microseconds, even if there are no file descriptors ready for I/O. If both values of timeout are set to zero, the call returns immediately, reporting whether any events await processing when this call is made, but does not wait for any subsequent events.Fd_set Writefds;Fd_zero (writefds);1#include 2#i
When learning about Linux concurrent non-blocking servers. See how you can use the poll function. Preliminary understanding and writing procedures were tested.Server:#include #include/*Basic system data Types*/#include/*Basic Socket Definitions*/#includeinch.h>/*sockaddr_in{} and other Internet Defns*/#include/*inet (3) Functions*/#include#include#include#include#includestring.h>#defineMax_link 10intMain () {structsockaddr_in server_addr,client_addr;
1. User space Invocation (ref. poll (2)-Linux man page)int poll (struct POLLFD *fds, nfds_t nfds, int timeout);It waits for one of a set of the file descriptors to become ready to perform I/O.The set of file descriptors to be monitored are specified in the fds argument, which are an array of structures of The following form:struct POLLFD { int FD; /* */short events; /* */short rev
Turn from: http://blog.csdn.net/tianmohust/article/details/6677985
i). Epoll Introduction
Epoll is currently in Linux under the development of large-scale concurrent network programs popular candidates, Epoll in the Linux2.6 kernel formally introduced, and select similar, in fact, I/O multiplexing technology just , and nothing mysterious. In fact, the design of concurrent network programs in Linux, has always been a lack of methods, such as the typical Apache model (Process per Connection, refe
block state and re-runs it.Therefore, the blocking IO is characterized by block in both phases of IO execution.2.2 Non-blocking I/O (nonblocking IO)Under Linux, you can make it non-blocking by setting the socket. When you perform a read operation on a non-blocking socket, the process looks like this:When the user process issues a read operation, if the data in the kernel is not yet ready, it does not block the user process, but returns an error immediately. From the user process point of view,
sockets, so this also wastes some overhead.PollPoll was born in 1986 in System V Release 3, and it does not differ substantially from select in nature, but poll does not have a limit on the maximum number of file descriptors .The disadvantage of poll and select is that an array containing a large number of file descriptors is copied in between the user state and the kernel's address space, regardless of wh
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.