Several IO models of the system

Foreword:

  1. In a computer system, the operating system acts as one of the basic roles in the specific implementation of hardware-related functions such as IO operations. And we opened

When the hardware-related operations are involved in the    developed application program, it is essentially realized by calling the functions provided by the operating system.

   

  2. Concept introduction

   User mode and system mode:

     User mode: When the instruction executed by the CPU is in the user layer, the program is in the user mode at this time.

     System state: When the instruction executed by the CPU is in the system layer, the program is in the system state.

   Blocking and non-blocking:

     Blocking: During the execution of an event, a request to perform an operation is issued, and the conditions required for the requested operation are not satisfied, then a

         Just wait there until the conditions are met. In the IO operation, it is the process from the initiation of the system call to the start of the IO execution .

     Non-blocking: When a request to perform an operation is issued during the execution of an event, and the conditions required for the requested operation are not satisfied , the operation will be executed immediately.

          A flag is returned to inform that the condition is not met, and the initiator will not wait there forever. In IO operations, it is a system call

          Initiated, if the IO operation is not satisfied, the system layer returns information to the user layer, and the user layer will not wait.    

   Synchronous and asynchronous:

     Synchronization: When an event occurs, it must be performed sequentially one by one, which will lead to a temporary wait for the entire process, and events outside the event.

         There is no way to execute concurrently. In the IO operation, when the IO operation occurs, other cannot be executed ( standing on the

         From the user-level program's point of view, when the IO operation occurs, the rest of my program cannot execute ) .

     Asynchronous: The occurrence of an event does not cause a temporary wait for the entire process, and events outside the event can be executed concurrently. in IO operation

         It is shown that when an IO operation occurs, other processes in the user layer are still executed ( from the point of view of the user layer program ).

   The main process of IO operation :

     1. The user program calls the system IO function.

     2. The system waits for the data to be ready.

     3. Copy the IO data from the system memory space to the user memory space.

     4. The user program operates the data returned by the IO function call.

Operating system IO model:

   1. There are currently 5 IO models of operating systems : blocking IO/non-blocking IO/IO multiplexing/event-driven IO/asynchronous IO

   1.1. Blocking IO
      

     The first stage refers to the disk loading of data into the kernel's in-memory space.

     The second stage refers to copying the data from the kernel's memory space to the user's memory space ( this is the real I/O operation).

   1.2. Non-blocking IO

     
       

     Every once in a while, it asks whether the system data is ready to be completed . On the user layer, it is shown that it needs to continuously call the while loop to ask whether the data is ready.

     no ready.

   1.3.IO multiplexing

      
        

      1. Need to use two system calls ( marked 1 and 2 in the above figure) , the system call in 1 is a mechanism provided by the operating system (linux provides

       It is implemented in select and poll ) , which is called a multiplexer.

      2. Introduction to the IO multiplexing process: a) The main function of the select/poll mechanism helps the caller to find a device that is ready to register on it ( just

                   In non - blocking IO , the function of judging whether the data is ready by the user layer is handed over to the system, and

                   可判断多个IO设备),用户层程序调用后会被阻塞。

                  b)当有设备数据就绪时,通知调用者。

                  c)调用者即可进行IO数据的系统调用

      3.好处:只需启动一个线程不断去轮询复用器上注册IO设备的状态即可,避免了一个线程对应一

           个IO设备的情况,减少了资源占用。

   1.4.事件驱动IO

      
        

      1.采用回调的方式,给相应IO设备事件注册事件处理函数,用户层程序继续往下执行由系统来关

        注数据是否就绪,并通过发生信号来通知用户层程序来调用事件处理函数。

      2.好处:等待数据准备好这段时间,用户层程序不会处于阻塞状态中。

      3.如果一个事件通知一个进程,进程正在忙的,提供一下两种机制处理

       水平触发机制: 内核通知进程来读取数据,进程没来读取数据,内核需要一次一次的通知进程。

       边缘触发机制: 内核只通知一次让进程来取数据,进程在超时时间内,随时可以来取数据,把这个事

                 件信息状态发给进程,好比发个短息给进程( nginx默认采用了该机制)。

    1.5.异步IO

      
       

                  1.不同于以上的IO模型,当IO数据就绪后,也是由系统负责将数据拷贝到用户层中,再通过信号

        通知用户层程序后,用户层程序就直接可以操作IO调用后返回的数据了(用户层程序无需发起读

        取数据的系统调用了)。

总结:

    1.我们可以看出IO操作的功能实现逐步移至系统层,避免用户层到系统层上下文的切换。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326444561&siteId=291194637