Five types of I/O models for Linux

Source: Internet
Author: User

How do I/O operations complete?
Because the process cannot manipulate the I/O device directly, it is necessary to assist with the I/O operation through the system call request kernel, and the kernel maintains a buffer for each I/O device. Its workflow is:

For input, Waiting (wait) data input into buffer takes time, and it takes time to copy from buffer to user process buffers. Therefore, depending on the wait mode, I/O actions can be divided into 5 modes:
5 Types of I/O models in Linux

1. Blocking I/O model
Under this model. When the process executes a system call, if no corresponding data is found in the buffer of the user process space, the process will be in a blocked state. When the kernel reads data from the disk into the buffer of the kernel space, and the data in buffer is copied to the buffer in the user process space, the process will be in a blocked state. When the data arrives in the user process cache, the process is unblocked, and then the process returns the data to the client.
Because the process is in a blocked state, the CPU is rarely consumed, just waiting for the CPU to respond. Therefore, this model can improve the efficiency of CPU usage. In this model, if there is still no data in the user process space, waking the process at this time is still useless, so we call this sleep non-disruptive.
How it works:

650) this.width=650; "title=" block. jpg "alt=" wkiol1unpeiq47kkaab_zn6p_zo675.jpg "src=" http://s3.51cto.com/wyfs02/M00/ 5b/95/wkiol1unpeiq47kkaab_zn6p_zo675.jpg "/>

2. Non-blocking I/O model
Under this model. The process executes a system call, and if no data is found in the user process space buffer, the process is not blocked. Instead, the process constantly asks if there is data in the user's process space buffer. If not, returns Ewouldblock immediately, because the process is in a non-blocking state, so the process can do other things.
In this model, the process needs to constantly ask the user whether there is data in the process buffer, so it will cause the CPU to make a lot of context switching (process switching), which will consume CPU. This model is rarely used by people.
How it works:

650) this.width=650; "title=" non-blocking. jpg "alt=" wkiom1unoztt3rreaadccb1kf6w256.jpg "src=" http://s3.51cto.com/wyfs02/M00 /5b/9b/wkiom1unoztt3rreaadccb1kf6w256.jpg "/>

3. I/O multiplexing
Under this model, the process is blocked by the Select,poll or Epoll function. These two functions can block multiple I/O operations, and can detect multiple reads and writes at the same time, until the data becomes readable or writable, that is, the data is already loaded into buffer. Before actually invoking the I/O operation function, and then copying the data in buffer into the user process cache, the process then reads its data and returns it to the client.
How it works:

650) this.width=650; "title=" io multiplexing. jpg "alt=" wkiol1unpiht0lz1aac_a-dkgve425.jpg "src=" http://s3.51cto.com/wyfs02/ M01/5b/95/wkiol1unpiht0lz1aac_a-dkgve425.jpg "/>

4. Signal-driven I/O model
In this model, when the kernel reads the data into buffer in kernel space, the kernel generates a Sigio signal for the process. Informs the process that the data is ready, and then the process executes the system call, copies the data in buffer to the user process buffer, and the last process reads the corresponding data back to the client.
How it works:

650) this.width=650; "title=" signal-driven. jpg "alt=" wkiom1uno2ijq31paac8ja4bqhc022.jpg "src=" http://s3.51cto.com/wyfs02/ M01/5b/9b/wkiom1uno2ijq31paac8ja4bqhc022.jpg "/>

5. Asynchronous I/O model
In this model, the data is loaded from disk into buffer, and the data in buffer is copied to the user process cache. When the data is copied, a signal is sent to the process so that the process reads the data in the user process cache and then returns it to the client.
How it works:

650) this.width=650; "title=" asynchronous. jpg "alt=" wkiol1unpwybar0uaacjjgfeix0731.jpg "src=" http://s3.51cto.com/wyfs02/M02/ 5b/95/wkiol1unpwybar0uaacjjgfeix0731.jpg "/>

5 different I/O model comparisons

650) this.width=650; "title=" io comparison. png "alt=" wkiom1unpfygobn8aai9_rjweu8611.jpg "src=" http://s3.51cto.com/wyfs02/ M02/5b/9b/wkiom1unpfygobn8aai9_rjweu8611.jpg "/>


Synchronizing i/o:synchronous I/O causes the request process to block until I/O completes. Read and write operations in the blocking I/O model and non-blocking I/O model are using synchronous I/O
Asynchronous i/o:asynchronous I/O does not cause the request process to block. asynchronous I/O is used in the I/O multiplexing model and the asynchronous I/O model

Horizontal trigger: Notifies once at a time until the process sees
Edge Trigger: Sends only one notification, regardless of whether the process sees it


Characteristics of processes and threads
Process features:
1. For a multi-process model, a process responds to a request
2, the process volume is large, process switching too many times
3, each process address space is independent, a lot of space is duplicate data, so memory use less efficient

Threading Features:
1. Each thread responds to a request:
2. Thread is still switched: Switching is lightweight compared to process
3, the thread of the same process can share many resources of the process, such as open files;
4, the demand for memory is slightly lower than the process;
5. Thread Jitter when switching quickly

This article is from the "Linux Learning Road" blog, reproduced please contact the author!

Five types of I/O models for Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.