{"id":195584,"date":"2020-07-19T08:12:00","date_gmt":"2020-07-19T00:12:00","guid":{"rendered":"https:\/\/gulass.cn\/?p=195584"},"modified":"2020-07-07T09:12:57","modified_gmt":"2020-07-07T01:12:57","slug":"linux-zero-copy-technology","status":"publish","type":"post","link":"https:\/\/gulass.cn\/linux-zero-copy-technology.html","title":{"rendered":"Linux\u96f6\u62f7\u8d1d\u6280\u672f\u6d45\u6790"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u63a2\u8ba8Linux\u4e2d\u4e3b\u8981\u7684\u51e0\u79cd\u96f6\u62f7\u8d1d\u6280\u672f\u4ee5\u53ca\u96f6\u62f7\u8d1d\u6280\u672f\u9002\u7528\u7684\u573a\u666f\u3002\u4e3a\u4e86\u8fc5\u901f\u5efa\u7acb\u8d77\u96f6\u62f7\u8d1d\u7684\u6982\u5ff5\uff0c\u6211\u4eec\u62ff\u4e00\u4e2a\u5e38\u7528\u7684\u573a\u666f\u8fdb\u884c\u5f15\u5165\u96f6\u62f7\u8d1d\u7684\u76f8\u5173\u77e5\u8bc6\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u96f6\u62f7\u8d1d\u5f15\u6587<\/strong><\/div>\n

\u5728\u5199\u4e00\u4e2a\u670d\u52a1\u7aef\u7a0b\u5e8f\u65f6\uff08Web Server\u6216\u8005\u6587\u4ef6\u670d\u52a1\u5668\uff09\uff0c\u6587\u4ef6\u4e0b\u8f7d\u662f\u4e00\u4e2a\u57fa\u672c\u529f\u80fd\u3002\u8fd9\u65f6\u5019\u670d\u52a1\u7aef\u7684\u4efb\u52a1\u662f\uff1a\u5c06\u670d\u52a1\u7aef\u4e3b\u673a\u78c1\u76d8\u4e2d\u7684\u6587\u4ef6\u4e0d\u505a\u4fee\u6539\u5730\u4ece\u5df2\u8fde\u63a5\u7684socket\u53d1\u51fa\u53bb\uff0c\u6211\u4eec\u901a\u5e38\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u5b8c\u6210\uff1a<\/p>\n

while((n = read(diskfd, buf, BUF_SIZE)) > 0)  \r\n    write(sockfd, buf , n); \r\n<\/pre>\n

\u57fa\u672c\u64cd\u4f5c\u5c31\u662f\u5faa\u73af\u7684\u4ece\u78c1\u76d8\u8bfb\u5165\u6587\u4ef6\u5185\u5bb9\u5230\u7f13\u51b2\u533a\uff0c\u518d\u5c06\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u53d1\u9001\u5230socket\u3002\u4f46\u662f\u7531\u4e8eLinux\u7684I\/O\u64cd\u4f5c\u9ed8\u8ba4\u662f\u7f13\u51b2I\/O\u3002\u8fd9\u91cc\u9762\u4e3b\u8981\u4f7f\u7528\u7684\u4e5f\u5c31\u662fread\u548cwrite\u4e24\u4e2a\u7cfb\u7edf\u8c03\u7528\uff0c\u6211\u4eec\u5e76\u4e0d\u77e5\u9053\u64cd\u4f5c\u7cfb\u7edf\u5728\u5176\u4e2d\u505a\u4e86\u4ec0\u4e48\u3002\u5b9e\u9645\u4e0a\u5728\u4ee5\u4e0aI\/O\u64cd\u4f5c\u4e2d\uff0c\u53d1\u751f\u4e86\u591a\u6b21\u7684\u6570\u636e\u62f7\u8d1d\u3002<\/p>\n

\u5f53\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u67d0\u5757\u6570\u636e\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u9996\u5148\u4f1a\u68c0\u67e5\uff0c\u662f\u4e0d\u662f\u6700\u8fd1\u8bbf\u95ee\u8fc7\u6b64\u6587\u4ef6\uff0c\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u7f13\u5b58\u5728\u5185\u6838\u7f13\u51b2\u533a\uff0c\u5982\u679c\u662f\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5219\u76f4\u63a5\u6839\u636eread\u7cfb\u7edf\u8c03\u7528\u63d0\u4f9b\u7684buf\u5730\u5740\uff0c\u5c06\u5185\u6838\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u62f7\u8d1d\u5230buf\u6240\u6307\u5b9a\u7684\u7528\u6237\u7a7a\u95f4\u7f13\u51b2\u533a\u4e2d\u53bb\u3002\u5982\u679c\u4e0d\u662f\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5219\u9996\u5148\u5c06\u78c1\u76d8\u4e0a\u7684\u6570\u636e\u62f7\u8d1d\u7684\u5185\u6838\u7f13\u51b2\u533a\uff0c\u8fd9\u4e00\u6b65\u76ee\u524d\u4e3b\u8981\u4f9d\u9760DMA\u6765\u4f20\u8f93\uff0c\u7136\u540e\u518d\u628a\u5185\u6838\u7f13\u51b2\u533a\u4e0a\u7684\u5185\u5bb9\u62f7\u8d1d\u5230\u7528\u6237\u7f13\u51b2\u533a\u4e2d\u3002<\/p>\n

\u63a5\u4e0b\u6765\uff0cwrite\u7cfb\u7edf\u8c03\u7528\u518d\u628a\u7528\u6237\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u62f7\u8d1d\u5230\u7f51\u7edc\u5806\u6808\u76f8\u5173\u7684\u5185\u6838\u7f13\u51b2\u533a\u4e2d\uff0c\u6700\u540esocket\u518d\u628a\u5185\u6838\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u53d1\u9001\u5230\u7f51\u5361\u4e0a\u3002\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u4e0d\u5982\u770b\u56fe\u6e05\u695a\uff1a<\/p>\n

\"\"<\/p>\n

\u4ece\u4e0a\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u5171\u4ea7\u751f\u4e86\u56db\u6b21\u6570\u636e\u62f7\u8d1d\uff0c\u5373\u4f7f\u4f7f\u7528\u4e86DMA\u6765\u5904\u7406\u4e86\u4e0e\u786c\u4ef6\u7684\u901a\u8baf\uff0cCPU\u4ecd\u7136\u9700\u8981\u5904\u7406\u4e24\u6b21\u6570\u636e\u62f7\u8d1d\uff0c\u4e0e\u6b64\u540c\u65f6\uff0c\u5728\u7528\u6237\u6001\u4e0e\u5185\u6838\u6001\u4e5f\u53d1\u751f\u4e86\u591a\u6b21\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u65e0\u7591\u4e5f\u52a0\u91cd\u4e86CPU\u8d1f\u62c5\u3002<\/p>\n

\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u5bf9\u6587\u4ef6\u5185\u5bb9\u505a\u4efb\u4f55\u4fee\u6539\uff0c\u90a3\u4e48\u5728\u5185\u6838\u7a7a\u95f4\u548c\u7528\u6237\u7a7a\u95f4\u6765\u56de\u62f7\u8d1d\u6570\u636e\u65e0\u7591\u5c31\u662f\u4e00\u79cd\u6d6a\u8d39\uff0c\u800c\u96f6\u62f7\u8d1d\u4e3b\u8981\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u79cd\u4f4e\u6548\u6027\u3002<\/p>\n

\u4ec0\u4e48\u662f\u96f6\u62f7\u8d1d\u6280\u672f\uff08zero-copy\uff09\uff1f<\/strong><\/div>\n

\u96f6\u62f7\u8d1d\u4e3b\u8981\u7684\u4efb\u52a1\u5c31\u662f\u907f\u514dCPU\u5c06\u6570\u636e\u4ece\u4e00\u5757\u5b58\u50a8\u62f7\u8d1d\u5230\u53e6\u5916\u4e00\u5757\u5b58\u50a8\uff0c\u4e3b\u8981\u5c31\u662f\u5229\u7528\u5404\u79cd\u96f6\u62f7\u8d1d\u6280\u672f\uff0c\u907f\u514d\u8ba9CPU\u505a\u5927\u91cf\u7684\u6570\u636e\u62f7\u8d1d\u4efb\u52a1\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u6216\u8005\u8ba9\u522b\u7684\u7ec4\u4ef6\u6765\u505a\u8fd9\u4e00\u7c7b\u7b80\u5355\u7684\u6570\u636e\u4f20\u8f93\u4efb\u52a1\uff0c\u8ba9CPU\u89e3\u8131\u51fa\u6765\u4e13\u6ce8\u4e8e\u522b\u7684\u4efb\u52a1\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u8ba9\u7cfb\u7edf\u8d44\u6e90\u7684\u5229\u7528\u66f4\u52a0\u6709\u6548\u3002<\/p>\n

\u6211\u4eec\u7ee7\u7eed\u56de\u5230\u5f15\u6587\u4e2d\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u5982\u4f55\u51cf\u5c11\u6570\u636e\u62f7\u8d1d\u7684\u6b21\u6570\u5462\uff1f\u4e00\u4e2a\u5f88\u660e\u663e\u7684\u7740\u529b\u70b9\u5c31\u662f\u51cf\u5c11\u6570\u636e\u5728\u5185\u6838\u7a7a\u95f4\u548c\u7528\u6237\u7a7a\u95f4\u6765\u56de\u62f7\u8d1d\uff0c\u8fd9\u4e5f\u5f15\u5165\u4e86\u96f6\u62f7\u8d1d\u7684\u4e00\u4e2a\u7c7b\u578b\uff1a\u8ba9\u6570\u636e\u4f20\u8f93\u4e0d\u9700\u8981\u7ecf\u8fc7user space\u3002<\/p>\n

mmap\u6280\u672f<\/strong><\/div>\n

\u6211\u4eec\u51cf\u5c11\u62f7\u8d1d\u6b21\u6570\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u8c03\u7528mmap()\u6765\u4ee3\u66ffread\u8c03\u7528\uff1a<\/p>\n

buf = mmap(diskfd, len);  \r\nwrite(sockfd, buf, len); \r\n<\/pre>\n

\u5e94\u7528\u7a0b\u5e8f\u8c03\u7528mmap()\uff0c\u78c1\u76d8\u4e0a\u7684\u6570\u636e\u4f1a\u901a\u8fc7DMA\u88ab\u62f7\u8d1d\u7684\u5185\u6838\u7f13\u51b2\u533a\uff0c\u63a5\u7740\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u628a\u8fd9\u6bb5\u5185\u6838\u7f13\u51b2\u533a\u4e0e\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\uff0c\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u628a\u5185\u6838\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u5f80\u7528\u6237\u7a7a\u95f4\u62f7\u8d1d\u3002\u5e94\u7528\u7a0b\u5e8f\u518d\u8c03\u7528write(),\u64cd\u4f5c\u7cfb\u7edf\u76f4\u63a5\u5c06\u5185\u6838\u7f13\u51b2\u533a\u7684\u5185\u5bb9\u62f7\u8d1d\u5230socket\u7f13\u51b2\u533a\u4e2d\uff0c\u8fd9\u4e00\u5207\u90fd\u53d1\u751f\u5728\u5185\u6838\u6001\uff0c\u6700\u540e\uff0csocket\u7f13\u51b2\u533a\u518d\u628a\u6570\u636e\u53d1\u5230\u7f51\u5361\u53bb\u3002\u540c\u6837\u7684\uff0c\u770b\u56fe\u5f88\u7b80\u5355\uff1a<\/p>\n

\"\"<\/p>\n

\u4f7f\u7528mmap\u66ff\u4ee3read\u5f88\u660e\u663e\u51cf\u5c11\u4e86\u4e00\u6b21\u62f7\u8d1d\uff0c\u5f53\u62f7\u8d1d\u6570\u636e\u91cf\u5f88\u5927\u65f6\uff0c\u65e0\u7591\u63d0\u5347\u4e86\u6548\u7387\u3002\u4f46\u662f\u4f7f\u7528mmap\u662f\u6709\u4ee3\u4ef7\u7684\u3002\u5f53\u4f60\u4f7f\u7528mmap\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u9690\u85cf\u7684\u9677\u9631\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u7684\u7a0b\u5e8fmap\u4e86\u4e00\u4e2a\u6587\u4ef6\uff0c\u4f46\u662f\u5f53\u8fd9\u4e2a\u6587\u4ef6\u88ab\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u622a\u65ad(truncate)\u65f6, write\u7cfb\u7edf\u8c03\u7528\u4f1a\u56e0\u4e3a\u8bbf\u95ee\u975e\u6cd5\u5730\u5740\u800c\u88abSIGBUS\u4fe1\u53f7\u7ec8\u6b62\u3002SIGBUS\u4fe1\u53f7\u9ed8\u8ba4\u4f1a\u6740\u6b7b\u4f60\u7684\u8fdb\u7a0b\u5e76\u4ea7\u751f\u4e00\u4e2acoredump,\u5982\u679c\u4f60\u7684\u670d\u52a1\u5668\u8fd9\u6837\u88ab\u4e2d\u6b62\u4e86\uff0c\u90a3\u4f1a\u4ea7\u751f\u4e00\u7b14\u635f\u5931\u3002<\/p>\n

\u901a\u5e38\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u89e3\u51b3\u65b9\u6848\u907f\u514d\u8fd9\u79cd\u95ee\u9898\uff1a<\/p>\n

1. \u4e3aSIGBUS\u4fe1\u53f7\u5efa\u7acb\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f<\/strong><\/span><\/div>\n

\u5f53\u9047\u5230SIGBUS\u4fe1\u53f7\u65f6\uff0c\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\u7b80\u5355\u5730\u8fd4\u56de\uff0cwrite\u7cfb\u7edf\u8c03\u7528\u5728\u88ab\u4e2d\u65ad\u4e4b\u524d\u4f1a\u8fd4\u56de\u5df2\u7ecf\u5199\u5165\u7684\u5b57\u8282\u6570\uff0c\u5e76\u4e14errno\u4f1a\u88ab\u8bbe\u7f6e\u6210success,\u4f46\u662f\u8fd9\u662f\u4e00\u79cd\u7cdf\u7cd5\u7684\u5904\u7406\u529e\u6cd5\uff0c\u56e0\u4e3a\u4f60\u5e76\u6ca1\u6709\u89e3\u51b3\u95ee\u9898\u7684\u5b9e\u8d28\u6838\u5fc3\u3002<\/p>\n

2. \u4f7f\u7528\u6587\u4ef6\u79df\u501f\u9501<\/strong><\/span><\/div>\n

\u901a\u5e38\u6211\u4eec\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u4f7f\u7528\u79df\u501f\u9501\uff0c\u6211\u4eec\u4e3a\u6587\u4ef6\u5411\u5185\u6838\u7533\u8bf7\u4e00\u4e2a\u79df\u501f\u9501\uff0c\u5f53\u5176\u5b83\u8fdb\u7a0b\u60f3\u8981\u622a\u65ad\u8fd9\u4e2a\u6587\u4ef6\u65f6\uff0c\u5185\u6838\u4f1a\u5411\u6211\u4eec\u53d1\u9001\u4e00\u4e2a\u5b9e\u65f6\u7684RTSIGNALLEASE\u4fe1\u53f7\uff0c\u544a\u8bc9\u6211\u4eec\u5185\u6838\u6b63\u5728\u7834\u574f\u4f60\u52a0\u6301\u5728\u6587\u4ef6\u4e0a\u7684\u8bfb\u5199\u9501\u3002\u8fd9\u6837\u5728\u7a0b\u5e8f\u8bbf\u95ee\u975e\u6cd5\u5185\u5b58\u5e76\u4e14\u88abSIGBUS\u6740\u6b7b\u4e4b\u524d\uff0c\u4f60\u7684write\u7cfb\u7edf\u8c03\u7528\u4f1a\u88ab\u4e2d\u65ad\u3002write\u4f1a\u8fd4\u56de\u5df2\u7ecf\u5199\u5165\u7684\u5b57\u8282\u6570\uff0c\u5e76\u4e14\u7f6eerrno\u4e3asuccess\u3002<\/p>\n

\u6211\u4eec\u5e94\u8be5\u5728mmap\u6587\u4ef6\u4e4b\u524d\u52a0\u9501\uff0c\u5e76\u4e14\u5728\u64cd\u4f5c\u5b8c\u6587\u4ef6\u540e\u89e3\u9501\uff1a<\/p>\n

if(fcntl(diskfd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {  \r\n       perror(\"kernel lease set signal\");  \r\n   return -1;  \r\n   }  \r\n   \/* l_type can be F_RDLCK F_WRLCK  \u52a0\u9501*\/  \r\n   \/* l_type can be  F_UNLCK \u89e3\u9501*\/  \r\n   if(fcntl(diskfd, F_SETLEASE, l_type)){  \r\n       perror(\"kernel lease set type\");  \r\n   return -1;  \r\n   } \r\n<\/pre>\n
sendfile\u6280\u672f<\/strong><\/div>\n

\u4ece2.1\u7248\u5185\u6838\u5f00\u59cb\uff0cLinux\u5f15\u5165\u4e86sendfile\u6765\u7b80\u5316\u64cd\u4f5c:<\/p>\n

#include<sys\/sendfile.h>  \r\nssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count); \r\n<\/pre>\n

\u7cfb\u7edf\u8c03\u7528sendfile()\u5728\u4ee3\u8868\u8f93\u5165\u6587\u4ef6\u7684\u63cf\u8ff0\u7b26infd\u548c\u4ee3\u8868\u8f93\u51fa\u6587\u4ef6\u7684\u63cf\u8ff0\u7b26outfd\u4e4b\u95f4\u4f20\u9001\u6587\u4ef6\u5185\u5bb9\uff08\u5b57\u8282\uff09\u3002\u63cf\u8ff0\u7b26outfd\u5fc5\u987b\u6307\u5411\u4e00\u4e2a\u5957\u63a5\u5b57\uff0c\u800cinfd\u6307\u5411\u7684\u6587\u4ef6\u5fc5\u987b\u662f\u53ef\u4ee5mmap\u7684\u3002\u8fd9\u4e9b\u5c40\u9650\u9650\u5236\u4e86sendfile\u7684\u4f7f\u7528\uff0c\u4f7fsendfile\u53ea\u80fd\u5c06\u6570\u636e\u4ece\u6587\u4ef6\u4f20\u9012\u5230\u5957\u63a5\u5b57\u4e0a\uff0c\u53cd\u4e4b\u5219\u4e0d\u884c\u3002<\/p>\n

\u4f7f\u7528sendfile\u4e0d\u4ec5\u51cf\u5c11\u4e86\u6570\u636e\u62f7\u8d1d\u7684\u6b21\u6570\uff0c\u8fd8\u51cf\u5c11\u4e86\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u6570\u636e\u4f20\u9001\u59cb\u7ec8\u53ea\u53d1\u751f\u5728kernel space\u3002<\/p>\n

\"\"<\/p>\n

\u5728\u6211\u4eec\u8c03\u7528sendfile\u65f6\uff0c\u5982\u679c\u6709\u5176\u5b83\u8fdb\u7a0b\u622a\u65ad\u4e86\u6587\u4ef6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f\u5047\u8bbe\u6211\u4eec\u6ca1\u6709\u8bbe\u7f6e\u4efb\u4f55\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\uff0csendfile\u8c03\u7528\u4ec5\u4ec5\u8fd4\u56de\u5b83\u5728\u88ab\u4e2d\u65ad\u4e4b\u524d\u5df2\u7ecf\u4f20\u8f93\u7684\u5b57\u8282\u6570\uff0cerrno\u4f1a\u88ab\u7f6e\u4e3asuccess\u3002\u5982\u679c\u6211\u4eec\u5728\u8c03\u7528sendfile\u4e4b\u524d\u7ed9\u6587\u4ef6\u52a0\u4e86\u9501\uff0csendfile\u7684\u884c\u4e3a\u4ecd\u7136\u548c\u4e4b\u524d\u76f8\u540c\uff0c\u6211\u4eec\u8fd8\u4f1a\u6536\u5230RTSIGNALLEASE\u7684\u4fe1\u53f7\u3002<\/p>\n

\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u51cf\u5c11\u4e86\u6570\u636e\u62f7\u8d1d\u7684\u6b21\u6570\u4e86\uff0c\u4f46\u662f\u4ecd\u7136\u5b58\u5728\u4e00\u6b21\u62f7\u8d1d\uff0c\u5c31\u662f\u9875\u7f13\u5b58\u5230socket\u7f13\u5b58\u7684\u62f7\u8d1d\u3002\u90a3\u4e48\u80fd\u4e0d\u80fd\u628a\u8fd9\u4e2a\u62f7\u8d1d\u4e5f\u7701\u7565\u5462\uff1f<\/p>\n

\u501f\u52a9\u4e8e\u786c\u4ef6\u4e0a\u7684\u5e2e\u52a9\uff0c\u6211\u4eec\u662f\u53ef\u4ee5\u529e\u5230\u7684\u3002\u4e4b\u524d\u6211\u4eec\u662f\u628a\u9875\u7f13\u5b58\u7684\u6570\u636e\u62f7\u8d1d\u5230socket\u7f13\u5b58\u4e2d\uff0c\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u4ec5\u4ec5\u9700\u8981\u628a\u7f13\u51b2\u533a\u63cf\u8ff0\u7b26\u4f20\u5230socket\u7f13\u51b2\u533a\uff0c\u518d\u628a\u6570\u636e\u957f\u5ea6\u4f20\u8fc7\u53bb\uff0c\u8fd9\u6837DMA\u63a7\u5236\u5668\u76f4\u63a5\u5c06\u9875\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u6253\u5305\u53d1\u9001\u5230\u7f51\u7edc\u4e2d\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n

\u603b\u7ed3\u4e00\u4e0b\uff0csendfile\u7cfb\u7edf\u8c03\u7528\u5229\u7528DMA\u5f15\u64ce\u5c06\u6587\u4ef6\u5185\u5bb9\u62f7\u8d1d\u5230\u5185\u6838\u7f13\u51b2\u533a\u53bb\uff0c\u7136\u540e\u5c06\u5e26\u6709\u6587\u4ef6\u4f4d\u7f6e\u548c\u957f\u5ea6\u4fe1\u606f\u7684\u7f13\u51b2\u533a\u63cf\u8ff0\u7b26\u6dfb\u52a0socket\u7f13\u51b2\u533a\u53bb\uff0c\u8fd9\u4e00\u6b65\u4e0d\u4f1a\u5c06\u5185\u6838\u4e2d\u7684\u6570\u636e\u62f7\u8d1d\u5230socket\u7f13\u51b2\u533a\u4e2d\uff0cDMA\u5f15\u64ce\u4f1a\u5c06\u5185\u6838\u7f13\u51b2\u533a\u7684\u6570\u636e\u62f7\u8d1d\u5230\u534f\u8bae\u5f15\u64ce\u4e2d\u53bb\uff0c\u907f\u514d\u4e86\u6700\u540e\u4e00\u6b21\u62f7\u8d1d\u3002<\/p>\n

\"\"<\/p>\n

\u4e0d\u8fc7\u8fd9\u4e00\u79cd\u6536\u96c6\u62f7\u8d1d\u529f\u80fd\u662f\u9700\u8981\u786c\u4ef6\u4ee5\u53ca\u9a71\u52a8\u7a0b\u5e8f\u652f\u6301\u7684\u3002<\/p>\n

\u4f7f\u7528splice<\/strong><\/div>\n

sendfile\u53ea\u9002\u7528\u4e8e\u5c06\u6570\u636e\u4ece\u6587\u4ef6\u62f7\u8d1d\u5230\u5957\u63a5\u5b57\u4e0a\uff0c\u9650\u5b9a\u4e86\u5b83\u7684\u4f7f\u7528\u8303\u56f4\u3002Linux\u57282.6.17\u7248\u672c\u5f15\u5165splice\u7cfb\u7edf\u8c03\u7528\uff0c\u7528\u4e8e\u5728\u4e24\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2d\u79fb\u52a8\u6570\u636e\uff1a<\/p>\n

#define _GNU_SOURCE         \/* See feature_test_macros(7) *\/  \r\n   #include  \r\n   ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsignedint flags);\r\n<\/pre>\n

splice\u8c03\u7528\u5728\u4e24\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e4b\u95f4\u79fb\u52a8\u6570\u636e\uff0c\u800c\u4e0d\u9700\u8981\u6570\u636e\u5728\u5185\u6838\u7a7a\u95f4\u548c\u7528\u6237\u7a7a\u95f4\u6765\u56de\u62f7\u8d1d\u3002\u4ed6\u4ecefdin\u62f7\u8d1dlen\u957f\u5ea6\u7684\u6570\u636e\u5230fdout\uff0c\u4f46\u662f\u6709\u4e00\u65b9\u5fc5\u987b\u662f\u7ba1\u9053\u8bbe\u5907\uff0c\u8fd9\u4e5f\u662f\u76ee\u524dsplice\u7684\u4e00\u4e9b\u5c40\u9650\u6027\u3002flags\u53c2\u6570\u6709\u4ee5\u4e0b\u51e0\u79cd\u53d6\u503c\uff1a<\/p>\n

    SPLICEFMOVE \uff1a\u5c1d\u8bd5\u53bb\u79fb\u52a8\u6570\u636e\u800c\u4e0d\u662f\u62f7\u8d1d\u6570\u636e\u3002\u8fd9\u4ec5\u4ec5\u662f\u5bf9\u5185\u6838\u7684\u4e00\u4e2a\u5c0f\u63d0\u793a\uff1a\u5982\u679c\u5185\u6838\u4e0d\u80fd\u4ecepipe\u79fb\u52a8\u6570\u636e\u6216\u8005pipe\u7684\u7f13\u5b58\u4e0d\u662f\u4e00\u4e2a\u6574\u9875\u9762\uff0c\u4ecd\u7136\u9700\u8981\u62f7\u8d1d\u6570\u636e\u3002Linux\u6700\u521d\u7684\u5b9e\u73b0\u6709\u4e9b\u95ee\u9898\uff0c\u6240\u4ee5\u4ece2.6.21\u5f00\u59cb\u8fd9\u4e2a\u9009\u9879\u4e0d\u8d77\u4f5c\u7528\uff0c\u540e\u9762\u7684Linux\u7248\u672c\u5e94\u8be5\u4f1a\u5b9e\u73b0\u3002<\/ol>\n
      SPLICEFNONBLOCK \uff1asplice \u64cd\u4f5c\u4e0d\u4f1a\u88ab\u963b\u585e\u3002\u7136\u800c\uff0c\u5982\u679c\u6587\u4ef6\u63cf\u8ff0\u7b26\u6ca1\u6709\u88ab\u8bbe\u7f6e\u4e3a\u4e0d\u53ef\u88ab\u963b\u585e\u65b9\u5f0f\u7684 I\/O \uff0c\u90a3\u4e48\u8c03\u7528 splice \u6709\u53ef\u80fd\u4ecd\u7136\u88ab\u963b\u585e\u3002<\/ol>\n
        SPLICEFMORE\uff1a\u540e\u9762\u7684splice\u8c03\u7528\u4f1a\u6709\u66f4\u591a\u7684\u6570\u636e\u3002<\/ol>\n

        splice\u8c03\u7528\u5229\u7528\u4e86Linux\u63d0\u51fa\u7684\u7ba1\u9053\u7f13\u51b2\u533a\u673a\u5236\uff0c \u6240\u4ee5\u81f3\u5c11\u4e00\u4e2a\u63cf\u8ff0\u7b26\u8981\u4e3a\u7ba1\u9053\u3002<\/p>\n

        \u4ee5\u4e0a\u51e0\u79cd\u96f6\u62f7\u8d1d\u6280\u672f\u90fd\u662f\u51cf\u5c11\u6570\u636e\u5728\u7528\u6237\u7a7a\u95f4\u548c\u5185\u6838\u7a7a\u95f4\u62f7\u8d1d\u6280\u672f\u5b9e\u73b0\u7684\uff0c\u4f46\u662f\u6709\u4e9b\u65f6\u5019\uff0c\u6570\u636e\u5fc5\u987b\u5728\u7528\u6237\u7a7a\u95f4\u548c\u5185\u6838\u7a7a\u95f4\u4e4b\u95f4\u62f7\u8d1d\u3002\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u53ea\u80fd\u9488\u5bf9\u6570\u636e\u5728\u7528\u6237\u7a7a\u95f4\u548c\u5185\u6838\u7a7a\u95f4\u62f7\u8d1d\u7684\u65f6\u673a\u4e0a\u4e0b\u529f\u592b\u4e86\u3002Linux\u901a\u5e38\u5229\u7528\u5199\u65f6\u590d\u5236(copy on write)\u6765\u51cf\u5c11\u7cfb\u7edf\u5f00\u9500\uff0c\u8fd9\u4e2a\u6280\u672f\u53c8\u65f6\u5e38\u79f0\u4f5cCOW\u3002<\/p>\n

        \u7531\u4e8e\u7bc7\u5e45\u539f\u56e0\uff0c\u672c\u6587\u4e0d\u8be6\u7ec6\u4ecb\u7ecd\u5199\u65f6\u590d\u5236\u3002\u5927\u6982\u63cf\u8ff0\u4e0b\u5c31\u662f\uff1a\u5982\u679c\u591a\u4e2a\u7a0b\u5e8f\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u5757\u6570\u636e\uff0c\u90a3\u4e48\u6bcf\u4e2a\u7a0b\u5e8f\u90fd\u62e5\u6709\u6307\u5411\u8fd9\u5757\u6570\u636e\u7684\u6307\u9488\uff0c\u5728\u6bcf\u4e2a\u7a0b\u5e8f\u770b\u6765\uff0c\u81ea\u5df1\u90fd\u662f\u72ec\u7acb\u62e5\u6709\u8fd9\u5757\u6570\u636e\u7684\uff0c\u53ea\u6709\u5f53\u7a0b\u5e8f\u9700\u8981\u5bf9\u6570\u636e\u5185\u5bb9\u8fdb\u884c\u4fee\u6539\u65f6\uff0c\u624d\u4f1a\u628a\u6570\u636e\u5185\u5bb9\u62f7\u8d1d\u5230\u7a0b\u5e8f\u81ea\u5df1\u7684\u5e94\u7528\u7a7a\u95f4\u91cc\u53bb\uff0c\u8fd9\u65f6\u5019\uff0c\u6570\u636e\u624d\u6210\u4e3a\u8be5\u7a0b\u5e8f\u7684\u79c1\u6709\u6570\u636e\u3002\u5982\u679c\u7a0b\u5e8f\u4e0d\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4fee\u6539\uff0c\u90a3\u4e48\u6c38\u8fdc\u90fd\u4e0d\u9700\u8981\u62f7\u8d1d\u6570\u636e\u5230\u81ea\u5df1\u7684\u5e94\u7528\u7a7a\u95f4\u91cc\u3002\u8fd9\u6837\u5c31\u51cf\u5c11\u4e86\u6570\u636e\u7684\u62f7\u8d1d\u3002\u5199\u65f6\u590d\u5236\u7684\u5185\u5bb9\u53ef\u4ee5\u518d\u5199\u4e00\u7bc7\u6587\u7ae0\u4e86\u3002<\/p>\n

        \u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u96f6\u62f7\u8d1d\u6280\u672f\uff0c\u6bd4\u5982\u4f20\u7edf\u7684Linux I\/O\u4e2d\u52a0\u4e0aO_DIRECT\u6807\u8bb0\u53ef\u4ee5\u76f4\u63a5I\/O\uff0c\u907f\u514d\u4e86\u81ea\u52a8\u7f13\u5b58\uff0c\u8fd8\u6709\u5c1a\u672a\u6210\u719f\u7684fbufs\u6280\u672f\uff0c\u672c\u6587\u5c1a\u672a\u8986\u76d6\u6240\u6709\u96f6\u62f7\u8d1d\u6280\u672f\uff0c\u53ea\u662f\u4ecb\u7ecd\u5e38\u89c1\u7684\u4e00\u4e9b\uff0c\u5982\u6709\u5174\u8da3\uff0c\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\uff0c\u4e00\u822c\u6210\u719f\u7684\u670d\u52a1\u7aef\u9879\u76ee\u4e5f\u4f1a\u81ea\u5df1\u6539\u9020\u5185\u6838\u4e2d\u6709\u5173I\/O\u7684\u90e8\u5206\uff0c\u63d0\u9ad8\u81ea\u5df1\u7684\u6570\u636e\u4f20\u8f93\u901f\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

        \u5728\u5199\u4e00\u4e2a\u670d\u52a1\u7aef\u7a0b\u5e8f\u65f6\uff08Web Server\u6216\u8005\u6587\u4ef6\u670d\u52a1\u5668\uff09\uff0c\u6587\u4ef6\u4e0b\u8f7d\u662f\u4e00\u4e2a\u57fa\u672c\u529f\u80fd\u3002\u8fd9\u65f6\u5019\u670d\u52a1\u7aef\u7684\u4efb\u52a1\u662f\uff1a\u5c06\u670d […]<\/p>\n","protected":false},"author":1329,"featured_media":195591,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-195584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/195584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=195584"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/195584\/revisions"}],"predecessor-version":[{"id":195592,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/195584\/revisions\/195592"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/195591"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=195584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=195584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=195584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}