Five kinds of Linux IO model

Disclaimer: This article is a blogger original article, please follow bloggers Zhiyi Sheng forwarding. https://blog.csdn.net/Ga4ra/article/details/90183574

1. blocking IO model

This is the simplest I / O model, the general performance of processes or threads waiting for some condition, if the condition is not satisfied, then wait forever.

Blocking io

Until the kernel has finished copying the data reported, the application process will exit the blocked state.

This model is time consuming for low concurrency, aged less demanding situations.

2. The non-blocking IO model

We're waiting, can do something else, and by way of polling, from time to time to ask the kernel data is not ready.

Non-blocking io

If a poll that data is ready, then copy the data to the user space.

3. The drive signal IO model

If the kernel can automatically, after we finished copying the data reported to the notification of the application of it?

IO drive signal

This is the signal to drive IO.

Application process, receive a signal to the kernel specified signal handler SIGIO, the handler automatically.

So how do we deal with more than a few iodo? We can lower a model monitoring a plurality of descriptors.

4. IO multiplexing pattern

IO by multiplexing a plurality of descriptors may be monitored, once a descriptor ready (ready generally read or write-ready), the program can be notified accordingly read and write operations.

IO multiplexing

select()Registration will listen to all good IO.

  • If all the data needed to monitor the IO are not ready, select()the calling process will block;
  • Description descriptor is ready to return the number of symbols is ready;
  • No description ready within the timeout return 0;
  • Return -1 on failure.
int select(
  int maxfd,
  fd_set *rdset,
  fd_set *wrest,
  fd_set *exset,
  struct timeval *timeout);

Note that the IOreuse model is not non-blocking, because the signal handler is not registered with the kernel.

5. Asynchronous IO model

In front of the four models are synchronized, because the data copy is synchronized, through recvfroma data copying operation.

The signal driver model, data preparation phase is considered to be asynchronous, data copy operations are synchronized.

To asynchronously, you must specify additional tasks to the kernel.

Asynchronous IO

Initiates a user process aio_read()after the operation, passing the kernel descriptor buffer pointer, the buffer size; kernel receives aio_readafter, will return immediately, and then waits for the kernel starts data preparation.

After the data is ready to directly copy the data to the user space, save recvfrom(), and then this notification process IOhas been completed.

Asynchronous model is clearly the most easy, and consequently do not have control of.

Guess you like

Origin blog.csdn.net/Ga4ra/article/details/90183574
Recommended