{"id":103661,"date":"2024-09-20T23:23:12","date_gmt":"2024-09-20T15:23:12","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=103661"},"modified":"2024-09-20T23:23:12","modified_gmt":"2024-09-20T15:23:12","slug":"linux-server","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-server.html","title":{"rendered":"\u6d45\u6790Linux\u670d\u52a1\u5668\u7f51\u7edc\u5f00\u53d1\u6a21\u578b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u51e0\u65e5\u524d\u516c\u53f8\u540c\u4e8b\u738b\u5f00\u6e90\u540c\u5fd7\u5c06\u7b14\u8bb0\u672c\u7cfb\u7edf\u8f6c\u6210\u4e86liunx\u7cfb\u7edf\uff0c\u770b\u4e0a\u53bb\u4e0d\u9519\uff0c\u5e94\u8be5\u6709\u7684\u5e94\u7528\u57fa\u672c\u4e0a\u90fd\u6709\u4e86\u3002\u5982\u6587\u6863\u5904\u7406\u8f6f\u4ef6\u3001\u8868\u683c\u5904\u7406\u8f6f\u4ef6\u3001eclipse\u5f00\u53d1\u73af\u5883\u7b49\u3002\u522b\u8bf4\uff0c\u8fd8\u771f\u4e0d\u9519\u3002\u81ea\u5df1\u53c8\u4e00\u6b21\u52a8\u5fc3\u60f3\u88c5\u4e00\u4e2aliunx\u7cfb\u7edf\u4f7f\u7528\uff0c\u53ef\u662f\u8fd8\u6ca1\u6709\u6562\u8fd9\u6837\u505a\uff0c\u91c7\u7528linux\u7cfb\u7edf\u5b9e\u5728\u662f\u592a\u6d6a\u8d39\u7cbe\u529b\u4e86\uff0c\u5f88\u591a\u4e1c\u897f\u8981\u81ea\u5df1\u8bbe\u7f6e\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e3a\u4ec0\u4e48Nginx\u7684\u6027\u80fd\u8981\u6bd4Apache\u9ad8\u5f97\u591a?<\/strong><\/div>\n

\u8fd9\u4e3b\u8981\u662f\u56e0\u4e3aNginx\u4f7f\u7528\u4e86\u6700\u65b0\u7684epoll(Linux 2.6\u5185\u6838)\u548ckqueue(FreeBSD)\u7f51\u7edcI\/O\u6a21\u578b\uff0c\u800cApache\u5219\u4f7f\u7528\u7684\u662f\u4f20\u7edf\u7684select\u6a21\u578b\u3002\u66fe\u5728\u4e00\u7bc7\u535a\u5ba2\u4e0a\u770b\u5230\u6709\u8fd9\u4e48\u4e2a\u5b9e\u4f8b\uff1a<\/p>\n

\u5047\u8bbe\u4f60\u5728\u5927\u5b66\u4e2d\u8bfb\u4e66,\u8981\u7b49\u5f85\u4e00\u4e2a\u670b\u53cb\u6765\u8bbf,\u800c\u8fd9\u4e2a\u670b\u53cb\u53ea\u77e5\u9053\u4f60\u5728A\u53f7\u697c,\u4f46\u662f\u4e0d\u77e5\u9053\u4f60\u5177\u4f53\u4f4f\u5728\u54ea\u91cc,\u4e8e\u662f\u4f60\u4eec\u7ea6\u597d\u4e86\u5728A\u53f7\u697c\u95e8\u53e3\u89c1\u9762.\u5982\u679c\u4f60\u4f7f\u7528\u7684\u963b\u585eIO \u6a21\u578b\u6765\u5904\u7406\u8fd9\u4e2a\u95ee\u9898,\u90a3\u4e48\u4f60\u5c31\u53ea\u80fd\u4e00\u76f4\u5b88\u5019\u5728A\u53f7\u697c\u95e8\u53e3\u7b49\u5f85\u670b\u53cb\u7684\u5230\u6765,\u5728\u8fd9\u6bb5\u65f6\u95f4\u91cc\u4f60\u4e0d\u80fd\u505a\u522b\u7684\u4e8b\u60c5,\u4e0d\u96be\u77e5\u9053,\u8fd9\u79cd\u65b9\u5f0f\u7684\u6548\u7387\u662f\u4f4e\u4e0b\u7684.\u73b0\u5728\u65f6\u4ee3\u53d8\u5316\u4e86,\u5f00\u59cb\u4f7f\u7528\u591a\u8def\u590d\u7528IO\u6a21\u578b\u6765\u5904\u7406\u8fd9\u4e2a\u95ee\u9898.\u4f60\u544a\u8bc9\u4f60\u7684\u670b\u53cb\u6765\u4e86A\u53f7\u697c\u627e\u697c\u7ba1\u5927\u5988,\u8ba9\u5979\u544a\u8bc9\u4f60\u8be5\u600e\u4e48\u8d70.\u8fd9\u91cc\u7684\u697c\u7ba1\u5927\u5988\u626e\u6f14\u7684\u5c31\u662f\u591a\u8def\u590d\u7528IO\u7684\u89d2\u8272\u3002<\/p>\n

\u89e3\u91caselect\u548cepoll\u6a21\u578b\u7684\u5de5\u4f5c\u65b9\u5f0f\uff1a<\/strong><\/div>\n

select\u7248\u5927\u5988\u505a\u7684\u662f\u5982\u4e0b\u7684\u4e8b\u60c5:\u6bd4\u5982\u540c\u5b66\u7532\u7684\u670b\u53cb\u6765\u4e86,select\u7248\u5927\u5988\u6bd4\u8f83\u7b28,\u5979\u5e26\u7740\u670b\u53cb\u6328\u4e2a\u623f\u95f4\u8fdb\u884c\u67e5\u8be2\u8c01\u662f\u540c\u5b66\u7532,\u4f60\u7b49\u7684\u670b\u53cb\u6765\u4e86\u3002\u5982\u679c\u6bcf\u5230\u6765\u4e00\u4e2a\u670b\u53cb\u697c\u7ba1\u5927\u5988\u90fd\u8981\u5168\u697c\u7684\u67e5\u8be2\u540c\u5b66,\u90a3\u4e48\u5904\u7406\u7684\u6548\u7387\u5fc5\u7136\u5c31\u4f4e\u4e0b\u4e86,\u8fc7\u4e0d\u4e45\u697c\u5e95\u5c31\u6709\u4e0d\u5c11\u7684\u4eba\u4e86\u3002<\/p>\n

epoll\u7248\u5927\u5988\u5c31\u6bd4\u8f83\u5148\u8fdb\u4e86,\u5979\u8bb0\u4e0b\u4e86\u540c\u5b66\u7532\u7684\u4fe1\u606f,\u6bd4\u5982\u8bf4\u4ed6\u7684\u623f\u95f4\u53f7,\u90a3\u4e48\u7b49\u540c\u5b66\u7532\u7684\u670b\u53cb\u5230\u6765\u65f6,\u53ea\u9700\u8981\u544a\u8bc9\u8be5\u670b\u53cb\u540c\u5b66\u7532\u5728\u54ea\u4e2a\u623f\u95f4\u5373\u53ef,\u4e0d\u7528\u81ea\u5df1\u4eb2\u81ea\u5e26\u7740\u4eba\u6ee1\u5927\u697c\u7684\u627e\u4eba\u4e86\u3002epoll\u5927\u5988\u53ef\u4ee5\u4e0d\u7528\u5439\u7070\u4e4b\u529b\u5c31\u53ef\u4ee5\u5b9a\u4f4d\u5230\u540c\u5b66\u7532\u3002\u4e00\u770b\u5c31\u5f88\u660e\u767d epoll\u548cselect \u6a21\u578b\u7684\u533a\u522b\u4e86\u5427\u3002<\/p>\n

\u5728Linux\u5185\u6838\u4e2d\uff0cselect\u6240\u7528\u5230\u7684FD_SET\u662f\u6709\u9650\u7684\uff0c\u5373\u5185\u6838\u4e2d\u6709\u4e2a\u53c2\u6570__FD_SETSIZE\u5b9a\u4e49\u4e86\u6bcf\u4e2aFD_SET\u7684\u53e5\u67c4\u4e2a\u6570\uff0c\u5728\u5185\u6838\u6e90\u7801\u4e2d \/usr\/include\/linux\/posix_types.h <\/span>\u4e2d<\/p>\n

#undef __FD_SETSIZE\r\n\r\n#define __FD_SETSIZE 1024<\/pre>\n

\u5982\u679c\u60f3\u8981\u540c\u65f6\u68c0\u6d4b1025\u4e2a\u53e5\u67c4\u7684\u53ef\u8bfb\u72b6\u6001\u6216 \u53ef\u5199\u72b6\u6001 \uff0cselect\u662f\u4e0d\u80fd\u5b9e\u73b0\u7684\u3002\u5728\u5185\u6838\u4e2d\u5b9e\u73b0select\u662f\u4f7f\u7528\u8f6e\u8be2\u65b9\u6cd5\uff0c\u5373\u6bcf\u6b21\u68c0\u6d4b\u90fd\u4f1a\u904d\u5386\u6240\u6709FD_SET\u4e2d\u7684\u53e5\u67c4\uff0c\u663e\u7136\uff0cselect\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u4e0e FD\u68c0\u6d4b\u7684\u53e5\u67c4\u6570\u8d8a\u591a\u5c31\u4f1a\u8d8a\u8d39\u65f6\u3002<\/p>\n

epoll\u662f\u591a\u8def\u590d\u7528IO(I\/O Multiplexing) \u4e2d\u7684\u4e00\u79cd\u65b9\u5f0f,\u4ec5\u7528\u4e8elinux2.6\u4ee5\u4e0a\u5185\u6838\u3002\u800cepoll\u6a21\u578b\u5b83\u6240\u652f\u6301\u7684FD\u4e0a\u9650\u662f\u6700\u5927\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u7684\u6570\u76ee\uff0c\u8fd9\u4e2a\u6570\u5b57\u4e00\u822c\u8fdc\u5927\u4e8e2048,\u4e3e\u4e2a\u4f8b\u5b50,\u57281GB\u5185\u5b58\u7684\u673a\u5668\u4e0a\u5927\u7ea6\u662f10\u4e07\u5de6\u53f3\uff0c\u5177\u4f53\u8bf7\u67e5\u770b\uff1acat \/proc\/sys\/fs\/file-max ,\u8fd9\u4e2a\u6570\u76ee\u548c\u7cfb\u7edf\u5185\u5b58\u5173\u7cfb\u5f88\u5927\u3002<\/p>\n

\u4f20\u7edf\u7684select\/poll\u53e6\u4e00\u4e2a\u81f4\u547d\u5f31\u70b9\u5c31\u662f\u5f53\u4f60\u62e5\u6709\u4e00\u4e2a\u5f88\u5927\u7684socket\u96c6\u5408\uff0c\u4e0d\u8fc7\u7531\u4e8e\u7f51\u7edc\u5ef6\u65f6\uff0c\u4efb\u4e00\u65f6\u95f4\u53ea\u6709\u90e8\u5206\u7684socket\u662f\"\u6d3b\u8dc3\"\u7684\uff0c\u4f46\u662fselect\/poll\u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u7ebf\u6027\u626b\u63cf\u5168\u90e8\u7684\u96c6\u5408\uff0c\u5bfc\u81f4\u6548\u7387\u5448\u73b0\u7ebf\u6027\u4e0b\u964d\u3002\u4f46\u662fepoll\u4e0d\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\uff0c\u5b83\u53ea\u4f1a\u5bf9\"\u6d3b\u8dc3\"\u7684socket\u8fdb\u884c\u64cd\u4f5c---\u8fd9\u662f\u56e0\u4e3a\u5728\u5185\u6838\u5b9e\u73b0\u4e2depoll\u662f\u6839\u636e\u6bcf\u4e2afd\u4e0a\u9762\u7684callback\u51fd\u6570\u5b9e\u73b0\u7684\u3002\u90a3\u4e48\uff0c\u53ea\u6709\"\u6d3b\u8dc3\"\u7684socket\u624d\u4f1a\u4e3b\u52a8\u7684\u53bb\u8c03\u7528 callback\u51fd\u6570\uff0c\u5176\u4ed6idle\u72b6\u6001socket\u5219\u4e0d\u4f1a\uff0c\u5728\u8fd9\u70b9\u4e0a\uff0cepoll\u5b9e\u73b0\u4e86\u4e00\u4e2a\"\u4f2a\"AIO\uff0c\u56e0\u4e3a\u8fd9\u65f6\u5019\u63a8\u52a8\u529b\u5728os\u5185\u6838\u3002\u5728\u4e00\u4e9b benchmark\u4e2d\uff0c\u5982\u679c\u6240\u6709\u7684socket\u57fa\u672c\u4e0a\u90fd\u662f\u6d3b\u8dc3\u7684---\u6bd4\u5982\u4e00\u4e2a\u9ad8\u901fLAN\u73af\u5883\uff0cepoll\u5e76\u4e0d\u6bd4select\/poll\u6709\u4ec0\u4e48\u6548\u7387\uff0c\u76f8\u53cd\uff0c\u5982\u679c\u8fc7\u591a\u4f7f\u7528epoll_ctl,\u6548\u7387\u76f8\u6bd4\u8fd8\u6709\u7a0d\u5fae\u7684\u4e0b\u964d\u3002\u4f46\u662f\u4e00\u65e6\u4f7f\u7528idle connections\u6a21\u62dfWAN\u73af\u5883,epoll\u7684\u6548\u7387\u5c31\u8fdc\u5728select\/poll\u4e4b\u4e0a\u4e86\u3002<\/p>\n

epoll\u6709\u4e24\u79cd\u5de5\u4f5c\u6a21\u5f0f:Edge Triggered (ET)<\/span>\u3001Level Triggered<\/span> (LT)<\/p>\n

LT(level triggered)\u662f\u7f3a\u7701\u7684\u5de5\u4f5c\u65b9\u5f0f\uff0c\u5e76\u4e14\u540c\u65f6\u652f\u6301block\u548cno-block socket.\u5728\u8fd9\u79cd\u505a\u6cd5\u4e2d\uff0c\u5185\u6838\u544a\u8bc9\u4f60\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u5c31\u7eea\u4e86\uff0c\u7136\u540e\u53ef\u4ee5\u5bf9\u8fd9\u4e2a\u5c31\u7eea\u7684fd\u8fdb\u884cIO\u64cd\u4f5c\u3002\u5982\u679c\u4f60\u4e0d\u4f5c\u4efb\u4f55\u64cd\u4f5c\uff0c\u5185\u6838\u8fd8\u662f\u4f1a\u7ee7\u7eed\u901a\u77e5\u4f60\u7684\uff0c\u6240\u4ee5\uff0c\u8fd9\u79cd\u6a21\u5f0f\u7f16\u7a0b\u51fa\u9519\u8bef\u53ef\u80fd\u6027\u8981\u5c0f\u4e00\u70b9\u3002\u4f20\u7edf\u7684select\/poll\u90fd\u662f\u8fd9\u79cd\u6a21\u578b\u7684\u4ee3\u8868\u3002<\/p>\n

ET (edge-triggered)\u662f\u9ad8\u901f\u5de5\u4f5c\u65b9\u5f0f\uff0c\u53ea\u652f\u6301no-block socket\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u5f53\u63cf\u8ff0\u7b26\u4ece\u672a\u5c31\u7eea\u53d8\u4e3a\u5c31\u7eea\u65f6\uff0c\u5185\u6838\u901a\u8fc7epoll\u544a\u8bc9\u4f60\u3002\u7136\u540e\u5b83\u4f1a\u5047\u8bbe\u4f60\u77e5\u9053\u6587\u4ef6\u63cf\u8ff0\u7b26\u5df2\u7ecf\u5c31\u7eea\uff0c\u5e76\u4e14\u4e0d\u4f1a\u518d\u4e3a\u90a3\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u53d1\u9001\u66f4\u591a\u7684\u5c31\u7eea\u901a\u77e5\uff0c\u76f4\u5230\u4f60\u505a\u4e86\u67d0\u4e9b\u64cd\u4f5c\u5bfc\u81f4\u90a3\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0d\u518d\u4e3a\u5c31\u7eea\u72b6\u6001\u4e86(\u6bd4\u5982\uff0c\u4f60\u5728\u53d1\u9001\uff0c\u63a5\u6536\u6216\u8005\u63a5\u6536\u8bf7\u6c42\uff0c\u6216\u8005\u53d1\u9001\u63a5\u6536\u7684\u6570\u636e\u5c11\u4e8e\u4e00\u5b9a\u91cf\u65f6\u5bfc\u81f4\u4e86\u4e00\u4e2aEWOULDBLOCK \u9519\u8bef)\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.jsgho.net\/help\/fwq\/27115.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1a https:\/\/lrxjmw.cn\/linux-server.html<\/a>\u7f16\u8f91\uff1a\u90dd\u5927\u53d1\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u51e0\u65e5\u524d\u516c\u53f8\u540c\u4e8b\u738b\u5f00\u6e90\u540c\u5fd7\u5c06\u7b14\u8bb0\u672c\u7cfb\u7edf\u8f6c\u6210\u4e86liunx\u7cfb\u7edf\uff0c\u770b\u4e0a\u53bb\u4e0d\u9519\uff0c\u5e94\u8be5\u6709\u7684\u5e94\u7528\u57fa\u672c\u4e0a\u90fd\u6709\u4e86\u3002\u5982\u6587\u6863\u5904 […]<\/p>\n","protected":false},"author":63,"featured_media":104553,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-103661","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/103661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=103661"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/103661\/revisions"}],"predecessor-version":[{"id":293092,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/103661\/revisions\/293092"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/104553"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=103661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=103661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=103661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}