Read/write data read/write Transmission Mode

Source: Internet
Author: User
Preface

I intend to write a blog post explaining the standard I/O (Cache I/O), but I find that some netizens have done the same job and the quality of work is superior.

Original address http://lenky.info/archives/2012/08/1856

Body

Using the system call function read ()/write () is the most commonly used data read/write method. In most cases, we do not consider the execution efficiency of this data read/write method, the execution time consumed by the read ()/write () function in many applications that do not use frequent data read/write as the performance bottleneck can be ignored, but what are their internal implementation and execution efficiency? The following is a detailed analysis.
The read ()/write () function is defined in the header file unistd. h. The prototype is as follows:

#include <unistd.h>ssize_t read(int fildes, void *buf, size_t nbyte);ssize_t write(int fildes, const void *buf, size_t nbyte);

Here I am not going to explain the source code of the read ()/write () function. I will briefly describe the execution process and the main call relationships involved are shown in:

 

If our server programs, such as nginx, use the read ()/write () data read/write transmission method, when a client sends a "Get/index.htm HTTP/1.1133 request, nginxneeds to send the index.htm text file stored in the site root directory to the client as response data. The Writable Text File data is read into the memory, and then the first data read into the memory is written to the connector interface descriptor to send the response data:

As shown in, nginx applications use read ()/write () in data read/write transmission mode, a total of four context switches and four data copies are required to send response data (that is, if the data in the index.htm text file is sent once read ()/write(metadata, if there are more than one switch, the number of copies will increase.) Are these switching and copying processes necessary? The answer is no. For example, when mmapis is enabled, you can copy the local file index.htm data to the kernel cache using mmap(((, and return the start value of the ing target address to the nginx application, it is precisely because the nginx application has the starting address of the ing of this kernel cache area and can share this kernel cache area (the result of the system calling MMAP (). Therefore, in the first figure, the copy from the kernel buffer to the user buffer and then to the socket buffer can be changed to a direct copy from the kernel buffer to the socket buffer:

 

 

Read/write data read/write transmission mode ()

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.