Five models of I/o and three working models of web must be understood for web optimization

Source: Internet
Author: User

Five models of I/o and three working models of web must be understood for web optimization

========================================================== ===

Five I/O models

Three working modes supported by web

========================================================== ===


Five types of I/O:

1) block I/0

2) non-blocking I/O

3) I/O multiplexing

4) event (signal)-driven I/O

5) asynchronous I/O

Why do we need to initiate a system call?

Because the process wants to obtain data in the disk, but can only deal with the hardware is the kernel, the process notifies the kernel that I want data in the disk, this process is called by the system.


Steps for completing one I/O operation

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

I/O operations are divided into two steps;

1. the disk loads data to the memory space of the kernel,

2. copy the data in the kernel's memory space to the user's memory space (this process is where I/O occurs)


The following is a detailed illustration of the process's data acquisition process;

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/16405A342-0.png "title =" 0.png"/>


The entire process: if this process requires operations on data in the disk, a system call is initiated to the kernel, and the process will be switched out, this process will be suspended or sleep, also known as non-disruptive sleep, because the data is not yet obtained. The process will be awakened only after the system call is complete, continue with the following steps:

① A process initiates a system call to the kernel,

② When the kernel receives a system call, it knows it is a file request, so it tells the disk to read the file

③ After the disk receives the kernel command, it loads the file into the memory space of the kernel.

④ After the kernel's memory space receives data, it copies the data to the memory space of the user's process (where I/O occurs)

⑤ Send a notification to the kernel after the process memory space obtains data

6. the kernel replies the received notifications to the process. This process is a wake-up Process. Then, the process obtains data and performs the next step.


Where I/O occurs, blocking or non-blocking occurs.

Blocking: The process initiates an I/O call and the process has to wait for the completion of I/O. At this time, the CPU switches the process out and the process is in sleep state.

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

When I/O is completed and the system directly notifies the process, the process is awakened.

Diagram of blocking I/O

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/16405CS8-1.png "title =" 1.png"/>

Non-blocking: The process initiates an I/O call. If I/O knows that it takes some time to complete, it immediately notifies the process to perform other operations, and the non-blocking I/O

How does the system notify the process of non-blocking I/O?

After the system completes, the process obtains the data and continues to execute the kernel data (this process is also called Blind wait)


Diagram of non-blocking I/O:


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/16405C420-2.png "title =" 2.png"/>


I/O reuse diagram:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1640564463-3.png "title =" 3.png"/>

Event (signal)-driven I/O diagram:

The event-driven mechanism triggered horizontally. The kernel notifies the process to read data. The process does not read data. The kernel needs to notify the process once;

Edge-triggered event-driven mechanism. The kernel only notifies the process to read data once, and the process can read data at any time within the time-out period.

Nginx adopts the edge-triggered event-driven mechanism, which is why nginx has better concurrency than apache. Of course, nginx has better performance than apache, and there are other aspects, for example, nginx supports asynchronous I/O and mmap (memory ing ).

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1640561328-4.png "title =" 4.png"/>


Asynchronous I/O diagram:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/16405CW6-5.png "title =" 5.png"/>

The first four types of I/O are synchronous operations, and the last one is asynchronous operations.


Comparison of five I/O models:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/16405CN8-6.png "title =" 6.png"/>

Three working modes of web

How Prefork works

The main process generates multiple worker processes, and the worker process responds to client requests one by one.

How Prefork works:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1640563458-7.png "title =" 11.png"/>

How Worker works

The main process generates multiple worker processes. Each worker process generates multiple threads and each thread goes

Respond to client requests

Schematic Worker working principle:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/16405BH9-8.png "title =" 12.png"/>

How Event works

The main process generates multiple worker processes. Each engineering process responds to requests from multiple clients.

After the I/O operation request to the client, the I/O operation is handed over to the kernel for execution, and the process responds to other

Client request, the process finally receives a notification from the kernel, and then replies to the client through this process

Request result, through the Event Callback Function

How Event works:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1640561H2-9.png "title =" 13.png"/>


This blog is based on reading relevant articles and writing my own understanding of I/O, which may not be accurate. I hope you can give me more advice. Thank you for your advice.


This article is from the "technical path-NLP" blog, please be sure to keep this source http://litaotao.blog.51cto.com/6224470/1289790

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.