Linux driver for embedded learning: IO model (1) overview

What is IO?

The full English name of IO is input and output, which translates to input and output.
In the von Neumann structure, the computer is divided into five parts: arithmetic unit, controller, memory, input device, and output device. The input device refers to the input of data or information to the computer, such as a mouse and a keyboard are both input devices. An output device is a device that receives computer output, such as a computer monitor.
insert image description here

IO execution process

The operating system (Linux) is responsible for managing computer resources, and applications run on the operating system in user space. The application program cannot operate the hardware directly, but can only operate the hardware through the API provided by the operating system.

So when we are performing IO operations, such as reading and writing disk operations, the process needs to switch to the kernel space to perform such operations. And the application program cannot directly manipulate the data in the kernel space, and needs to copy the data in the kernel space to the user space.

A complete IO process includes the following steps:

  1. The application initiates an IO call request (system call) to the operating system.
  2. The operating system prepares the data and loads the IO data into the kernel buffer.
  3. The operating system copies the data, and copies the data in the kernel buffer from the kernel space to the user/application space.

IO model introduction

However, in the IO execution process, since the speed of the CPU and memory is much higher than that of the peripherals, there is a serious speed mismatch.

For example, if I want to write 100M data to the disk, it may only take a few seconds for the CPU to output 100M data, but it may take several minutes for the disk to receive 100M data.
How to deal with this contradiction? You can use the IO model for programming.

Types of IO models

The IO model includes blocking IO, non-blocking IO, signal-driven IO, IO multiplexing, and asynchronous IO, the first four of which are called synchronous IO.

Whether to wait for the execution result of IO is the difference between synchronous IO and asynchronous IO. Waiting for the execution result of IO is synchronous, and not waiting is asynchronous.

blocking IO

To block means to wait forever.
A very representative library function in C language isscanf函数

insert image description here

non-blocking IO

Non-blocking IO just doesn't wait.

insert image description here

I/O multiplexing

There is one more select function, and one parameter in the select function is a collection of file descriptors. Use it to monitor these files. When a file description is ready, the file descriptor is processed.

insert image description here

Signal driven IO

insert image description here

Asynchronous I/O

insert image description here

Guess you like

Origin blog.csdn.net/qq_28877125/article/details/128407272