Linux System file I/O programming (iii) I/O multiplexing

Source: Internet
Author: User
Tags signal handler

Multi-channel multiplexing

Function description

We all know that the Fcntl () function solves the problem of file sharing, if you don't know, please see: http://blog.csdn.net/mybelief321/article/details/8993138. The next step is to handle I/O reuse. So, what is I/O multiplexing? Let's make it clear that the so-called I/O multiplexing is nothing more than multiple processes using an I/O input stream.

In the classic "UNIX Network programming volume 1th" Chapter 6, the author introduces five I/O models in detail, respectively:

①blocking I/O

②nonblocking I/O

③I/O multiplexing (SELECT and poll)

④signal driven I/O (Sigio)

⑤asynchronous I/O (the POSIX aio_functions)

My English level is general, still change Chinese to say. There are 5 types of I/O processing models:

① blocking I/O type: In this mode, if the called I/O function does not complete the associated function, it suspends the process until the relevant data arrives before returning. This is often the case when reading and writing to piping, terminal devices, and network devices is common.

② non-blocking I/o: In this mode, when the requested I/O operation cannot be completed, the process does not sleep and returns immediately. Non-blocking I/O enables users to invoke I/O operations that are not blocked, such as open (), write (), and read (). If the operation does not complete, an error is returned immediately (such as not opening the file) or a return of 0 (for example, there is no data in the buffer to read or there is no space to write data).

③I/O multiplexing Model: In this mode, I/O can perform other operations if the requested I/O operation is blocked and it is not a real blocking I/O, but rather a function that waits. The Select () function and the poll () function that we are going to talk about are of this type.

④ Signal-driven I/O model: In this mode, the process defines a signal handler that can automatically capture the arrival of a particular signal to start I/O. This is determined by the kernel notifying the user when an I/O operation can be initiated. The model is non-blocking and the kernel sends a SIGIO signal to the process when there is a ready data. Regardless of how we handle the Sigio signal, the advantage of this model is that it is not blocked when the data is waiting to arrive. The main program continues to execute, only when the Sigio signal is received, the data can be processed.

⑤ asynchronous I/O model: In this model, the process first lets the kernel start I/O operations and notifies the process after the entire operation completes. The main difference between this model and the signal-driven model is that the signal-driven I/O is the kernel's notification of when an I/O operation can be started, and the asynchronous I/O model is the kernel's notification of when the process I/O was completed. Not all systems now support this model.

By comparison, the I/O multiplexing model for select () and poll () is an efficient way to handle I/O reuse. It can set the conditions for each file descriptor that is concerned with the program, the time you want to wait, when returned from the Select () function and the poll () function, the kernel notifies the user of the number of prepared file descriptors, the prepared condition (or event), and so on. By using the return results of the Select () and poll () functions, which may detect a registration event for a file descriptor, or a time-out, or an error in the call, the appropriate I/O handler function can be invoked.

function format

The following figure is an introduction to the Select () function

As you can see, the Select function classifies file descriptors according to the desired file operation, where the handling of the file descriptor involves 4 macro functions, as shown in table 2 below

In addition, the Timeou in the Select () function is a pointer to a struct TIMEVAL type, which looks like this:

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.