Linux Network IO multiplexing

2019-10-20

Keywords: select and poll


 

In the Linux system, IO total can be divided into the following four categories:

1, blocking IO;

2, non-blocking IO;

3, IO multiplexing;

It allows simultaneous control of the plurality of IO.

4, the IO driver signal;

An asynchronous communication model. The first three are synchronized IO type, only this one is asynchronous type.

 

 

Blocking IO

The so-called blocking IO is when you call the correlation function, the program will run pointer down suspended until the outcome of IO operations returned. In short, I initiated a request IO operation, you have the data returned to me, I'll wait for you data did not have so far to the data.

 

Obstructive IO IO model is the most commonly used, most of the programs are using this model. Sockets used by default is blocking IO mode.

 

Common blocking mode functions are: read, recv, recvfrom, write, send, accept, connect. It should be emphasized, sendto function is non-blocking.

 

 

Non-blocking IO

Non-blocking IO relatively simply. When it initiates an IO request, if the IO data to return results, the absence of data, the program will continue down the pointer, will not die like.

 

We can switch obstructive and non-blocking IO by two functions.

1、fcntl()

Suppose we need to obstructive IO is set to non-blocking IO.

int flag;

flag = fcntl(sockfd, F_GETFL, 0);

flag |= O_NONBLOCK;

fcntl(sockfd, F_SETFL, flag);

This one a go, it will change over to the IO form.

2、ioctl()

int b_on = 1;

ioctl(sock_fd, FIONBIO,  &b_on);

 

 

IO multiplexing

In fact, the use of multi-channel IO C programming select / poll epoll model and model.

 

Select function:

Function is to select the role originally require a separate monitor blocking resources are unified by the select to listen. Whenever the select listens resources fluctuations in the data, select will use polling way to find out which blocked resources data coming. Saved data and the resource fluctuations, and then select the interrupt function blocking state, execute the code behind. select () function has the following prototype:

int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

This function is a blocking function. It represents a wait timeout value 0 when it returns. Waiting for the return value of -1 indicates a failure. Return value greater than 0 indicates successful monitoring signal.

Nfds parameter is the number of fd. Because Linux, file descriptors are always increasing order growth, so this parameter can also be filled in the maximum file descriptor number plus one. I.e. maxfd + 1.

Readfds parameters collection is read.

Writefds is a collection of write parameters.

Exceptfds parameters are abnormal collections.

The timeout parameter is the timeout period.

 

fd_set is a structure defined by the system for storing information of file descriptors, can be set by the following function:

1、void FD_ZERO(fd_set *fdset);

Of the specified collection is cleared.

2、void FD_SET(int fd, fd_set *fdset);

Fd will be added to the collection fdset go.

3、void FD_CLR(int fd, fd_set *fdset);

Clear specified fd from the collection.

4、int FD_ISSET(int fd, fd_set *fdset);

Determining whether the specified fd can read and write.

 

For the select function, writefds exceptfds usually filled with NULL. At the same time, for select () before and after fd_set collection of objects contained in it are not the same. Before select (), we will fill all the resources we want to listen to fd fd_set collection, but after select (), the data set corresponding to the fd_set has changed. There are usually only can be understood as fluctuations in the data set of resources will appear in select () later in the code.

 

The following is a use select () to re-use TCP pseudo-code programming:

 

poll function:

int poll(struct pollfd *fds, nfds_t nfds, int timeout);

Function is executed, return value greater than 0, returns EOF failure timeout value 0 is returned.

Is a system parameter pollfd guitar body body which has the following prototype:

struct pollfd {

    int fd; // file descriptor number to listen.

    short events; // event request.

    short revents; // Return of the event.

};

Nfds timeout parameter and will not repeat them.

The following is a sample program, the program from https://www.jianshu.com/p/6a6845464770

 

epoll function:

slightly.

 

IO drive signal

slightly.

 


 

Guess you like

Origin www.cnblogs.com/chorm590/p/11688916.html