Linux-five types of IO models

Source: Internet
Author: User

I. Synchronous vs. asynchronous

Earlier in the discussion of threads mentioned a thread to the critical resource access of a synchronous and mutually exclusive relationship, here to emphasize that in the IO Model synchronization and asynchronous and thread synchronization and mutual exclusion is completely not the same thing.

The so-called synchronization , that is, when the caller makes a call, before the result of the call does not return, but the caller itself has been there to wait for the results, as for the way the wait can be divided into different IO models, the following will be discussed in detail; The synchronization here can be understood as: The caller makes a call (such as a system call function) and the desired result is to maintain consistency is also synchronous, you do not give me the result, I have been there waiting to not return, I can not fly alone I must go with the result, For the results of the wait and the extraction is done by the caller, this is synchronization ;

As for the asynchronous , that is, the caller will return directly after the call, of course, there will be no return results, as for the waiting result and get the result of the thing is processed by the call of the part to complete, then through the state, notification or callback to tell the caller, then the caller will come back to get the results to deal with Therefore, the asynchronous here can also be understood as: The caller makes a call can not take the results with their own first return, and this time the caller is you do not have the result of my own go, and so the results are ready when you notify me, I'll come back to take care of it. The wait for the result and the relocation reuse are done by others without the hand of the caller, which is asynchronous ;


Two. Blocking and non-blocking

blocking , as the name implies is blocked in the other, except wait, when the caller initiates the call when the result is returned before the caller is blocked can not continue to execute, that is, the current thread or process will be suspended waiting; Synchronization is unrelated to the process or whether the thread is blocked, only to emphasize that the results of waiting and fetching are self-completion, and blocking is blocking, refers to the current process or the running state of the thread;

non-blocking is the opposite of blocking, which means that the current process or thread is running, that the process or thread is not suspended when no results are returned, and that there is nothing to do with synchronization or asynchrony;


Three. Five types of IO models

For the system IO is the data receiving and sending, it is necessary to go through two processes: one is waiting for data arrival or preparation, and second, when the data is ready or after the arrival to extract from the kernel into the user's memory for processing, that is, the data relocation; Any IO model must pass through these two important processes to carry on the data transmission and the processing;


    1. Blocking IO Model

In the blocking IO model, the current process or thread is always in a blocking state from the call to the system function to get the data to get the data, or nothing until the data is ready and the data is moved to user space:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/80/72/wKioL1dCazqT0gUAAAAPHT3YnIs274.png "title=" Block Io.png "alt=" Wkiol1dcazqt0guaaaapht3ynis274.png "/>

    1. The user first issues the system call function to obtain the data;

    2. System call will enter the kernel to check if there is data ready, if not, wait;

    3. When the data is ready, the data will be copied to the user space;

    4. Copy complete returns a successful return value to tell the user that data can be processed, during which time the user process or thread is always in a blocking state, whether it is waiting for data or a copy of the data;


2. Non-blocking IO model

Unlike the blocking IO model, when a system call is made, if the data is not ready at this time, the process or thread does not go into blocking mode until it waits, but it repeatedly polls "data ok ..." Data Good not ... Data is good ... "This is CPU-intensive, and when the data is ready, it will be copied like the blocking IO model:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/80/74/wKiom1dCbHvQ-30lAAAQOlapjIw722.png "title=" Non-blocking Io.png "alt=" Wkiom1dcbhvq-30laaaqolapjiw722.png "/>

    1. First, the user makes a system call to the kernel to request the desired data;

    2. When the kernel checks that the data is not ready, it returns an error value;

    3. The user receives the error value is not reconciled, will repeatedly ask the kernel whether there is data ready;

    4. The kernel keeps checking until the data is ready and the data is moved, then the user enters the blocking wait state and waits for the data to be copied.

    5. After the data is extracted to the user space, a successful status is returned to inform the user that the data has been processed and the user can process the data;


3. IO multiplexing Model

Since it is reusable, it is possible to manage or process multiple IO at once, mainly by the Select function or the poll (epoll) function, which also blocks the process, but unlike the blocking IO, the IO multiplexing model can block multiple IO operations at once, As long as the data for an IO interface is ready, it returns to the notifying user and makes a system call to get the data:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/74/wKiom1dCcRWQQalGAAAT2gIfcbg954.png "title=" IO multiplexing. png "alt=" Wkiom1dccrwqqalgaaat2gifcbg954.png "/>

    1. The user first invokes the Select or poll function to detect multiple IO interface operations while blocking the process or thread;

    2. When there is at least one IO interface response, the system notifies the kernel to invoke the corresponding function to fetch the data;

    3. The kernel then migrates the copy of the data to the kernel space, and the process or thread is still in a blocking state;

    4. Data ready, return a success value tells the user to process the data;


4. Signal-driven IO model

The user first registers a signal processing function to process the IO signal, when the data is not ready, the process or thread does not block, when the data is ready, the user process or thread will receive a signal SIGIO, this time will call the signal processing function, The IO function operation data is called in the signal processing function, and the user is notified after completion:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/80/77/wKiom1dCjUPhFfEdAAA9vVkWRwc352.png "title=" Signal-driven Io.png "alt=" Wkiom1dcjuphffedaaa9vvkwrwc352.png "/>

    1. Pre-registration of good one for Sigio signal processing function in the user program;

    2. When the data is ready, a sigio signal is sent to the user's process or thread, and the signal is captured;

    3. After capturing the signal, a user-defined signal processing function is executed, and the system function is called in the function to fetch the data;

    4. Fetching data also copies the data to the user space, where the process or thread is still blocked;

    5. When the data is ready to be notified to the user, then the data can be processed;


5. Asynchronous IO Model

For asynchronous IO models, the data waits and moves are not handled by the current process or thread, and the corresponding system functions are returned directly to continue execution, so the current user is not blocked, and the data is in the state after the user space is ready. Notification or callback to tell the user that data can be processed:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/80/75/wKioL1dCkeaCpGXuAAARbVOCoR4737.png "title=" Async io.png "alt=" Wkiol1dckeacpgxuaaarbvocor4737.png "/>

    1. The user program calls the Aio_read function to tell the kernel descriptor, buffer pointer, buffer size, file offset, and how to notify, and then returns immediately;

    2. At this time, the corresponding data operating components of the kernel will be data waiting and relocation, during which the user program is not affected continue to execute;

    3. When the data are already in the user space is ready to be in the function of the notification method reserved to inform the user program processing data;


It is not difficult to find out from the above analysis that in the process of data acquisition, there are two main parts: data waiting and data moving; In addition to this same point, the following summarizes the differences between the various IO models:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/80/75/wKioL1dCm2vhdsMpAAAteL_WMKA853.png "title=" IO model comparison. png "alt=" Wkiol1dcm2vhdsmpaaatel_wmka853.png "/>

From the above comparison can be found: the first four IO models are blocking IO, non-blocking IO, IO multiplexing and signal-driven IO models are synchronous, only the last asynchronous asynchronous IO model; The socket created by default is in block form, For example, Recvfrom and sendto in network communication, or read and write functions are implemented in a blocking way.




Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1782110

Linux-five IO models

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.