Linux I/O model --- I/O reuse: select and poll Functions

Source: Internet
Author: User

Select and poll Functions

In the previous chapter, we encountered a problem that the client was blocked from reading data from the standard input. At the same time, the server required to close the connection for some reason and sent a fin to the client, the client can only know that the connection is closed after reading the data from the standard input. I/O reuse introduced in this chapter can solve this problem well. 1. i/O model. in UNIX, there are five I/O models blocking I/O non-blocking I/OI/o multiplexing (select and poll) signal-driven I/O (sigio) asynchronous I/O (posix.1 AIO _ series functions) B. blocking an I/O model application calls an I/O function, causing application blocking and waiting for data preparation. If the data is not ready, keep waiting .... The data is ready. A successful instruction is returned when I/O functions are copied from the kernel to the user space.

 

C. in the non-blocking I/O model, if we set a set of interfaces to non-blocking, we will tell the kernel that when the requested I/O operation cannot be completed, do not sleep the process, but return an error. In this way, our I/O operation function will
Continuously test whether the data is ready. If not, continue the test until the data is ready. In this continuous testing process, it will take a lot of CPU time.

D. I/O Reuse Model

The I/O reuse model uses select or poll functions. These two functions can also block the process, but they are different from those that block I/O, these two functions can block Multiple I/O operations at the same time. In addition, I/O functions of multiple read operations and write operations can be detected at the same time. I/O operation functions can be called only when data is readable or writable. E. Signal-driven I/O model first, we allow the set of interfaces for signal-driven I/O, and install a signal processing function, the process continues to run without blocking. When the data is ready, the process receives a sigio signal and can call the I/O operation function in the signal processing function to process the data.

F. asynchronous I/O model

