{"id":170,"date":"2022-10-23T20:54:19","date_gmt":"2022-10-23T12:54:19","guid":{"rendered":"https:\/\/lmceric.top\/?p=170"},"modified":"2022-10-23T20:54:21","modified_gmt":"2022-10-23T12:54:21","slug":"c-socket-epoll%e5%88%9d%e8%af%86","status":"publish","type":"post","link":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/","title":{"rendered":"C++ socket epoll\u521d\u8bc6"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"c-socket-epoll\u521d\u8bc6\">C++ socket epoll\u521d\u8bc6<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528epoll\">1.\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528epoll<\/h2>\n\n\n\n<p>\u5c31\u50cf\u4e0b\u9762\u6240\u7ed9\u51fa\u7684\u4ee3\u7801\u4e00\u6837\uff0c\u5728\u7b80\u5355\u7684\u60c5\u51b5\u4e0bS\/C\u670d\u52a1\u5668\u53ea\u80fd\u540c\u65f6\u5904\u7406\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">#include &lt;stdio.h&gt;\n#include &lt;sys\/socket.h&gt;\n#include &lt;arpa\/inet.h&gt;\n#include &lt;string.h&gt;\n\nint main() {\n    int sockfd = socket(AF_INET, SOCK_STREAM, 0);\n\n    struct sockaddr_in serv_addr;}\n    bzero(&amp;serv_addr, sizeof(serv_addr));\n    serv_addr.sin_family = AF_INET;\n    serv_addr.sin_addr.s_addr = inet_addr(\"127.0.0.1\");\n    serv_addr.sin_port = htons(8888);\n\n    bind(sockfd, (sockaddr*)&amp;serv_addr, sizeof(serv_addr));\n\n    listen(sockfd, SOMAXCONN);\n\n    struct sockaddr_in clnt_addr;\n    socklen_t clnt_addr_len = sizeof(clnt_addr);\n    bzero(&amp;clnt_addr, sizeof(clnt_addr));\n\n    int clnt_sockfd = accept(sockfd, (sockaddr*)&amp;clnt_addr, &amp;clnt_addr_len);\n\n    printf(\"new client fd %d! IP: %s Port: %d\\n\", clnt_sockfd, inet_ntoa(clnt_addr.sin_addr), ntohs(clnt_addr.sin\n_port));\n    return 0;\n\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u8fde\u63a5\u7684\u751f\u547d\u5468\u671f\u91cc\uff0c\u7edd\u5927\u90e8\u5206\u65f6\u95f4\u90fd\u662f\u7a7a\u95f2\u7684\uff0c\u6d3b\u8dc3\u65f6\u95f4\uff08\u53d1\u9001\u6570\u636e\u548c\u63a5\u6536\u6570\u636e\u7684\u65f6\u95f4\uff09\u5360\u6bd4\u6781\u5c11\uff0c\u8fd9\u6837\u72ec\u5360\u4e00\u4e2a\u670d\u52a1\u5668\u662f\u4e25\u91cd\u7684\u8d44\u6e90\u6d6a\u8d39\u3002\u4e8b\u5b9e\u4e0a\u6240\u6709\u7684\u670d\u52a1\u5668\u90fd\u662f\u9ad8\u5e76\u53d1\u7684\uff0c\u53ef\u4ee5\u540c\u65f6\u4e3a\u6210\u5343\u4e0a\u4e07\u4e2a\u5ba2\u6237\u7aef\u63d0\u4f9b\u670d\u52a1\uff0c\u8fd9\u4e00\u6280\u672f\u53c8\u88ab\u79f0\u4e3aIO\u590d\u7528\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\u6765\u8bf4\uff0c\u5b9e\u73b0\u5904\u7406tcp\u8bf7\u6c42\uff0c\u4e3a\u4e00\u4e2a\u8fde\u63a5\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5728\u9ad8\u5e76\u53d1\u7684\u573a\u666f\uff0c\u8fd9\u79cd\u591a\u7ebf\u7a0b\u6a21\u578b\u4e0eEpoll\u76f8\u6bd4\u5c31\u663e\u5f97\u76f8\u5f62\u89c1\u7ecc\u4e86\u3002<code>epoll<\/code>\u662flinux2.6\u5185\u6838\u7684\u4e00\u4e2a\u65b0\u7684\u7cfb\u7edf\u8c03\u7528\uff0c<code>epoll<\/code>\u5728\u8bbe\u8ba1\u4e4b\u521d\uff0c\u5c31\u662f\u4e3a\u4e86\u66ff\u4ee3<code>select, poll<\/code>\u7ebf\u6027\u590d\u6742\u5ea6\u7684\u6a21\u578b\uff0cepoll\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1), \u4e5f\u5c31\u610f\u5473\u7740\uff0c<code>epoll<\/code>\u5728\u9ad8\u5e76\u53d1\u573a\u666f\uff0c\u968f\u7740\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u589e\u957f\uff0c\u6709\u826f\u597d\u7684\u53ef\u6269\u5c55\u6027\u3002<\/p>\n\n\n\n<p>\u5728linux\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">\/proc\/sys\/fs\/epoll\/max_user_watches<\/code><\/pre>\n\n\n\n<p>\u8868\u793a\u7528\u6237\u80fd\u6ce8\u518c\u5230<code>epoll<\/code>\u5b9e\u4f8b\u4e2d\u7684\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6570\u91cf\u9650\u5236\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2epoll\u539f\u7406\">2.epoll\u539f\u7406<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"21-io\u591a\u8def\u590d\u7528\">2.1 I\/O\u591a\u8def\u590d\u7528<\/h3>\n\n\n\n<p>\u8f93\u5165\u8f93\u51fa(input\/output)\u7684\u5bf9\u8c61\u53ef\u4ee5\u662f\u6587\u4ef6(file)\uff0c \u7f51\u7edc(socket)\uff0c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u7ba1\u9053(pipe)\u3002\u5728linux\u7cfb\u7edf\u4e2d\uff0c\u90fd\u7528\u6587\u4ef6\u63cf\u8ff0\u7b26(fd)\u6765\u8868\u793a\u3002<\/p>\n\n\n\n<p>I\/O \u591a\u8def\u590d\u7528\u7684\u672c\u8d28\uff0c\u662f\u901a\u8fc7\u4e00\u79cd\u673a\u5236\uff08\u7cfb\u7edf\u5185\u6838\u7f13\u51b2I\/O\u6570\u636e\uff09\uff0c\u8ba9\u5355\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u76d1\u89c6\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4e00\u65e6\u67d0\u4e2a\u63cf\u8ff0\u7b26\u5c31\u7eea\uff08\u4e00\u822c\u662f\u8bfb\u5c31\u7eea\u6216\u5199\u5c31\u7eea\uff09\uff0c\u80fd\u591f\u901a\u77e5\u7a0b\u5e8f\u8fdb\u884c\u76f8\u5e94\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>select\u3001poll \u548c epoll \u90fd\u662f Linux API \u63d0\u4f9b\u7684 IO \u590d\u7528\u65b9\u5f0f\u3002 Linux\u4e2d\u63d0\u4f9b\u7684epoll\u76f8\u5173\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int epoll_create(int size);\nint epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);\nint epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><code>epoll_create<\/code>: \u521b\u5efa\u4e00\u4e2aepoll\u5b9e\u4f8b\uff0c\u6587\u4ef6\u63cf\u8ff0\u7b26<\/li><li><code>epoll_ctl<\/code>: \u5c06\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6dfb\u52a0\u5230epoll\u5b9e\u4f8b\u4e2d\uff0c\u5b9e\u4f8b\u4ee3\u7801\u4e3a\u5c06\u6807\u51c6\u8f93\u5165\u6587\u4ef6\u63cf\u8ff0\u7b26\u6dfb\u52a0\u5230epoll\u4e2d<\/li><li><code>epoll_wait<\/code>: \u7b49\u5f85epoll\u4e8b\u4ef6\u4eceepoll\u5b9e\u4f8b\u4e2d\u53d1\u751f\uff0c \u5e76\u8fd4\u56de\u4e8b\u4ef6\u4ee5\u53ca\u5bf9\u5e94\u6587\u4ef6\u63cf\u8ff0\u7b26l<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"22-unix\u7684io\u6a21\u578b\">2.2 Unix\u7684IO\u6a21\u578b<\/h3>\n\n\n\n<p>\u6211\u4eec\u5728\u8fdb\u884c\u7f16\u7a0b\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u7ecf\u5e38\u4f1a\u6d89\u53ca\u5230\u540c\u6b65\uff0c\u5f02\u6b65\uff0c\u963b\u585e\uff0c\u975e\u963b\u585e\uff0cIO\u591a\u8def\u590d\u7528\u7b49\u6982\u5ff5\uff0c\u8fd9\u91cc\u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\u3002<\/p>\n\n\n\n<p>Unix\u7f51\u7edc\u7f16\u7a0b\u4e2d\u7684\u4e94\u79cdIO\u6a21\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">Blocking IO - \u963b\u585eIO\nNoneBlocking IO - \u975e\u963b\u585eIO\nIO multiplexing - IO\u591a\u8def\u590d\u7528\nsignal driven IO - \u4fe1\u53f7\u9a71\u52a8IO\nasynchronous IO - \u5f02\u6b65IO<\/code><\/pre>\n\n\n\n<p>\u5bf9\u4e8e\u4e00\u4e2anetwork IO\uff0c\u5b83\u4f1a\u6d89\u53ca\u5230\u4e24\u4e2a\u7cfb\u7edf\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Application \u8c03\u7528\u8fd9\u4e2aIO\u7684\u8fdb\u7a0b<\/li><li>kernel \u7cfb\u7edf\u5185\u6838<\/li><\/ol>\n\n\n\n<p>\u90a3\u4ed6\u4eec\u7ecf\u5386\u7684\u4e24\u4e2a\u4ea4\u4e92\u8fc7\u7a0b\u662f\uff1a<\/p>\n\n\n\n<p>\u9636\u6bb51\uff1a wait for data \u7b49\u5f85\u6570\u636e\u51c6\u5907\uff1b \u9636\u6bb52\uff1a copy data from kernel to user \u5c06\u6570\u636e\u4ece\u5185\u6838\u62f7\u8d1d\u5230\u7528\u6237\u8fdb\u7a0b\u4e2d\u3002<\/p>\n\n\n\n<p>\u4e4b\u6240\u4ee5\u4f1a\u6709\u540c\u6b65\u3001\u5f02\u6b65\u3001\u963b\u585e\u548c\u975e\u963b\u585e\u8fd9\u51e0\u79cd\u8bf4\u6cd5\u5c31\u662f\u6839\u636e\u7a0b\u5e8f\u5728\u8fd9\u4e24\u4e2a\u9636\u6bb5\u7684\u5904\u7406\u65b9\u5f0f\u4e0d\u540c\u800c\u4ea7\u751f\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"23-\u4e8b\u4ef6\">2.3 \u4e8b\u4ef6<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"231-\u53ef\u8bfb\u4e0e\u53ef\u5199\">2.3.1 \u53ef\u8bfb\u4e0e\u53ef\u5199<\/h4>\n\n\n\n<p>\u53ef\u8bfb\u4e8b\u4ef6\uff0c\u5f53\u6587\u4ef6\u63cf\u8ff0\u7b26\u5173\u8054\u7684\u5185\u6838\u8bfb\u7f13\u51b2\u533a\u53ef\u8bfb\uff0c\u5219\u89e6\u53d1\u53ef\u8bfb\u4e8b\u4ef6\u3002 (\u53ef\u8bfb\uff1a\u5185\u6838\u7f13\u51b2\u533a\u975e\u7a7a\uff0c\u6709\u6570\u636e\u53ef\u4ee5\u8bfb\u53d6)<\/p>\n\n\n\n<p>\u53ef\u5199\u4e8b\u4ef6\uff0c\u5f53\u6587\u4ef6\u63cf\u8ff0\u7b26\u5173\u8054\u7684\u5185\u6838\u5199\u7f13\u51b2\u533a\u53ef\u5199\uff0c\u5219\u89e6\u53d1\u53ef\u5199\u4e8b\u4ef6\u3002 (\u53ef\u5199\uff1a\u5185\u6838\u7f13\u51b2\u533a\u4e0d\u6ee1\uff0c\u6709\u7a7a\u95f2\u7a7a\u95f4\u53ef\u4ee5\u5199\u5165\uff09<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"232-\u901a\u77e5\u673a\u5236\">2.3.2 \u901a\u77e5\u673a\u5236<\/h4>\n\n\n\n<p>\u901a\u77e5\u673a\u5236\uff0c\u5c31\u662f\u5f53\u4e8b\u4ef6\u53d1\u751f\u7684\u65f6\u5019\uff0c\u5219\u4e3b\u52a8\u901a\u77e5\u3002\u901a\u77e5\u673a\u5236\u7684\u53cd\u9762\uff0c\u5c31\u662f\u8f6e\u8be2\u673a\u5236\u3002<\/p>\n\n\n\n<p>\u7ed3\u5408\u4ee5\u4e0a\u4e09\u6761\uff0cepoll\u7684\u901a\u4fd7\u89e3\u91ca\u662f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u5f53\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5185\u6838\u7f13\u51b2\u533a\u975e\u7a7a\u7684\u65f6\u5019\uff0c\u53d1\u51fa\u53ef\u8bfb\u4fe1\u53f7\u8fdb\u884c\u901a\u77e5\uff0c\u5f53\u5199\u7f13\u51b2\u533a\u4e0d\u6ee1\u7684\u65f6\u5019\uff0c\u53d1\u51fa\u53ef\u5199\u4fe1\u53f7\u901a\u77e5\u7684\u673a\u5236\u3002<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"233-epoll\u4e8b\u4ef6\u89e6\u53d1\">2.3.3 epoll\u4e8b\u4ef6\u89e6\u53d1<\/h4>\n\n\n\n<p><code>epoll<\/code>\u4e8b\u4ef6\u6709\u4e24\u79cd\u6a21\u578b\uff0c\u8fb9\u6cbf\u89e6\u53d1\uff1aedge-triggered (ET)\uff0c \u6c34\u5e73\u89e6\u53d1\uff1alevel-triggered (LT)<\/p>\n\n\n\n<p><strong>\u6c34\u5e73\u89e6\u53d1<\/strong>\uff08LT\uff09<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>socket\u63a5\u6536\u7f13\u51b2\u533a\u4e0d\u4e3a\u7a7a \u6709\u6570\u636e\u53ef\u8bfb \u8bfb\u4e8b\u4ef6\u4e00\u76f4\u89e6\u53d1<\/li><li>socket\u53d1\u9001\u7f13\u51b2\u533a\u4e0d\u6ee1 \u53ef\u4ee5\u7ee7\u7eed\u5199\u5165\u6570\u636e \u5199\u4e8b\u4ef6\u4e00\u76f4\u89e6\u53d1<\/li><\/ul>\n\n\n\n<p><strong>\u8fb9\u7f18\u89e6\u53d1<\/strong>\uff08ET\uff09<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>socket\u7684\u63a5\u6536\u7f13\u51b2\u533a\u72b6\u6001\u53d8\u5316\u65f6\u89e6\u53d1\u8bfb\u4e8b\u4ef6\uff0c\u5373\u7a7a\u7684\u63a5\u6536\u7f13\u51b2\u533a\u521a\u63a5\u6536\u5230\u6570\u636e\u65f6\u89e6\u53d1\u8bfb\u4e8b\u4ef6<\/li><li>socket\u7684\u53d1\u9001\u7f13\u51b2\u533a\u72b6\u6001\u53d8\u5316\u65f6\u89e6\u53d1\u5199\u4e8b\u4ef6\uff0c\u5373\u6ee1\u7684\u7f13\u51b2\u533a\u521a\u7a7a\u51fa\u7a7a\u95f4\u65f6\u89e6\u53d1\u8bfb\u4e8b\u4ef6<\/li><\/ul>\n\n\n\n<p>\u8fb9\u6cbf\u89e6\u53d1\u4ec5\u89e6\u53d1\u4e00\u6b21\uff0c\u6c34\u5e73\u89e6\u53d1\u4f1a\u4e00\u76f4\u89e6\u53d1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3epoll\u5904\u7406\u9ad8\u5e76\u53d1\">3.epoll\u5904\u7406\u9ad8\u5e76\u53d1<\/h2>\n\n\n\n<p>IO\u590d\u7528\u7684\u57fa\u672c\u601d\u60f3\u662f\u4e8b\u4ef6\u9a71\u52a8\uff0c\u670d\u52a1\u5668\u540c\u65f6\u4fdd\u6301\u591a\u4e2a\u5ba2\u6237\u7aefIO\u8fde\u63a5\uff0c\u5f53\u8fd9\u4e2aIO\u4e0a\u6709\u53ef\u8bfb\u6216\u53ef\u5199\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u8868\u793a\u8fd9\u4e2aIO\u5bf9\u5e94\u7684\u5ba2\u6237\u7aef\u5728\u8bf7\u6c42\u670d\u52a1\u5668\u7684\u67d0\u9879\u670d\u52a1\uff0c\u6b64\u65f6\u670d\u52a1\u5668\u54cd\u5e94\u8be5\u670d\u52a1\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0cIO\u590d\u7528\u4f7f\u7528select, poll\u548cepoll\u6765\u5b9e\u73b0\u3002epoll\u6539\u8fdb\u4e86\u524d\u4e24\u8005\uff0c\u66f4\u52a0\u9ad8\u6548\u3001\u6027\u80fd\u66f4\u597d\uff0c\u662f\u76ee\u524d\u51e0\u4e4e\u6240\u6709\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7684\u57fa\u77f3\u3002<\/p>\n\n\n\n<p>epoll\u4e3b\u8981\u7531\u4e09\u4e2a\u7cfb\u7edf\u8c03\u7528\u7ec4\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">\/\/int epfd = epoll_create(1024);  \/\/\u53c2\u6570\u8868\u793a\u76d1\u542c\u4e8b\u4ef6\u7684\u5927\u5c0f\uff0c\u5982\u8d85\u8fc7\u5185\u6838\u4f1a\u81ea\u52a8\u8c03\u6574\uff0c\u5df2\u7ecf\u88ab\u820d\u5f03\uff0c\u65e0\u5b9e\u9645\u610f\u4e49\uff0c\u4f20\u5165\u4e00\u4e2a\u5927\u4e8e0\u7684\u6570\u5373\u53ef\nint epfd = epoll_create1(0);       \/\/\u53c2\u6570\u662f\u4e00\u4e2aflag\uff0c\u4e00\u822c\u8bbe\u4e3a0\uff0c\u8be6\u7ec6\u53c2\u8003man epoll<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2aepoll\u6587\u4ef6\u63cf\u8ff0\u7b26\u5e76\u8fd4\u56de\uff0c\u5931\u8d25\u5219\u8fd4\u56de-1\u3002<\/p>\n\n\n\n<p>epoll\u76d1\u542c\u4e8b\u4ef6\u7684\u63cf\u8ff0\u7b26\u4f1a\u653e\u5728\u4e00\u9897\u7ea2\u9ed1\u6811\u4e0a\uff0c\u6211\u4eec\u5c06\u8981\u76d1\u542c\u7684IO\u53e3\u653e\u5165epoll\u7ea2\u9ed1\u6811\u4e2d\uff0c\u5c31\u53ef\u4ee5\u76d1\u542c\u8be5IO\u4e0a\u7684\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"31-epoll\u6570\u636e\u7ed3\u6784\">3.1 epoll\u6570\u636e\u7ed3\u6784<\/h3>\n\n\n\n<p>epoll \u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u662f\uff1a1\u4e2a\u7ea2\u9ed1\u6811\u548c1\u4e2a\u94fe\u8868\u3002\u8fd8\u67093\u4e2a\u6838\u5fc3API\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/ask.qcloudimg.com\/http-save\/yehe-2233022\/th3ck72dli.png?imageView2\/2\/w\/1620\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"311-\u5c31\u7eea\u5217\u8868\">3.1.1 \u5c31\u7eea\u5217\u8868<\/h4>\n\n\n\n<p>\u5c31\u7eea\u5217\u8868\u5f15\u7528\u7740\u5c31\u7eea\u7684socket\uff0c\u6240\u4ee5\u5b83\u5e94\u80fd\u591f\u5feb\u901f\u7684\u63d2\u5165\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u7a0b\u5e8f\u53ef\u80fd\u968f\u65f6\u8c03\u7528epoll_ctl\u6dfb\u52a0\u76d1\u89c6socket\uff0c\u4e5f\u53ef\u80fd\u968f\u65f6\u5220\u9664\u3002\u5f53\u5220\u9664\u65f6\uff0c\u82e5\u8be5socket\u5df2\u7ecf\u5b58\u653e\u5728\u5c31\u7eea\u5217\u8868\u4e2d\uff0c\u5b83\u4e5f\u5e94\u8be5\u88ab\u79fb\u9664\u3002\uff08\u4e8b\u5b9e\u4e0a\uff0c\u6bcf\u4e2aepoll_item\u65e2\u662f\u7ea2\u9ed1\u6811\u8282\u70b9\uff0c\u4e5f\u662f\u94fe\u8868\u8282\u70b9\uff0c\u5220\u9664\u7ea2\u9ed1\u6811\u8282\u70b9\uff0c\u81ea\u7136\u5220\u9664\u4e86\u94fe\u8868\u8282\u70b9\uff09<\/p>\n\n\n\n<p>\u6240\u4ee5\u5c31\u7eea\u5217\u8868\u5e94\u662f\u4e00\u79cd\u80fd\u591f\u5feb\u901f\u63d2\u5165\u548c\u5220\u9664\u7684\u6570\u636e\u7ed3\u6784\u3002\u53cc\u5411\u94fe\u8868\u5c31\u662f\u8fd9\u6837\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0cepoll\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u6765\u5b9e\u73b0\u5c31\u7eea\u961f\u5217\uff08\u5bf9\u5e94\u4e0a\u56fe\u7684rdllist\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"312-\u7d22\u5f15\u7ed3\u6784\">3.1.2 \u7d22\u5f15\u7ed3\u6784<\/h4>\n\n\n\n<p>\u65e2\u7136epoll\u5c06\u201c\u7ef4\u62a4\u76d1\u89c6\u961f\u5217\u201d\u548c\u201c\u8fdb\u7a0b\u963b\u585e\u201d\u5206\u79bb\uff0c\u4e5f\u610f\u5473\u7740\u9700\u8981\u6709\u4e2a\u6570\u636e\u7ed3\u6784\u6765\u4fdd\u5b58\u76d1\u89c6\u7684socket\u3002\u81f3\u5c11\u8981\u65b9\u4fbf\u7684\u6dfb\u52a0\u548c\u79fb\u9664\uff0c\u8fd8\u8981\u4fbf\u4e8e\u641c\u7d22\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u6dfb\u52a0\u3002\u7ea2\u9ed1\u6811\u662f\u4e00\u79cd\u81ea\u5e73\u8861\u4e8c\u53c9\u67e5\u627e\u6811\uff0c\u641c\u7d22\u3001\u63d2\u5165\u548c\u5220\u9664\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662fO(log(N))\uff0c\u6548\u7387\u8f83\u597d\u3002epoll \u4f7f\u7528\u4e86\u7ea2\u9ed1\u6811\u4f5c\u4e3a\u7d22\u5f15\u7ed3\u6784\u3002<\/p>\n\n\n\n<p>Epoll\u5728linux\u5185\u6838\u4e2d\u6e90\u7801\u4e3b\u8981\u4e3a eventpoll.c \u548c eventpoll.h \u4e3b\u8981\u4f4d\u4e8efs\/eventpoll.c \u548c include\/linux\/eventpool.h, \u5177\u4f53\u53ef\u4ee5\u53c2\u8003linux3.16\u3002<\/p>\n\n\n\n<p>\u4e0b\u8ff0\u4e3a\u90e8\u5206\u5173\u952e\u6570\u636e\u7ed3\u6784\u6458\u8981, \u4e3b\u8981\u4ecb\u7ecdepitem \u7ea2\u9ed1\u6811\u8282\u70b9 \u548ceventpoll \u5173\u952e\u5165\u53e3\u6570\u636e\u7ed3\u6784\uff0c\u7ef4\u62a4\u7740\u94fe\u8868\u5934\u8282\u70b9ready list header\u548c\u7ea2\u9ed1\u6811\u6839\u8282\u70b9RB-Tree root\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">\/*\n * Each file descriptor added to the eventpoll interface will\n * have an entry of this type linked to the \"rbr\" RB tree.\n * Avoid increasing the size of this struct, there can be many thousands\n * of these on a server and we do not want this to take another cache line.\n *\/\nstruct epitem {\n    union {\n        \/* RB tree node links this structure to the eventpoll RB tree *\/\n        struct rb_node rbn;\n        \/* Used to free the struct epitem *\/\n        struct rcu_head rcu;\n    };\n\n    \/* List header used to link this structure to the eventpoll ready list *\/\n    struct list_head rdllink;\n\n    \/*\n     * Works together \"struct eventpoll\"-&gt;ovflist in keeping the\n     * single linked chain of items.\n     *\/\n    struct epitem *next;\n\n    \/* The file descriptor information this item refers to *\/\n    struct epoll_filefd ffd;\n\n    \/* Number of active wait queue attached to poll operations *\/\n    int nwait;\n\n    \/* List containing poll wait queues *\/\n    struct list_head pwqlist;\n\n    \/* The \"container\" of this item *\/\n    struct eventpoll *ep;\n\n    \/* List header used to link this item to the \"struct file\" items list *\/\n    struct list_head fllink;\n\n    \/* wakeup_source used when EPOLLWAKEUP is set *\/\n    struct wakeup_source __rcu *ws;\n\n    \/* The structure that describe the interested events and the source fd *\/\n    struct epoll_event event;\n};\n\n\/*\n * This structure is stored inside the \"private_data\" member of the file\n * structure and represents the main data structure for the eventpoll\n * interface.\n *\/\nstruct eventpoll {\n    \/* Protect the access to this structure *\/\n    spinlock_t lock;\n\n    \/*\n     * This mutex is used to ensure that files are not removed\n     * while epoll is using them. This is held during the event\n     * collection loop, the file cleanup path, the epoll file exit\n     * code and the ctl operations.\n     *\/\n    struct mutex mtx;\n\n    \/* Wait queue used by sys_epoll_wait() *\/\n    wait_queue_head_t wq;\n\n    \/* Wait queue used by file-&gt;poll() *\/\n    wait_queue_head_t poll_wait;\n\n    \/* List of ready file descriptors *\/\n    struct list_head rdllist;\n\n    \/* RB tree root used to store monitored fd structs *\/\n    struct rb_root rbr;\n\n    \/*\n     * This is a single linked list that chains all the \"struct epitem\" that\n     * happened while transferring ready events to userspace w\/out\n     * holding -&gt;lock.\n     *\/\n    struct epitem *ovflist;\n\n    \/* wakeup_source used when ep_scan_ready_list is running *\/\n    struct wakeup_source *ws;\n\n    \/* The user that created the eventpoll descriptor *\/\n    struct user_struct *user;\n\n    struct file *file;\n\n    \/* used to optimize loop detection check *\/\n    int visited;\n    struct list_head visited_list_link;\n};<\/code><\/pre>\n\n\n\n<p>epoll\u4f7f\u7528RB-Tree\u7ea2\u9ed1\u6811\u53bb\u76d1\u542c\u5e76\u7ef4\u62a4\u6240\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0cRB-Tree\u7684\u6839\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u8c03\u7528epoll_create\u65f6\uff0c\u5185\u6838\u9664\u4e86\u5e2e\u6211\u4eec\u5728epoll\u6587\u4ef6\u7cfb\u7edf\u91cc\u5efa\u4e86\u4e2afile\u7ed3\u70b9\uff0c\u5728\u5185\u6838cache\u91cc\u5efa\u4e86\u4e2a \u7ea2\u9ed1\u6811 \u7528\u4e8e\u5b58\u50a8\u4ee5\u540eepoll_ctl\u4f20\u6765\u7684socket\u5916\uff0c\u8fd8\u4f1a\u518d\u5efa\u7acb\u4e00\u4e2alist\u94fe\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u51c6\u5907\u5c31\u7eea\u7684\u4e8b\u4ef6.<\/p>\n\n\n\n<p>\u5f53epoll_wait\u8c03\u7528\u65f6\uff0c\u4ec5\u4ec5\u89c2\u5bdf\u8fd9\u4e2alist\u94fe\u8868\u91cc\u6709\u6ca1\u6709\u6570\u636e\u5373\u53ef\u3002\u6709\u6570\u636e\u5c31\u8fd4\u56de\uff0c\u6ca1\u6709\u6570\u636e\u5c31sleep\uff0c\u7b49\u5230timeout\u65f6\u95f4\u5230\u540e\u5373\u4f7f\u94fe\u8868\u6ca1\u6570\u636e\u4e5f\u8fd4\u56de\u3002\u6240\u4ee5\uff0cepoll_wait\u975e\u5e38\u9ad8\u6548\u3002\u800c\u4e14\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u5373\u4f7f\u6211\u4eec\u8981\u76d1\u63a7\u767e\u4e07\u8ba1\u7684\u53e5\u67c4\uff0c\u5927\u591a\u4e00\u6b21\u4e5f\u53ea\u8fd4\u56de\u5f88\u5c11\u91cf\u7684\u51c6\u5907\u5c31\u7eea\u53e5\u67c4\u800c\u5df2\uff0c\u6240\u4ee5\uff0cepoll_wait\u4ec5\u9700\u8981\u4ece\u5185\u6838\u6001copy\u5c11\u91cf\u7684\u53e5\u67c4\u5230\u7528\u6237\u6001\u800c\u5df2\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"32-\u76d1\u542cio\u4e8b\u4ef6\">3.2 \u76d1\u542cIO\u4e8b\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &amp;ev);    \/\/\u6dfb\u52a0\u4e8b\u4ef6\u5230epoll\nepoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &amp;ev);    \/\/\u4fee\u6539epoll\u7ea2\u9ed1\u6811\u4e0a\u7684\u4e8b\u4ef6\nepoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);   \/\/\u5220\u9664\u4e8b\u4ef6<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2dsockfd\u8868\u793a\u6211\u4eec\u8981\u6dfb\u52a0\u7684IO\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0cev\u662f\u4e00\u4e2aepoll_event\u7ed3\u6784\u4f53\uff0c\u5176\u4e2d\u7684events\u8868\u793a\u4e8b\u4ef6\uff0c\u5982EPOLLIN\u7b49\uff0cdata\u662f\u4e00\u4e2a\u7528\u6237\u6570\u636eunion:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">typedef union epoll_data {\n  void *ptr;\n  int fd;\n  uint32_t u32;\n  uint64_t u64;\n} epoll_data_t;\nstruct epoll_event {\n  uint32_t events;    \/* Epoll events *\/\n  epoll_data_t data;    \/* User data variable *\/\n} __EPOLL_PACKED;<\/code><\/pre>\n\n\n\n<p>epoll\u9ed8\u8ba4\u91c7\u7528LT\u89e6\u53d1\u6a21\u5f0f\uff0c\u5373\u6c34\u5e73\u89e6\u53d1\uff0c\u53ea\u8981fd\u4e0a\u6709\u4e8b\u4ef6\uff0c\u5c31\u4f1a\u4e00\u76f4\u901a\u77e5\u5185\u6838\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u6240\u6709\u4e8b\u4ef6\u90fd\u5f97\u5230\u5904\u7406\u3001\u4e0d\u5bb9\u6613\u4e22\u5931\uff0c\u4f46\u53ef\u80fd\u53d1\u751f\u7684\u5927\u91cf\u91cd\u590d\u901a\u77e5\u4e5f\u4f1a\u5f71\u54cdepoll\u7684\u6027\u80fd\u3002\u5982\u4f7f\u7528ET\u6a21\u5f0f\uff0c\u5373\u8fb9\u7f18\u89e6\u6cd5\uff0cfd\u4ece\u65e0\u4e8b\u4ef6\u5230\u6709\u4e8b\u4ef6\u7684\u53d8\u5316\u4f1a\u901a\u77e5\u5185\u6838\u4e00\u6b21\uff0c\u4e4b\u540e\u5c31\u4e0d\u4f1a\u518d\u6b21\u901a\u77e5\u5185\u6838\u3002\u8fd9\u79cd\u65b9\u5f0f\u5341\u5206\u9ad8\u6548\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u652f\u6301\u7684\u5e76\u53d1\u5ea6\uff0c\u4f46\u7a0b\u5e8f\u903b\u8f91\u5fc5\u987b\u4e00\u6b21\u6027\u5f88\u597d\u5730\u5904\u7406\u8be5fd\u4e0a\u7684\u4e8b\u4ef6\uff0c\u7f16\u7a0b\u6bd4LT\u66f4\u7e41\u7410\u3002\u6ce8\u610fET\u6a21\u5f0f\u5fc5\u987b\u642d\u914d\u975e\u963b\u585e\u5f0fsocket\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u968f\u65f6\u4f7f\u7528<code>epoll_wait<\/code>\u83b7\u53d6\u6709\u4e8b\u4ef6\u53d1\u751f\u7684fd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int nfds = epoll_wait(epfd, events, maxevents, timeout);<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2devents\u662f\u4e00\u4e2aepoll_event\u7ed3\u6784\u4f53\u6570\u7ec4\uff0cmaxevents\u662f\u53ef\u4f9b\u8fd4\u56de\u7684\u6700\u5927\u4e8b\u4ef6\u5927\u5c0f\uff0c\u4e00\u822c\u662fevents\u7684\u5927\u5c0f\uff0ctimeout\u8868\u793a\u6700\u5927\u7b49\u5f85\u65f6\u95f4\uff0c\u8bbe\u7f6e\u4e3a-1\u8868\u793a\u4e00\u76f4\u7b49\u5f85\u3002<\/p>\n\n\n\n<p>\u5728\u521b\u5efa\u4e86\u670d\u52a1\u5668socket fd\u540e\uff0c\u5c06\u8fd9\u4e2afd\u6dfb\u52a0\u5230epoll\uff0c\u53ea\u8981\u8fd9\u4e2afd\u4e0a\u53d1\u751f\u53ef\u8bfb\u4e8b\u4ef6\uff0c\u8868\u793a\u6709\u4e00\u4e2a\u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u7136\u540eaccept\u8fd9\u4e2a\u5ba2\u6237\u7aef\u5e76\u5c06\u5ba2\u6237\u7aef\u7684socket fd\u6dfb\u52a0\u5230epoll\uff0cepoll\u4f1a\u76d1\u542c\u5ba2\u6237\u7aefsocket fd\u662f\u5426\u6709\u4e8b\u4ef6\u53d1\u751f\uff0c\u5982\u679c\u53d1\u751f\u5219\u5904\u7406\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"33-epoll-api\">3.3 epoll API<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"331-epoll_creat\">3.3.1 epoll_creat<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\"> int epoll_create(int size)<\/code><\/pre>\n\n\n\n<p>\u5185\u6838\u4f1a\u4ea7\u751f\u4e00\u4e2aepoll \u5b9e\u4f8b\u6570\u636e\u7ed3\u6784\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u8fd9\u4e2a\u7279\u6b8a\u7684\u63cf\u8ff0\u7b26\u5c31\u662fepoll\u5b9e\u4f8b\u7684\u53e5\u67c4\uff0c\u540e\u9762\u7684\u4e24\u4e2a\u63a5\u53e3\u90fd\u4ee5\u5b83\u4e3a\u4e2d\u5fc3\uff08\u5373epfd\u5f62\u53c2\uff09\u3002size\u53c2\u6570\u8868\u793a\u6240\u8981\u76d1\u89c6\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6700\u5927\u503c\uff0c\u4e0d\u8fc7\u5728\u540e\u6765\u7684Linux\u7248\u672c\u4e2d\u5df2\u7ecf\u88ab\u5f03\u7528\uff08\u540c\u65f6\uff0csize\u4e0d\u8981\u4f200\uff0c\u4f1a\u62a5invalid argument\u9519\u8bef\uff09<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"332-epoll_ctl\">3.3.2 epoll_ctl<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int epoll_ctl(int epfd\uff0c int op\uff0c int fd\uff0c struct epoll_event *event)<\/code><\/pre>\n\n\n\n<p>\u5c06\u88ab\u76d1\u542c\u7684\u63cf\u8ff0\u7b26\u6dfb\u52a0\u5230\u7ea2\u9ed1\u6811\u6216\u4ece\u7ea2\u9ed1\u6811\u4e2d\u5220\u9664\u6216\u8005\u5bf9\u76d1\u542c\u4e8b\u4ef6\u8fdb\u884c\u4fee\u6539<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">typedef union epoll_data {\nvoid *ptr; \/* \u6307\u5411\u7528\u6237\u81ea\u5b9a\u4e49\u6570\u636e *\/\nint fd; \/* \u6ce8\u518c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26 *\/\nuint32_t u32; \/* 32-bit integer *\/\nuint64_t u64; \/* 64-bit integer *\/\n} epoll_data_t;\nstruct epoll_event {\nuint32_t events; \/* \u63cf\u8ff0epoll\u4e8b\u4ef6 *\/\nepoll_data_t data; \/* \u89c1\u4e0a\u9762\u7684\u7ed3\u6784\u4f53 *\/\n};<\/code><\/pre>\n\n\n\n<p>\u5bf9\u4e8e\u9700\u8981\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u96c6\u5408\uff0cepoll_ctl\u5bf9\u7ea2\u9ed1\u6811\u8fdb\u884c\u7ba1\u7406\uff0c\u7ea2\u9ed1\u6811\u4e2d\u6bcf\u4e2a\u6210\u5458\u7531\u63cf\u8ff0\u7b26\u503c\u548c\u6240\u8981\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6307\u5411\u7684\u6587\u4ef6\u8868\u9879\u7684\u5f15\u7528\u7b49\u7ec4\u6210\u3002<\/p>\n\n\n\n<p>op\u53c2\u6570\u8bf4\u660e\u64cd\u4f5c\u7c7b\u578b\uff1a<\/p>\n\n\n\n<p>EPOLL_CTL_ADD\uff1a\u5411interest list\u6dfb\u52a0\u4e00\u4e2a\u9700\u8981\u76d1\u89c6\u7684\u63cf\u8ff0\u7b26EPOLL_CTL_DEL\uff1a\u4eceinterest list\u4e2d\u5220\u9664\u4e00\u4e2a\u63cf\u8ff0\u7b26EPOLL_CTL_MOD\uff1a\u4fee\u6539interest list\u4e2d\u4e00\u4e2a\u63cf\u8ff0\u7b26struct epoll_event\u7ed3\u6784\u63cf\u8ff0\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684epoll\u884c\u4e3a\u3002\u5728\u4f7f\u7528epoll_wait\u51fd\u6570\u8fd4\u56de\u5904\u4e8eready\u72b6\u6001\u7684\u63cf\u8ff0\u7b26\u5217\u8868\u65f6\uff0c<\/p>\n\n\n\n<p>data\u57df\u662f\u552f\u4e00\u80fd\u7ed9\u51fa\u63cf\u8ff0\u7b26\u4fe1\u606f\u7684\u5b57\u6bb5\uff0c\u6240\u4ee5\u5728\u8c03\u7528epoll_ctl\u52a0\u5165\u4e00\u4e2a\u9700\u8981\u76d1\u6d4b\u7684\u63cf\u8ff0\u7b26\u65f6\uff0c\u4e00\u5b9a\u8981\u5728\u6b64\u57df\u5199\u5165\u63cf\u8ff0\u7b26\u76f8\u5173\u4fe1\u606fevents\u57df\u662fbit mask\uff0c\u63cf\u8ff0\u4e00\u7ec4epoll\u4e8b\u4ef6\uff0c\u5728epoll_ctl\u8c03\u7528\u4e2d\u89e3\u91ca\u4e3a\uff1a\u63cf\u8ff0\u7b26\u6240\u671f\u671b\u7684epoll\u4e8b\u4ef6\uff0c\u53ef\u591a\u9009\u3002\u5e38\u7528\u7684epoll\u4e8b\u4ef6\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>EPOLLIN\uff1a\u63cf\u8ff0\u7b26\u5904\u4e8e\u53ef\u8bfb\u72b6\u6001EPOLLOUT\uff1a\u63cf\u8ff0\u7b26\u5904\u4e8e\u53ef\u5199\u72b6\u6001EPOLLET\uff1a\u5c06epoll event\u901a\u77e5\u6a21\u5f0f\u8bbe\u7f6e\u6210edge triggeredEPOLLONESHOT\uff1a\u7b2c\u4e00\u6b21\u8fdb\u884c\u901a\u77e5\uff0c\u4e4b\u540e\u4e0d\u518d\u76d1\u6d4bEPOLLHUP\uff1a\u672c\u7aef\u63cf\u8ff0\u7b26\u4ea7\u751f\u4e00\u4e2a\u6302\u65ad\u4e8b\u4ef6\uff0c\u9ed8\u8ba4\u76d1\u6d4b\u4e8b\u4ef6EPOLLRDHUP\uff1a\u5bf9\u7aef\u63cf\u8ff0\u7b26\u4ea7\u751f\u4e00\u4e2a\u6302\u65ad\u4e8b\u4ef6EPOLLPRI\uff1a\u7531\u5e26\u5916\u6570\u636e\u89e6\u53d1EPOLLERR\uff1a\u63cf\u8ff0\u7b26\u4ea7\u751f\u9519\u8bef\u65f6\u89e6\u53d1\uff0c\u9ed8\u8ba4\u68c0\u6d4b\u4e8b\u4ef6<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"333-epoll_wait\">3.3.3 epoll_wait<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int epoll_wait(int epfd\uff0c struct epoll_event *events\uff0c int maxevents\uff0c int timeout);<\/code><\/pre>\n\n\n\n<p>\u963b\u585e\u7b49\u5f85\u6ce8\u518c\u7684\u4e8b\u4ef6\u53d1\u751f\uff0c\u8fd4\u56de\u4e8b\u4ef6\u7684\u6570\u76ee\uff0c\u5e76\u5c06\u89e6\u53d1\u7684\u4e8b\u4ef6\u5199\u5165events\u6570\u7ec4\u4e2d\u3002<\/p>\n\n\n\n<p>events: \u7528\u6765\u8bb0\u5f55\u88ab\u89e6\u53d1\u7684events\uff0c\u5176\u5927\u5c0f\u5e94\u8be5\u548cmaxevents\u4e00\u81f4<\/p>\n\n\n\n<p>maxevents: \u8fd4\u56de\u7684events\u7684\u6700\u5927\u4e2a\u6570\u5904\u4e8eready\u72b6\u6001\u7684\u90a3\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u4f1a\u88ab\u590d\u5236\u8fdbready list\u4e2d\uff0cepoll_wait\u7528\u4e8e\u5411\u7528\u6237\u8fdb\u7a0b\u8fd4\u56deready list\u3002<\/p>\n\n\n\n<p>events\u548cmaxevents\u4e24\u4e2a\u53c2\u6570\u63cf\u8ff0\u4e00\u4e2a\u7531\u7528\u6237\u5206\u914d\u7684struct epoll event\u6570\u7ec4\uff0c\u8c03\u7528\u8fd4\u56de\u65f6\uff0c\u5185\u6838\u5c06ready list\u590d\u5236\u5230\u8fd9\u4e2a\u6570\u7ec4\u4e2d\uff0c\u5e76\u5c06\u5b9e\u9645\u590d\u5236\u7684\u4e2a\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u5982\u679cready list\u6bd4maxevents\u957f\uff0c\u5219\u53ea\u80fd\u590d\u5236\u524dmaxevents\u4e2a\u6210\u5458\uff1b\u53cd\u4e4b\uff0c\u5219\u80fd\u591f\u5b8c\u5168\u590d\u5236ready list\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0cstruct epoll event\u7ed3\u6784\u4e2d\u7684events\u57df\u5728\u8fd9\u91cc\u7684\u89e3\u91ca\u662f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u5728\u88ab\u76d1\u6d4b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u5b9e\u9645\u53d1\u751f\u7684\u4e8b\u4ef6\u3002<\/p><\/blockquote>\n\n\n\n<p>\u53c2\u6570timeout\u63cf\u8ff0\u5728\u51fd\u6570\u8c03\u7528\u4e2d\u963b\u585e\u65f6\u95f4\u4e0a\u9650\uff0c\u5355\u4f4d\u662fms\uff1a<\/p>\n\n\n\n<p>timeout = -1\u8868\u793a\u8c03\u7528\u5c06\u4e00\u76f4\u963b\u585e\uff0c\u76f4\u5230\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u8fdb\u5165ready\u72b6\u6001\u6216\u8005\u6355\u83b7\u5230\u4fe1\u53f7\u624d\u8fd4\u56de\uff1btimeout = 0\u7528\u4e8e\u975e\u963b\u585e\u68c0\u6d4b\u662f\u5426\u6709\u63cf\u8ff0\u7b26\u5904\u4e8eready\u72b6\u6001\uff0c\u4e0d\u7ba1\u7ed3\u679c\u600e\u4e48\u6837\uff0c\u8c03\u7528\u90fd\u7acb\u5373\u8fd4\u56de\uff1btimeout &gt; 0\u8868\u793a\u8c03\u7528\u5c06\u6700\u591a\u6301\u7eedtimeout\u65f6\u95f4\uff0c\u5982\u679c\u671f\u95f4\u6709\u68c0\u6d4b\u5bf9\u8c61\u53d8\u4e3aready\u72b6\u6001\u6216\u8005\u6355\u83b7\u5230\u4fe1\u53f7\u5219\u8fd4\u56de\uff0c\u5426\u5219\u76f4\u5230\u8d85\u65f6\u3002epoll\u7684\u4e24\u79cd\u89e6\u53d1\u65b9\u5f0f<\/p>\n\n\n\n<p>epoll\u76d1\u63a7\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684I\/O\u4e8b\u4ef6\u3002epoll\u652f\u6301\u8fb9\u7f18\u89e6\u53d1(edge trigger\uff0cET)\u6216\u6c34\u5e73\u89e6\u53d1\uff08level trigger\uff0cLT)\uff0c\u901a\u8fc7epoll_wait\u7b49\u5f85I\/O\u4e8b\u4ef6\uff0c\u5982\u679c\u5f53\u524d\u6ca1\u6709\u53ef\u7528\u7684\u4e8b\u4ef6\u5219\u963b\u585e\u8c03\u7528\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p>select\u548cpoll\u53ea\u652f\u6301LT\u5de5\u4f5c\u6a21\u5f0f\uff0cepoll\u7684\u9ed8\u8ba4\u7684\u5de5\u4f5c\u6a21\u5f0f\u662fLT\u6a21\u5f0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u53c2\u8003\u6587\u732e\">\u53c2\u8003\u6587\u732e<\/h2>\n\n\n\n<p><a href=\"https:\/\/cloud.tencent.com\/developer\/article\/1805838\">\u56fe\u6587\u8be6\u89e3 epoll \u539f\u7406\u3010Redis\uff0cNetty\uff0cNginx\u5b9e\u73b0\u9ad8\u6027\u80fdIO\u7684\u6838\u5fc3\u539f\u7406\u3011epoll \u8be6\u89e3 &#8211; \u817e\u8baf\u4e91\u5f00\u53d1\u8005\u793e\u533a-\u817e\u8baf\u4e91 (tencent.com)<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/93609693\">\u6df1\u5165\u7406\u89e3 Epoll &#8211; \u77e5\u4e4e (zhihu.com)<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/csblog.cc\/30dayMakeCppServer\/day03-%E9%AB%98%E5%B9%B6%E5%8F%91%E8%BF%98%E5%BE%97%E7%94%A8epoll.html\">day03-\u9ad8\u5e76\u53d1\u8fd8\u5f97\u7528epoll | csblog<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ socket epoll\u521d\u8bc6 1.\u4e3a\u4ec0\u4e48\u8981&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/\">Continue reading<span class=\"screen-reader-text\">C++ socket epoll\u521d\u8bc6<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[11],"tags":[8,16,6,7],"class_list":["post-170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tec","tag-c","tag-web","tag-6","tag-7","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ socket epoll\u521d\u8bc6 - LMC\u7684\u4e2a\u4eba\u7ad9<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ socket epoll\u521d\u8bc6 - LMC\u7684\u4e2a\u4eba\u7ad9\" \/>\n<meta property=\"og:description\" content=\"C++ socket epoll\u521d\u8bc6 1.\u4e3a\u4ec0\u4e48\u8981&#8230;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/\" \/>\n<meta property=\"og:site_name\" content=\"LMC\u7684\u4e2a\u4eba\u7ad9\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-23T12:54:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-23T12:54:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/10\/2021-07-06-10-23-40.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1828\" \/>\n\t<meta property=\"og:image:height\" content=\"962\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"SimpleAstronaut\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"SimpleAstronaut\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/\"},\"author\":{\"name\":\"SimpleAstronaut\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/#\\\/schema\\\/person\\\/1edca9cfe5ff17dbe947c332a66ee539\"},\"headline\":\"C++ socket epoll\u521d\u8bc6\",\"datePublished\":\"2022-10-23T12:54:19+00:00\",\"dateModified\":\"2022-10-23T12:54:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/\"},\"wordCount\":336,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/lmceric.top\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/2021-07-06-10-23-40.png\",\"keywords\":[\"C++\",\"web\",\"\u4e92\u8054\u7f51\",\"\u4e92\u8054\u7f51\u6280\u672f\"],\"articleSection\":[\"\u6280\u672f\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/\",\"url\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/\",\"name\":\"C++ socket epoll\u521d\u8bc6 - LMC\u7684\u4e2a\u4eba\u7ad9\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/lmceric.top\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/2021-07-06-10-23-40.png\",\"datePublished\":\"2022-10-23T12:54:19+00:00\",\"dateModified\":\"2022-10-23T12:54:21+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/#\\\/schema\\\/person\\\/1edca9cfe5ff17dbe947c332a66ee539\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/#primaryimage\",\"url\":\"https:\\\/\\\/lmceric.top\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/2021-07-06-10-23-40.png\",\"contentUrl\":\"https:\\\/\\\/lmceric.top\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/2021-07-06-10-23-40.png\",\"width\":1828,\"height\":962},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/2022\\\/10\\\/23\\\/170\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/lmceric.top\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ socket epoll\u521d\u8bc6\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/#website\",\"url\":\"https:\\\/\\\/lmceric.top\\\/\",\"name\":\"LMC\u7684\u4e2a\u4eba\u7ad9\",\"description\":\"\u5b64\u5e06\u8fdc\u5f71\u78a7\u7a7a\u5c3d\uff0c\u552f\u89c1\u957f\u6c5f\u5929\u9645\u6d41\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/lmceric.top\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/lmceric.top\\\/#\\\/schema\\\/person\\\/1edca9cfe5ff17dbe947c332a66ee539\",\"name\":\"SimpleAstronaut\",\"sameAs\":[\"http:\\\/\\\/lmceric.top\"],\"url\":\"https:\\\/\\\/lmceric.top\\\/index.php\\\/author\\\/simpleastronaut\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ socket epoll\u521d\u8bc6 - LMC\u7684\u4e2a\u4eba\u7ad9","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/","og_locale":"zh_CN","og_type":"article","og_title":"C++ socket epoll\u521d\u8bc6 - LMC\u7684\u4e2a\u4eba\u7ad9","og_description":"C++ socket epoll\u521d\u8bc6 1.\u4e3a\u4ec0\u4e48\u8981&#8230;","og_url":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/","og_site_name":"LMC\u7684\u4e2a\u4eba\u7ad9","article_published_time":"2022-10-23T12:54:19+00:00","article_modified_time":"2022-10-23T12:54:21+00:00","og_image":[{"width":1828,"height":962,"url":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/10\/2021-07-06-10-23-40.png","type":"image\/png"}],"author":"SimpleAstronaut","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"SimpleAstronaut","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/#article","isPartOf":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/"},"author":{"name":"SimpleAstronaut","@id":"https:\/\/lmceric.top\/#\/schema\/person\/1edca9cfe5ff17dbe947c332a66ee539"},"headline":"C++ socket epoll\u521d\u8bc6","datePublished":"2022-10-23T12:54:19+00:00","dateModified":"2022-10-23T12:54:21+00:00","mainEntityOfPage":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/"},"wordCount":336,"commentCount":0,"image":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/#primaryimage"},"thumbnailUrl":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/10\/2021-07-06-10-23-40.png","keywords":["C++","web","\u4e92\u8054\u7f51","\u4e92\u8054\u7f51\u6280\u672f"],"articleSection":["\u6280\u672f"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/","url":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/","name":"C++ socket epoll\u521d\u8bc6 - LMC\u7684\u4e2a\u4eba\u7ad9","isPartOf":{"@id":"https:\/\/lmceric.top\/#website"},"primaryImageOfPage":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/#primaryimage"},"image":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/#primaryimage"},"thumbnailUrl":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/10\/2021-07-06-10-23-40.png","datePublished":"2022-10-23T12:54:19+00:00","dateModified":"2022-10-23T12:54:21+00:00","author":{"@id":"https:\/\/lmceric.top\/#\/schema\/person\/1edca9cfe5ff17dbe947c332a66ee539"},"breadcrumb":{"@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/#primaryimage","url":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/10\/2021-07-06-10-23-40.png","contentUrl":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/10\/2021-07-06-10-23-40.png","width":1828,"height":962},{"@type":"BreadcrumbList","@id":"https:\/\/lmceric.top\/index.php\/2022\/10\/23\/170\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/lmceric.top\/"},{"@type":"ListItem","position":2,"name":"C++ socket epoll\u521d\u8bc6"}]},{"@type":"WebSite","@id":"https:\/\/lmceric.top\/#website","url":"https:\/\/lmceric.top\/","name":"LMC\u7684\u4e2a\u4eba\u7ad9","description":"\u5b64\u5e06\u8fdc\u5f71\u78a7\u7a7a\u5c3d\uff0c\u552f\u89c1\u957f\u6c5f\u5929\u9645\u6d41","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/lmceric.top\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/lmceric.top\/#\/schema\/person\/1edca9cfe5ff17dbe947c332a66ee539","name":"SimpleAstronaut","sameAs":["http:\/\/lmceric.top"],"url":"https:\/\/lmceric.top\/index.php\/author\/simpleastronaut\/"}]}},"jetpack_featured_media_url":"https:\/\/lmceric.top\/wp-content\/uploads\/2022\/10\/2021-07-06-10-23-40.png","_links":{"self":[{"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":1,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions\/172"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/media\/171"}],"wp:attachment":[{"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lmceric.top\/index.php\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}