Five models of I/O

Source: Internet
Author: User

1.1 of five I/O models

1) Blocking I/O

2) non-blocking I/O

3) I/O multiplexing

4) event (signal) drive I/O

5) asynchronous I/O

1.2 Why should I initiate a system call?

Because the process wants to get the data on the disk and can only deal with the kernel, the process notifies the kernel that it wants the data on the disk

This process is called by the system

1.3 Steps for I/O completion

When the process initiates a system call, the system call enters kernel mode and then starts the I/O operation

I/O operations are divided into two steps:

1) The disk loads the data into the kernel's memory space

2) The kernel's memory space is copied to the user's memory space (this process is where the real I/O occurs)

Note: Most of the IO calls are blocked

Process Analysis

The whole process: this process needs to operate on the data on the disk, it initiates a system call to the kernel, and then the process is switched out,

This process is suspended or goes to sleep, also known as non-disruptive sleep, because the data is not available until the results of the system call are completed,

The process is awakened and the next operation continues, from the beginning of the system call to the end of the system call:

The ① process initiates a system call to the kernel,

The ② kernel receives a system call, knows it is a request to the file, and then tells the disk to read the file.

③ disk receives the kernel command and loads the file into the kernel's memory space

after the ④ kernel's memory space receives the data, the data Copy to the memory space of the user process ( This process is where I/O occurs )

⑤ process memory space to get data, send notification to kernel

The ⑥ kernel replies the received notifications to the process, which wakes up the process, and then the process gets the data for the next steps

2.1 Blocking

means that the current thread will be suspended ( the thread goes to sleep ) until the result is returned before the call returns, in order to continue execution

How does blocking the I/O system notify the process?

After I/O is complete, the system notifies the process directly that the process is awakened

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

The second stage is the data copy of the kernel's memory space to the user's memory space (this is the real I/O operation)

2.2 Non-blocking

non-blocking: The process initiates I/O calls, I/O knows that it takes time to complete, notifies the process to do something else immediately, or non- Blocking I/O

non-blocking I/O , how does the system notify the process?

every once in a while, ask if the kernel data is ready to complete and the process gets the data after the system completes ( This process is also called blind waiting )

Disadvantage : can not handle multiple I/O, such as user open file , CTRL + C want to terminate this operation , is unable to stop

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

The second stage is the data copy of the kernel's memory space to the user's memory space (this is the real I/O operation)

2.3 I/O multiplexing select why I/O multiplexing

A process blocks multiple io , one process waits for information to be entered from the keyboard, and the other is ready to load information from the hard disk

For example, through the read command , called an IO operation , an io is completed , an io is not completed , blocking keyboard io, disk io is complete ,

This process is also unable to respond, because the keyboard IO is not finished , still blocking , the process is still asleep , this time what to do ?

This requires I/O multiplexing.

Execution process

After the process in the call io , not directly call io function , in the system kernel , a new system call , The help process monitors multiple io,

Once a process requires a system call, a special system call to the kernel , when the application is initiated, is blocked on the call of the multiplexer,

So reusing this function will monitor these IO operations , and any io will tell the process that one of the io is complete , If the process relies on an io operation ,

At this point in time , the process can proceed with the subsequent operation . A tool that can help monitor these io in a group process is called an io multiplexer .

I/O multiplexer in Linux

Select: is an implementation , the process needs to call the time , send the request to Select, can initiate multiple , but can only support A limitation of the congenital

Poll: There is no limit , but the performance of the extra 1024x768 will decrease

So early Apache itself prefork mpm model , the main process in accepting multiple user requests , the number of online requests more than one , Will not work anymore .

SoioWill reuse be better than the first two??

The process and the system kernel to communicate directly, in the middle add an I/O multiplexing Select, if it is a messenger , find someone to relay, So what's the end of this message ?

Although the problem of multiple system calls is resolved , the second half of the multiplexing itself is still blocked , blocking on select , not blocking on system calls ,

But his second paragraph is still blocked , because to scan all the multiple IO operations , a more processing mechanism , performance may not rise , performance may not be much improved

    
the first phase refers to the disk loading data into the kernel's in-memory space
                                        The second stage is the data copy of the kernel's memory space to the user's memory space (this is the real I/O operation) 2.4 Event-driven

The process initiates the call , through the callback function , the kernel will remember that process is requested , once the first paragraph is completed , you can initiate a notification to the process ,

So the first paragraph is non-blocking , the process does not need to be blind, but the second paragraph is still blocked

Event-driven mechanisms(Event-driven)

It is because of the event-driven mechanism that multiple requests can be

For example : a Web server . A process responds to multiple user requests

Defect : The second segment is still blocked

Two mechanisms

What if an event notifies a process that the process is busy and the process is not heard ?

Horizontal trigger mechanism: The kernel notifies the process to read the data, the process does not read the data, the kernel needs a one-time notification process

Edge trigger mechanism: The kernel only notifies the process to fetch the data once, the process can fetch the data at any time during the time-out period, send the event information status to the process , like to send a short interest to the process ,

Nginx

The default use of the Edge trigger drive mechanism

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

The second stage is the data copy of the kernel's memory space to the user's memory space (this is the real I/O operation)

2.5 Asynchronous AIO

Regardless of the first second paragraph , No feedback is made to the system call , only the data is completely copied to the service process memory , the service process returns OK information, other times,

Processes are free to do their own thing until the kernel notifies the OK message

Note: AIO can only be implemented in Files , and network asynchronous IO cannot be implemented

Nginx:
nginxfile IO file asynchronous request
A process responds to N requests
Static file bounds : support sendfile
Avoid wasting replication time : mmap supports memory mapping, kernel memory is copied to process memory this process , does not need to replicate , directly mapped into the process memory
Support for Edge triggering
Supports asynchronous IO
Solved the problem of c10k.
c10k: 10,000 simultaneous concurrent connections
c100k: you know

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

The second stage is the data copy of the kernel's memory space to the user's memory space (this is the real I/O operation)

The first four I/O models are synchronous operations, and the last Aio is an asynchronous operation

2.6 Comparison of five models

Synchronous blocking

Both segments are blocked , and all data is ready to respond before

Synchronous non-blocking

When the disk is copied from disk to kernel memory , it keeps asking if the kernel data is ready to complete . Blind etc.

Performance may be worse , it seems he can do something else , but in fact he is constantly circulating .

But there's a certain amount of flexibility.

Disadvantage : can not handle multiple I/O, such as user open file , CTRL + C want to terminate this operation , is unable to stop

Synchronous IO

If the second segment is blocked , the representation is synchronous

The first , second , io multiplexing , event-driven , are all synchronous .

Asynchronous IO

Kernel backend handles itself  , Take a lot of time to handle user requests

From for notes (Wiz)



Five models of I/O

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.