Linux | Common IO model

Text editor: Author: cloud nine percent notes, https://www.cnblogs.com/luyucheng/p/6249551.html

Foreword

  • Blocking IO (blocking IO)
  • Non-blocking IO (nonblocking IO)
  • IO multiplexing (IO multiplexing)
  • 异步IO(asynchronous IO (the POSIX aio_functions))

The first three are synchronized, only the last is asynchronous IO.

A blocking IO

Introduction: The process will block until data copying is completed.
IO application calls a function that causes the application blocks, waiting for the data is ready. If the data is not ready, we have been waiting for. Data ready, copy from the kernel to user space.
After machining, IO function returns a successful response to the application, the application gets the response, it is no longer blocked, and work back.
IO network blocking as shown below:

Example:
restaurant, the end point of the meal, can only sit inside the restaurant and other good food, go to the mall to eat, wait for the middle of cooking time wasted. This is blocked.

Second, non-blocking IO

Description: non-blocking IO IO functions by repeatedly calling process (multiple system calls, and returns immediately); in the process of copying the data, the process is blocked.
IO application calls a function, the IO operation will return immediately from the kernel (when the IO operation can not be completed, an error is returned). But things (writing data) this specific IO function to be executed may not be complete.
For applications, although this was soon returned to the IO operation, but it does not know whether the IO operation really successful. In order to know whether the IO operation is successful, there are two strategies:
actively cyclically one needs to ask the application until the data is ready so far, in this ever ask process, will take up a lot of CPU time;
the second is the use of IO notification mechanism, such as: IO multiplexed signal driving or IO.
Non-blocking IO network as shown below:

Example:
they want to go shopping, but also worry about the dinner ready. So shopping for a while, come back to ask the waiter dinner ready, no, back and forth several times. This is non-blocking.

Three, IO multiplexing

Description: IO than obstruction and no superiority, the key is to achieve simultaneously on multiple IO listening port.
IO multiplexing models will use select, poll, epoll functions, these functions also make the process of blocking, and blocking IO but are different, these two functions can block multiple IO operations simultaneously. But also for multiple read operations simultaneously, a plurality of write IO function operation is detected, until the data is read or written, it really calls the IO operation function.
Multiplexing network IO as shown below:

Example:
stores have electronic screen for displaying the status of the meal, so that the food is good, electronic screen directly see it. This is the IO multiplexing.

Fourth, asynchronous IO

简介:告知内核启动某个操作,并让内核在整个操作完成后通知我们;数据拷贝的时候进程无需阻塞。
当一个异步过程调用发出后,调用者不能立刻得到结果,实际处理这个调用的函数在完成后,通过状态、通知和回调来通知调用者的输入输出操作。
linux提供了AIO库函数实现异步,但是用的很少。目前有很多开源的异步IO库,例如libevent、libev、libuv。
网络异步IO如下图所示:

示例:
叫外卖,只需要打个电话说一下,然后可以做自己的事情,饭好了就送来了。这就是异步。

小结:
同步IO引起进程阻塞,直至IO操作完成。
异步IO不会引起进程阻塞。
IO复用是先通过select调用阻塞。
阻塞IO、非阻塞IO、多路复用IO都属于同步IO,异步必定是非阻塞的,所以不存在异步阻塞和异步非阻塞的说法。因为其中真正的IO操作(函数)都将会阻塞进程,只有异步IO模型真正实现了IO操作的异步性。

参考资料:
http://www.cnblogs.com/Anker/p/3254269.html
http://www.cnblogs.com/Anker/p/5965654.html

前言

  • 阻塞IO(blocking IO)
  • 非阻塞IO(nonblocking IO)
  • IO复用(IO multiplexing)
  • 异步IO(asynchronous IO (the POSIX aio_functions))

前三种都是同步,只有最后一种才是异步IO。

一、阻塞IO

简介:进程会一直阻塞,直到数据拷贝完成。
应用程序调用一个IO函数,导致应用程序阻塞,等待数据准备好。如果数据没有准备好,一直等待。数据准备好了,从内核拷贝到用户空间。
执行完毕后,IO函数会向应用程序返回成功响应,应用程序得到响应后,就不再阻塞,并进行后面的工作。
网络中IO阻塞如下图所示:

示例:
餐厅吃饭,点完餐后,只能坐在餐厅里面等做好饭,吃完才能去逛商场,中间等待做饭的时间浪费掉了。这就是阻塞。

二、非阻塞IO

简介:非阻塞IO通过进程反复调用IO函数(多次系统调用,并马上返回);在数据拷贝的过程中,进程是阻塞的。
应用程序调用一个IO函数,这个IO操作会从内核中立即返回(当IO操作无法完成时,返回一个错误)。但是这个IO函数具体要执行的事情(写数据)可能并没有完成。
而对于应用程序,虽然这个IO操作很快就返回了,但是它并不知道这个IO操作是否真的成功了。为了知道IO操作是否成功,一般有两种策略:
一是需要应用程序主动地循环地去问,直到数据准备好为止,在这个不断问的过程中,会大量的占用CPU的时间;
二是采用IO通知机制,比如:IO多路复用或信号驱动IO。
网络IO非阻塞如下图所示:

示例:
又想去逛商场,又担心饭好了。所以逛一会,回来询问服务员饭好了没有,来来回回好多次。这就是非阻塞。

三、IO多路复用

简介:比阻塞IO并没有什么优越性,关键是能实现同时对多个IO端口进行监听。
IO复用模型会用到select、poll、epoll函数,这几个函数也会使进程阻塞,但是和阻塞IO所不同的,这两个函数可以同时阻塞多个IO操作。而且可以同时对多个读操作,多个写操作的IO函数进行检测,直到有数据可读或可写时,才真正调用IO操作函数。
网络IO多路复用如下图所示:

示例:
商场安装了电子屏幕用来显示点餐的状态,这样饭是否好了,都直接看电子屏幕就可以了。这就是IO多路复用。

四、异步IO

简介:告知内核启动某个操作,并让内核在整个操作完成后通知我们;数据拷贝的时候进程无需阻塞。
当一个异步过程调用发出后,调用者不能立刻得到结果,实际处理这个调用的函数在完成后,通过状态、通知和回调来通知调用者的输入输出操作。
linux提供了AIO库函数实现异步,但是用的很少。目前有很多开源的异步IO库,例如libevent、libev、libuv。
网络异步IO如下图所示:

示例:
叫外卖,只需要打个电话说一下,然后可以做自己的事情,饭好了就送来了。这就是异步。

小结:
同步IO引起进程阻塞,直至IO操作完成。
异步IO不会引起进程阻塞。
IO复用是先通过select调用阻塞。
阻塞IO、非阻塞IO、多路复用IO都属于同步IO,异步必定是非阻塞的,所以不存在异步阻塞和异步非阻塞的说法。因为其中真正的IO操作(函数)都将会阻塞进程,只有异步IO模型真正实现了IO操作的异步性。

参考资料:
http://www.cnblogs.com/Anker/p/3254269.html
http://www.cnblogs.com/Anker/p/5965654.html

Guess you like

Origin www.cnblogs.com/wyf0518/p/12176172.html