Benefits of Linux Sendfile

Source: Internet
Author: User
Tags file copy sendfile

The Sendfile function passes data directly between two file descriptor descriptors ( operating in the kernel and transmitting ), thus avoiding the copying between the kernel buffer data and the user buffer data. The operating efficiency is very high, known as 0 copies.

the definition of the Sendfile function is as follows:

#include <sys/sendfile.h>

ssize_t sendfile (int out_fd,int in_fd,off_t*offset,size_t count);

Traditional wayRead/write send/recv

In the traditional file transfer inside (read/write mode), in the implementation of the fact is more complex, need to go through the context of the switch. Let's take a look at the following two lines of code, for example:

1. Read (file, tmp_buf, Len);

2. Write (socket, tmp_buf, Len);

The above two lines of code are the traditional Read/write way for file-to-socket transmission.

When a file needs to be transferred, its detailed process details such as the following:

1. Call the Read function to copy the file data to the kernel buffer

2. The Read function returns. Copy the file data from the kernel buffer to the user buffer

3. Write function call. Copy the file data from the user buffer to the kernel's socket-related buffer.

4, the data from the socket buffer copy to the relevant protocol engine.

The above details are the traditional read/write way of transmitting network files, which we can see in this process. The file data is actually four copy operations:

hard disk, kernel buf-> user Buf->socket related buffers (Kernel) Protocol engine

New Way Sendfile

The Sendfile system call provides a way to reduce the number of copy above. Ways to improve file transfer performance.

The Sendfile system call was introduced when the kernel of the 2.1 version number:

1. sendfile (socket, file, Len);  

The execution process is as follows:

1, sendfile system call, file data is copied to the kernel buffer

2. Copy from kernel buffer to socket-related buffer in kernel

3. Finally, the socket-related buffer copy to the Protocol engine

Compared to the traditional read/write approach, the introduction of the version 2.1 kernel Sendfile has lowered the kernel buffer to the user buffer. From the user buffer to the socket-related buffer file copy, and after the kernel version number 2.4, the file Description descriptor results are changed, sendfile implementation of the more simple way, the system is still called the same way, the details and the 2.1 version number of the difference is that, When the file data is copied to the kernel buffer, it will not copy all the data to the socket-related buffer, but only the data location and length of the record data is saved to the socket related cache, and the actual data will be sent directly to the protocol engine by the DMA module, and the copy operation is reduced once again.

Benefits of Linux Sendfile

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.