linux手冊介紹sendfile函數:
NAME
sendfile - transfer data between file descriptors
SYNOPSIS
#include <sys/sendfile.h> ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
DESCRIPTION
sendfile() copies data between one file descriptor and another. Because this copying is done within the kernel, sendfile() is more efficient than the combination of read(2) and write(2), which would require transferring data to and from user space. in_fd should be a file descriptor opened for reading and out_fd should be a descriptor opened for writing. If offset is not NULL, then it points to a variable holding the file offset from which sendfile() will start reading data from in_fd. When sendfile() returns, this variable will be set to the offset of the byte following the last byte that was read. If offset is not NULL, then sendfile() does not modify the file offset of in_fd; otherwise the file offset is adjusted to reflect the number of bytes read from in_fd. If offset is NULL, then data will be read from in_fd starting at the file offset, and the file offset will be updated by the call. count is the number of bytes to copy between the file descriptors. The in_fd argument must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket).
(
in_fd必須是一個支持類似mmap函數的文件描述符,即它必須指向真實的文件,不能是socket和管道
) In Linux kernels before 2.6.33, out_fd must refer to a socket. Since Linux 2.6.33 it can be any file. If it is a regular file, then sendfile() changes the file offset appropriately.
(從上面說的可以看出:in_fd必須指向真實的文件,而out_fd在2.6.33可以是任何fd(不一定是socket。由此可見,sendfile幾乎是專門為在網絡上傳輸文件而設計的。)
RETURN VALUE
If the transfer was successful, the number of bytes written to out_fd is returned. Note that a successful call to sendfile() may write fewer bytes than requested; the caller should be prepared to retry the call if there were unsent bytes. See also NOTES. On error, -1 is returned, and errno is set appropriately.
ERRORS
EAGAIN Nonblocking I/O has been selected using O_NONBLOCK and the write would block. EBADF The input file was not opened for reading or the output file was not opened for writing. EFAULT Bad address. EINVAL Descriptor is not valid or locked, or an mmap(2)-like operation is not available for in_fd, or count is negative. EINVAL out_fd has the O_APPEND flag set. This is not currently supported by sendfile(). EIO Unspecified error while reading from in_fd. ENOMEM Insufficient memory to read from in_fd. EOVERFLOW count is too large, the operation would result in exceeding the maximum size of either the input file or the output file. ESPIPE offset is not NULL but the input file is not seek(2)-able.
VERSIONS
sendfile() first appeared in Linux 2.2. The include file <sys/sendfile.h> is present since glibc 2.1.
CONFORMING TO
Not specified in POSIX.1-2001, nor in other standards. Other UNIX systems implement sendfile() with different semantics and prototypes. It should not be used in portable programs.
NOTES
sendfile() will transfer at most 0x7ffff000 (2,147,479,552) bytes, returning the number of bytes actually transferred. (This is true on both 32-bit and 64-bit systems.) If you plan to use sendfile() for sending files to a TCP socket, but need to send some header data in front of the file contents, you will find it useful to employ the TCP_CORK option, described in tcp(7), to minimize the number of packets and to tune performance. In Linux 2.4 and earlier, out_fd could also refer to a regular file; this possibility went away in the Linux 2.6.x kernel series, but was restored in Linux 2.6.33. The original Linux sendfile() system call was not designed to handle large file offsets. Consequently, Linux 2.4 added sendfile64(), with a wider type for the offset argument. The glibc sendfile() wrapper function transparently deals with the kernel differences. Applications may wish to fall back to read(2)/write(2) in the case where sendfile() fails with EINVAL or ENOSYS. If out_fd refers to a socket or pipe with zero-copy support, callers must ensure the transferred portions of the file referred to by in_fd remain unmodified until the reader on the other end of out_fd has consumed the transferred data. The Linux-specific splice(2) call supports transferring data between arbitrary file descriptors provided one (or both) of them is a pipe.
http://man7.org/linux/man-pages/man2/sendfile.2.html
首先我們來看看傳統的read/write方式進行socket的傳輸。
當需要對一個文件進行傳輸的時候,具體流程細節如下:
1:調用read函數,文件數據copy到內核緩沖區
2:read函數返回,文件數據從內核緩沖區copy到用戶緩沖區
3:write函數調用,將文件數據從用戶緩沖區copy到內核與socket相關的緩沖區
4:數據從socket緩沖區copy到相關協議引擎。
在這個過程中發生了四次copy操作。
硬盤->內核->用戶->socket緩沖區(內核)->協議引擎。
而sendfile的工作原理呢??
1、系統調用 sendfile() 通過 DMA 把硬盤數據拷貝到 kernel buffer,然后數據被 kernel 直接拷貝到另外一個與 socket 相關的 kernel buffer。這里沒有 用戶態和核心態 之間的切換,在內核中直接完成了從一個 buffer 到另一個 buffer 的拷貝。
2、DMA 把數據從 kernel buffer 直接拷貝給協議棧,沒有切換,也不需要數據從用戶態和核心態,因為數據就在 kernel 里。
————————————————
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <sys/sendfile.h> int main(int argc,char *argv[]) { if (argc <= 3) { printf("usage:%s ip port filename\n",argv[0]); return 1; } const char *ip = argv[1]; int port = atoi(argv[2]); const char *file_name = argv[3]; int filefd = open(file_name,O_RDONLY); assert(filefd > 0); struct stat file_stat; //為了獲取文件大小 fstat(filefd,&file_stat); struct sockaddr_in address; bzero(&address,sizeof(address)); address.sin_family = AF_INET; inet_pton(AF_INET,ip,&address.sin_addr); address.sin_port = htons(port); int sock = socket(PF_INET,SOCK_STREAM,0); assert(sock >= 0); int reuse = 1; setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)); int ret = bind(sock,(struct sockaddr *)&address,sizeof(address)); assert(ret != -1); ret = listen(sock,5); assert(ret != -1); struct sockaddr_in client; socklen_t client_addrlen = sizeof(client); int connfd = accept(sock,(struct sockaddr *)&client,&client_addrlen); if (connfd < 0) { printf("errno is :%d\n",errno); }else { sendfile(connfd,filefd,NULL,file_stat.st_size); close(filefd); close(connfd); } close(sock); return 0; }
g++ sendfile.c -o sendfile #服務器端 [hadoop@master Linux]$ ./sendfile master 5432 makefile #客戶端 [root@slave07]~# telnet 10.10.18.229 5432 Trying 10.10.18.229... Connected to master (10.10.18.229). Escape character is '^]'. all:send rec send:send.c gcc $? -o $@ rec:receve.c gcc $? -o $@ Connection closed by foreign host.