Call the aio_read function to tell the kernel description, buffer pointer, buffer size, file offset, and notification method, and then return immediately. When the kernel copies the data to the buffer, it notifies the application. 2. Comparison of several I/O models the difference between the first four models is that the first stage is basically the same, and the second stage is basically the same. All the data is copied from the kernel to the caller's buffer zone. The two phases of asynchronous I/O are different from the first four models. 3. synchronous I/O and asynchronous I/OA. synchronous I/O operations cause request process blocking until I/O operations are completed. Asynchronous I/O operations do not cause request process blocking. B. Our first four models are synchronous I/O, and only the last asynchronous I/O model is asynchronous I/O. 4. select function. the select function can instruct the kernel to wait for any of the multiple events to occur, and only return after any event or after a specified time to wake up process B. you can call the select function to notify the kernel that any descriptor in the set {, 5} is ready for read or any descriptor in the set {} is ready for write, or any descriptor in the set {10.2} has an exception condition to be processed, or it has passed seconds C. the description word can be unrestricted and socket, and any descriptor can be used to test D. function prototype int select (INT maxfds, fd_set * readfds, fd_set * writefds,
Fd_set * effectfds, struct Tim * timeout); parameter description: Timeout: indicates the time that the kernel can spend waiting for any descriptor to be ready. There are three cases: the first case is that timeout is null, this will wait until a descriptor is ready. The second case is that the value of timeout is 0, so the system will not wait and return immediately. The third case is that seconds or microseconds in timeout are assigned values, wait for the specified time. In addition, if a process receives a signal, the SELECT statement is also interrupted and returned. Readfds, writefds, and writable TFDs specify the descriptive words required for Kernel test read, write, and exception conditions. Maxfds: specifies the number of descriptors to be tested. Its value is the maximum descriptor to be tested plus 1. Return Value: the total number of prepared bits of all descriptor sets. When the returned value is 0, any unprepared descriptor in the descriptor set is cleared. We use fd_isset to test which descriptor is ready. Therefore, each time we call select, We need to reset the descriptor we care about in the descriptor set to 1. e. fd_set indicates that fd_set is an integer array. Each bit in each number corresponds to a descriptor. For example, if 32 characters are used to represent an integer, the first element of the array corresponds to the descriptive word 0 ~ 31. The second element corresponds to the description word 32 ~ 63. Four Related macros: fd_clr (int fd, fd_set * Set );
Fd_isset (int fd, fd_set * Set );
Fd_set (int fd, fd_set * Set );
Fd_zero (fd_set * Set); F. Conditions for reading the descriptor: The data bytes in the Interface Buffer must be greater than or equal to the current value of the interface receiving buffer. The set of interfaces will not be blocked and return a value greater than 0, that is, the number of data bytes to be read. When the set interface receives a fin, the read operation of the Set interface returns a 0. The set interface is a listening set interface, and the number of connections completed is not 0. There is a set of interface errors to be processed. The read operation of the Set interface will return the-1. G. descriptor prepared write condition: the available space of the Set interface sending buffer is greater than or equal to the current value of the Set interface sending buffer low tide limit ., Or set the interface to connect, set the interface does not require connection. If this half is disabled, a sigpipe error is generated. There is a set of interface errors to be processed. H. condition for descriptor exception: the out-of-band data of the Set interface is still in the out-of-band mark I. the maximum descriptor that each process can use has a macro fd_setsize that defines the maximum number of descriptors that a process can use. To change this value, you must not only change it in the defined header file, but also re-compile the kernel. 5. use the select function to modify the preceding client-server program. In the preceding client-server program, the client uses the stop-and other policies to receive user input from the standard input, the advantage is that the user input can be completed one by one and then read
The disadvantage of retrieving the string returned by the slave server is that when the program blocks and waits for user input, it cannot process such messages as fin from the server in a timely manner. Now we use the select function
To avoid the problems mentioned above. In addition, the server also uses the select function to avoid too many processes. After the select function is used, only one process can process multiple clients. Create
An integer array used to store client connections that have been completed. After reading data from the daily interface, we add a new connection from the client to this array and modify the value of maxfd. Each time
After reading data from the client interface, write the data again to the client interface. A. After the server reads data from the client interface, the returned value may be 0. This indicates that the client has closed the connection to write data in this direction. After writing data to the client-side interface, close the connection. And remove the customer connection from the array that stores the customer connection. B. When the above scheme is adopted, a potential problem may be the possibility of DoS attacks. A malicious user establishes a connection with the server and sends a single character, but does not send line breaks or final
In this way, the server will be blocked in the READ function. The possible solution is to use non-blocking I/O, allow each customer to process it with a separate process, or set timeout for I/O operations. C. When the client receives the EOF, it can only close the connection in this direction, because we still want to read data from the server. In this case, close cannot be used to close the connection.
Shutdown. If the access count of the socket is greater than 0, close will only reduce the count by 1. If the access count of the socket is equal to 0, close will terminate the two directions of the socket,
In this way, we will not be able to read data that is still not sent back from the server. 6. shutdown function int Shutdown (int s, int how); parameter description: S: represents the socket description word how: shut_rd -- close the connection to the Data Reading direction of the socket shut_wr -- close the connection to the data writing direction of the socket shut_rdwr -- close the two-way connection of the socket 7. pselect function int pselect (int n, fd_set * readfds, fd_set * writefds,
Fd_set * contains TFDs, const struct timespec * timeout, const
Sigset_t * sigmask);. the pselect function adopts the timespec structure. This structure supports nanosecond B. sigmask is a signal mask and will not be allowed to submit some signals 8. poll function int poll (struct pollfd * ufds, unsigned int NFDs, int timeout);. parameter description ufds: it is a pointer to a struct pollfd struct NFDs: It indicates the number of descriptive words we care about Timeout: the timeout wait time, in milliseconds B. struct pollfd struct description struct pollfd {
Int FD;
Short events;
Short revents;
};
FD: Describe words events: events of interest on the description word revents: Events returned on the description word

After the poll function returns, we need to test whether the events in revents are of our concern.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.