The five I/O Models of network programming

Source: Internet
Author: User

I. Overview:

In the network programming, blocking, non-blocking, synchronous, asynchronous is often referred to, let me first talk about the following I understand in I/O blocking, non-blocking, synchronous, asynchronous.

The five I/O models are blocking I/O, non-blocking I/O, signal driven, I/O multiplexing (four of which are synchronous I/O), asynchronous I/O.

Let me cite an example to explain these five I/O models:

There are a,b,c,d,e of five people in fishing:
A used is the most old-fashioned fishing rod, so, have to keep guarding, wait until the fish hooked up again lever. (Blocking I/O)


b, it's a relatively new fishing rod, not afraid of the fish hook after the fishing rod, so he looked at the mobile phone, while occasionally looking at the fishing rod whether there are fish bait, some words quickly pull the rod. (Non-blocking I/O)


C's Fishing rod has a function, can show whether there are fish hooked, so, B and next to the MM chat, and then see if there are fish bait, some words on the quick lever. (Signal driven)


D with the fishing rod and C is similar, but he thought of a good way, is to put several fishing rods at the same time, and then keep in the side, once the show said the fish hooked, it will be the corresponding rod pull up; (I/O multiplexing)


E is a rich man (Rich man), simply hired a person to help him to fish, once the person caught the fish, E sent a message. (Asynchronous I/O)


Let me start with the following I understand blocking, nonblocking, synchronous, asynchronous in I/O.

Two. I/O operation process:

First, the I/O operation involves hardware, so it deals with the operating system. (The two read/send data from the network card is the operating system to do this) so, first explain the following I understand the I/O process (in general, such as data and data relocation process)


First, through the system to our API function call I/O operation (at this time in the user state), the function waits for the I/O cache to have data, the operating system will monitor I/O cache (at this time in the kernel state), when the I/O cache received data, the operating system first notify the function call (into the user state) The function will then invoke some of the already packaged functions, which will copy the data from the kernel to the user buffer (first to the kernel, then to the user), and the function will return.

Specific example:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/80/EC/wKiom1dEco_iumueAAA4-KTD1WQ295.png "title=" io.png "alt=" Wkiom1deco_iumueaaa4-ktd1wq295.png "/>

Three. Blocking, non-blocking, synchronous, asynchronous in I/O operations:

(1). synchronization : When a function is called, the call does not return until the call does not get the result. (in I/O operations, such as calling the Read function, this function is blocked at the Read function call, but the kernel has been doing something related to read, that is, the function is active (CPU consuming), but on the surface, the function has not returned).


(2). blocking : A blocking call is called a function, and the thread that executes the function is suspended (in this state, the CPU does not allocate a time slice for the function) until the result is returned.


(3). non-blocking : means that if a function does not immediately get the result, the function does not block the thread and immediately returns.


(4). asynchronous : Refers to a function call, the call does not immediately get the result, and does not block the thread, the thread of the call will continue to do other things, and this function is done by other executing parts, when the part executes the function, through the state, Notification to notify the caller, or to process the call through a callback function.


execution parts and callers can return results in three ways:
A. Status,
B. Notifications,
C. callback function.
Which depends on the implementation of the execution part, unless the execution part provides multiple choices, otherwise it is not controlled by the caller.

A. If the execution part is notified by state,
then the caller needs to check every once in a while and the efficiency is low.

B. In the case of a notification,
The efficiency is high, as there is little need for extra action on the part of the actuator.

C. As for the callback function,
and the notice is not much different.


Four. Five I/O Models: (Understanding the above knowledge, the following should be relatively easy to understand)

(1). Blocked I/o:

The application calls an IO function that causes the application to block and wait for the data to be ready. If the data is not ready, wait .... The data is ready to be copied from the kernel to the user space, and the IO function returns a success indicator.

Such as:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/E8/wKioL1dEWbvwP2jAAAD2gplXjj4578.png "title=" 2016-05-24 21-35-48 screen. png "alt=" Wkiol1dewbvwp2jaaad2gplxjj4578.png "/>


(2). Non-blocking I/O:

In non-blocking I/O, be sure to put an I/O operation function in a dead loop until the I/O function returns a correct value before jumping out of the loop.

We set a socket interface to non-blocking to tell the kernel that when the requested I/O operation cannot be completed, do not sleep the thread , but return an error. This way our I/O operations function will constantly test whether the data is ready, and if not, continue testing until the data is ready. In this continuous testing process, the CPU will be a lot of time.

Such as:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/80/E9/wKioL1dEW2KzUSsfAAEX3gjDZME671.png "title=" 2016-05-24 21-42-53 screen. png "alt=" Wkiol1dew2kzussfaaex3gjdzme671.png "/>


(3). Signal-driven:

The first thing to do is to allow the interface to signal-drive I/O, and then install a signal-processing function that keeps the thread running and not blocking. When the data is ready, the thread receives a sigio signal that can be called by the I/O operation function in the signal processing function to process the data.

Such as:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/E9/wKioL1dEXQ3SxFLQAAERn2HrAl0766.png "title=" 2016-05-24 21-49-59 screen. png "alt=" Wkiol1dexq3sxflqaaern2hral0766.png "/>


(4). I/O multiplexing:

The I/O multiplexing model uses the Select, poll, Epoll functions, which also block the process, but unlike blocking I/O, these functions can block multiple I/O operations at the same time. I/O functions can be detected at the same time for multiple read operations, multiple write operations, and I/O operation functions are not actually invoked until there is data readable or writable . (There is no advantage over blocking I/O, but it enables monitoring of multiple I/O ports)

Such as:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/EA/wKiom1dEXU2TY0FJAAEQuABW8P0400.png "title=" 2016-05-24 21-55-06 screen. png "alt=" Wkiom1dexu2ty0fjaaequabw8p0400.png "/>



(5). Asynchronous I/O:

When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller of the input-output operation through state, notification, and callback after completion.

Such as:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/E9/wKioL1dEXqmwzseyAADfVcUvVCg016.png "title=" 2016-05-24 21-56-53 screen. png "alt=" Wkiol1dexqmwzseyaadfvcuvvcg016.png "/>


Six. Comparison of five I/O models:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/EB/wKiom1dEXoOwYmIDAAFecHDYRRo659.png "title=" 2016-05-24 21-59-43 screen. png "alt=" Wkiom1dexoowymidaafechdyrro659.png "/>



Seven. Summary:

Understanding the five I/O models should first understand the I/O operation process and understand some of the knowledge of blocking, non-blocking, synchronous, asynchronous.

I/O operations deal with hardware, so I/O operations involve the operating system.

Processing I/O operations is divided into two processes, one is waiting for data, and the other is moving data from the kernel to user space.

The difference between this v I/O model is that in the way it waits for data, the second process is the same as moving data from the kernel to the user space and then doing the relevant operations.


This article is from "Narcissus" blog, please make sure to keep this source http://10704527.blog.51cto.com/10694527/1782715

Five I/O models of network programming

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.