Benefits of Linux Sendfile

Source: Internet
Author: User
Tags file copy sendfile

The Sendfile function passes data directly between the two file descriptors ( operating completely in the kernel, transmitting ), thus avoiding the copying between the kernel buffer data and the user buffer data. The operation is highly efficient and is called a 0 copy.

the Sendfile function is defined 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 way), in the implementation is actually more complex, need to go through the context of the switch, we look at the following two lines of code:

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, the details of the process are as follows:

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

2. read function return, file data from kernel buffer copy to user buffer

3. Write function call, copy file data from user buffer to kernel buffer associated with socket.

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

The above details are the traditional read/write way of network File transfer, we can see that in this process, the file data is actually four copy operation:

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 and improve file transfer performance. The Sendfile system call was introduced at the 2.1 kernel:

1. sendfile (socket, file, Len);  

The running 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 method, the introduction of the version 2.1 kernel Sendfile has reduced the kernel buffer to the user buffer, and then from the user buffer to the socket-related buffer file copy, After kernel version 2.4, the file descriptor results were changed, Sendfile implemented a simpler way, the system is still called the same way, the details and the 2.1 version of the difference is that when the file data is copied to the kernel buffer, no longer copy all the data to the socket-related buffer, Instead, only data related to the location and length of the record data is saved to the socket-related cache, and the actual data is sent directly by the DMA module to the protocol engine, reducing the copy operation once again.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Benefits of Linux Sendfile

Related Article

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.