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