IO model comparison: synchronous, asynchronous, blocking, non-blocking

Recent work come into contact with the web services synchronous and asynchronous, so learning under the "UNIX Network Programming", as in this case summary.

A, I / O model

Input / Output (I / O) is a process between the main memory and an external device (such as a disk drive, and a network terminal) copy of the data. Main memory is input from the I / O device to copy the data, and the output data is copied from the main memory to the I / O device. For example, a network can be regarded as I / O devices as the data source and data receiver. The system can read the data transmitted by the other machines through the network, and copies the data into their own main memory. 

The following describes five kinds of I / O model of Unix:

  • Blocking IO
  • Non-blocking IO
  • IO multiplexing
  • IO drive signal
  • Asynchronous IO

An input operation is generally two stages:

(1) waiting for data to be ready;

(2) Copy the data from the kernel to the process;

Take the example network client requests service descriptions five models.

A network request, a socket (Socket) is an endpoint for communication, the application may send or receive data through it, it can be the same as file open, close, and write operations.

For the input operation of a soket:

(1) waits for data to arrive from the network. It is copied into the buffer in the kernel data arrival;

(2) Copy the data from the kernel buffer to the application process buffer.

1. Blocking IO Models

Blocking IO model is the most commonly used, as we will recvfrom system call to observe the distinction between kernel and application processes. The figure below process calls recvfrom, the system call until the datagram is ready to use and copy the buffer or error before returning, that is to say before the data is returned, the process is blocked when the process returns an indication of success, before they can begin the following deal with.

 

2. The non-blocking IO model

Below, the first three calls recvfrom, data is not ready, the kernel will immediately return an error EWOULDBLOCK, until the fourth call, the data is ready to be copied into the application buffer, the system call returns an indication of success, the next start Data processing. When the application process for a non-blocking like this word to describe the cycle call recvfrom, which is actually polling (polling).

Applications continue to query the kernel, check whether the data is ready, it is a waste of CPU, so this model is relatively rare.

 

 

 

3. IO multiplexing model

IO复用是指通过调用select,poll或者epoll函数,监听多个socket连接,每新来一个socket连接,就会被加入到监听列表,实现单个线程同时处理多个网络连接的IO。基本原理是通过select,poll或epoll不断轮询负责的全部socket,当其中一个数据准备好,就通知进程。然后调用recvfrom拷贝数据从内核到进程,返回成功指示后,进行下一步处理。

应用进程虽然不会被socket的IO阻塞,但一直被select,poll或epoll阻塞。如果socket数不是很多的话,使用IO复用模型可能比多线程 + 阻塞IO延迟更大,因为IO复用模型相对比之前的模型需要两次系统调用,它的优势在于能处理较多的连接。

 

 

 

4. 信号驱动IO模型

该模型通过系统调用sigaction安装一个信号处理程序。当内核准备好数据后,发送信号告知进程。在信号处理程序中调用recvfrom读取数据,并通知主循环。这种模型的好处是当等待数据报到达时,IO不被阻塞。主循环可以继续执行,只是等待信号处理程序的通知:数据已准备好被读。

 

 

5. 异步IO模型

异步IO模型让内核完成整个操作(包括将数据从内核拷贝到进程缓冲区)后才进行通知应用进程。这个模型和信号驱动模型的主要区别在于:信号驱动IO是由内核通知我们何时可以启动一个IO操作,而异步IO是由内核通知我们IO操作何时完成。

下图中调用aio_read,传递内核描述字、缓冲区指针、缓冲区大小、文件偏移,并告诉内核整个操作完成时如何通知我们。该系统调用立即返回,不阻塞于IO操作。该图中,内核在操作完成后传递一个信号,该信号直到数据被拷贝到缓冲区才产生,这是和信号驱动IO的不同之处。

 

 

二、IO模型区别

1. 对比

从这两个阶段来看,前四种模型在第一阶段有所不同,但第二个阶段基本相同,把数据从内核拷贝到应用进程的缓冲区时,进程被阻塞于recvfrom调用。异步IO模型的两个阶段都不同于前四种模型。

 

 

2. 同步vs异步

同步IO操作会阻塞请求进程,直到IO操作完成。

异步IO操作不会阻塞请求进程。

前四种模型:阻塞IO模型、非阻塞IO模型、IO复用模型和信号驱动IO都是同步IO模型,因为真正的IO操作(recvfrom)阻塞进程。

Guess you like

Origin www.cnblogs.com/hithink/p/11503792.html