Talking about the five kinds of I under Linux / O model (transfer) On the five kinds of I under Linux / O model

Talking about the five kinds of I under Linux / O model

 

 A, lead the I / O model

  We all know that in order to consider the security of the OS, etc., the process can not directly manipulate I / O devices, which must be completed to assist I / O request action by the kernel system call, the kernel for each I / O equipment maintenance a buffer. As shown below:

   

  Request for the entire process:  a user request to initiate a process, the kernel receives a request to retrieve data from the I / O device to the buffer, then the data in the copy buffer into the address space of the user process, the user process obtains the data then responds to the client.

  Throughout the process the request, the data input to the buffer takes time, and copying data from the buffer to the process takes time. Therefore, according to the different ways in which to wait for two time, I / O operation can be divided into the following five modes:

  (1) 阻塞I/O (Blocking I/O)

  (2) non-blocking I / O (Non-Blocking I / O)

  (3) I / O 复用 (I / O Multiplexing)

  (4) signal for driving I / O (Signal Driven I / O)

  (5) an asynchronous I / O (Asynchrnous I / O)

 

Second, the division of the I / O model

  Blocking: the calling process has been in a waiting state until the operation is complete.

  Non-blocking: data in the kernel is not ready, you return immediately, the process can shop and go to other things.

  From synchronous asynchronous, and blocking, non-blocking two dimensions to look at:

    

 

Three, I / O models are described as

  1, blocking I / O

     

  From the map you can see the whole process, when a user process makes a system call, the kernel starts the first phase I / O, and prepare the data into the buffer, when the data are ready completed, the data from copy the kernel buffer to the memory of a user process, then the user process before lifting block the state re-run.

  So, Blocking I / O feature is in two stages I / O block the execution have been.

 

  2, non-blocking I / O

     

  From the map you can see in two stages I / O execution, the user only in the second stage of the process is blocked, and the first stage is not blocked, but in the first stage, the process needs to blind users and other , to stop polling the kernel to see if the data is ready, so the model is more CPU intensive.

 

  3, I / O multiplexing

    

   From the map you can see in the I / O multiplexing model, the two stages I / O is performed by the user process is blocked, but the two phases are independent, in a complete I / O operation, the user process is initiated two system calls.

 

  4, the drive signal I / O

     

  The model also known as I / O model based on event-driven, you can see this model, only blocking the user process in the second phase of I / O performed in the first stage is not blocked.

  At first glance the feeling and non-blocking model is very similar, in fact, different in the fact that the model in the first phase I / O performed after the completion of data preparation, will take the initiative to inform the user of the data is ready to complete the process, that user process to make a callback. The notice is divided into two, one for the trigger level, that is, if the user does not respond to the process would have been to send a notification, two for the edge-triggered, notify only once.

 

  5, 异步 I / O

     

  In this model, when the user initiates the process of system calls, you can immediately start to do other things, and then until the I / O performed in two phases are complete, the kernel will give the user process to send a notification that the user process operation has been finished.

 

Four or five model summary

   

 

 A, lead the I / O model

  We all know that in order to consider the security of the OS, etc., the process can not directly manipulate I / O devices, which must be completed to assist I / O request action by the kernel system call, the kernel for each I / O equipment maintenance a buffer. As shown below:

   

  Request for the entire process:  a user request to initiate a process, the kernel receives a request to retrieve data from the I / O device to the buffer, then the data in the copy buffer into the address space of the user process, the user process obtains the data then responds to the client.

  Throughout the process the request, the data input to the buffer takes time, and copying data from the buffer to the process takes time. Therefore, according to the different ways in which to wait for two time, I / O operation can be divided into the following five modes:

  (1) 阻塞I/O (Blocking I/O)

  (2) 非阻塞I/O (Non-Blocking I/O)

  (3) I/O复用(I/O Multiplexing)

  (4) 信号驱动的I/O (Signal Driven I/O)

  (5) 异步I/O (Asynchrnous I/O)

 

二、关于I/O模型的划分

  阻塞:调用的进程一直处于等待状态,直到操作完成。

  非阻塞:在内核的数据还未准备好时,会立即返回,进程可以去干其他事情。

  从同步异步,以及阻塞、非阻塞两个维度来划分来看:

    

 

三、I/O模型分述

  1、阻塞I/O

     

  从上图可以看到在整个过程中,当用户进程进行系统调用时,内核就开始了I/O的第一个阶段,准备数据到缓冲区中,当数据都准备完成后,则将数据从内核缓冲区中拷贝到用户进程的内存中,这时用户进程才解除block的状态重新运行。

  所以,Blocking I/O的特点就是在I/O执行的两个阶段都被block了。

 

  2、非阻塞I/O

     

  从上图可以看到在I/O执行的两个阶段中,用户进程只有在第二个阶段被阻塞了,而第一个阶段没有阻塞,但是在第一个阶段中,用户进程需要盲等,不停的去轮询内核,看数据是否准备好了,因此该模型是比较消耗CPU的。

 

  3、I/O复用

    

   从上图可以看到在I/O复用模型中,I/O执行的两个阶段都是用户进程都是阻塞的,但是两个阶段是独立的,在一次完整的I/O操作中,该用户进程是发起了两次系统调用。

 

  4、信号驱动的I/O

     

  该模型也叫作基于事件驱动的I/O模型,可以看到该模型中,只有在I/O执行的第二阶段阻塞了用户进程,而在第一阶段是没有阻塞的。

  乍看起来感觉和非阻塞模型很相似,其实不同之处就在于,该模型在I/O执行的第一阶段,当数据准备完成之后,会主动的通知用户进程数据已经准备完成,即对用户进程做一个回调。该通知分为两种,一为水平触发,即如果用户进程不响应则会一直发送通知,二为边缘触发,即只通知一次。

 

  5、异步I/O

     

  在该模型中,当用户进程发起系统调用后,立刻就可以开始去做其它的事情,然后直到I/O执行的两个阶段都完成之后,内核会给用户进程发送通知,告诉用户进程操作已经完成了。

 

四、五种模型总结

   

Guess you like

Origin www.cnblogs.com/ptfe/p/10968954.html