Linux "0 Copy" Sendfile function Chinese description and practical operation

Source: Internet
Author: User
Tags sendfile

Sendfile function Description  
#include  
ssize_t sendfile (int out_fd, int in_fd, off_t *offset, size_t count); 

Sendfile () is an action function for copying data between two file descriptors. This copy operation is operating in the kernel, so it is called a "0 copy". The Sendfile function is much more efficient than the read and write functions, Because read and write are copies of the data to the user application layer operation.  

Parameter description:  
Out_fd is a file descriptor that has already been opened for writing (write);
In_fd is already open A file descriptor for the read operation (read);  
offset offset; Indicates the Sendfile function reads the data from which offset in the in_fd. If it is zero, it is read from the beginning of the file, otherwise it is read from the corresponding offset. If it is a cyclic read, The next offset value should be the value of the Sendfile function return value plus the offset for this time. Offset is the incoming outgoing parameter, which is self-increasing when you pass a large file with a loop.
Count is the number of bytes copied between two descriptors (bytes)  

return value:  
If a successful copy returns the number of bytes written to OUT_FD, the error returns-1, and the appropriate setting error information.  

Eagain No blocking I/O settings o_nonblock, write operation (write) is blocked.  
EBADF output or input file descriptor is not open.  
Efault the wrong address.   The
EINVAL descriptor is unavailable or locked, or in_fd that are operated with the mmap () function are not available.  
EIO An unknown error occurred while reading in_fd.  
Enomem Read (read) Insufficient memory when in_fd.

Off = 0;while (ret = Sendfile (new_fd,fd,&off,4096)) >0)     {          if (ret = =-1)         {             perror ("Sendfile");         }    }

  

Linux "0 Copy" Sendfile function Chinese description and practical operation

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.