{"id":10679,"date":"2025-04-18T18:08:01","date_gmt":"2025-04-18T10:08:01","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/10679.html"},"modified":"2025-04-18T18:08:01","modified_gmt":"2025-04-18T10:08:01","slug":"%e9%ab%98%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8-epoll%e7%9b%b8%e5%85%b3%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/10679.html","title":{"rendered":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668--epoll\u76f8\u5173\u51fd\u6570"},"content":{"rendered":"<\/p>\n<h3>\u4e00\u3001\u521b\u5efaepoll_create()<\/h3>\n<h4>1.\u529f\u80fd<\/h4>\n<p>\u662f Linux \u7cfb\u7edf\u4e2d\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a epoll \u5b9e\u4f8b\u7684\u7cfb\u7edf\u8c03\u7528\u3002epoll \u662f\u4e00\u79cd\u9ad8\u6548\u7684 I\/O \u4e8b\u4ef6\u901a\u77e5\u673a\u5236&#xff0c;\u901a\u5e38\u7528\u4e8e\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u5668\u6216\u9700\u8981\u5904\u7406\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u573a\u666f\u3002<\/p>\n<h4>2.\u51fd\u6570\u539f\u578b<\/h4>\n<p>int epoll_create(int size); <\/p>\n<h4>3.\u53c2\u6570<\/h4>\n<p>\u2460.\u00a0size: \u6307\u5b9a epoll \u5b9e\u4f8b\u53ef\u4ee5\u76d1\u63a7\u7684\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u3002\u8fd9\u4e2a\u53c2\u6570\u5728 Linux \u5185\u6838 2.6.8 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u5b9e\u9645\u4e0a\u88ab\u5ffd\u7565&#xff0c;\u56e0\u4e3a\u5185\u6838\u4f1a\u52a8\u6001\u8c03\u6574\u8d44\u6e90\u5206\u914d\u3002<\/p>\n<h4>4.\u8fd4\u56de\u503c<\/h4>\n<p>\u2460.\u00a0\u6210\u529f\u65f6\u8fd4\u56de\u4e00\u4e2a\u975e\u8d1f\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8868\u793a\u521b\u5efa\u7684 epoll \u5b9e\u4f8b\u3002 \u2461. \u5931\u8d25\u65f6\u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6e errno \u4e3a\u76f8\u5e94\u7684\u9519\u8bef\u7801\u3002<\/p>\n<\/p>\n<h3>\u4e8c\u3001\u4e0a\u6811\u3001\u4e0b\u6811\u3001\u4fee\u6539epoll_ctl()<\/h3>\n<h4>1.\u529f\u80fd<\/h4>\n<p>\u7528\u4e8e\u5411 epoll \u5b9e\u4f8b\u4e2d\u6dfb\u52a0\u3001\u4fee\u6539\u6216\u5220\u9664\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u76d1\u542c\u3002<\/p>\n<h4>2.\u51fd\u6570\u539f\u578b<\/h4>\n<p>int epoll_ctl(int epfd, int op, int fd, struct epoll_event* event); <\/p>\n<h4>3.\u53c2\u6570<\/h4>\n<p>\u2460.\u00a0epfd: \u901a\u8fc7 epoll_create \u6216 epoll_create1 \u521b\u5efa\u7684 epoll \u5b9e\u4f8b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002 \u2461. op: \u64cd\u4f5c\u7c7b\u578b&#xff0c;\u53ef\u4ee5\u662f\u4ee5\u4e0b\u4e4b\u4e00&#xff1a; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0EPOLL_CTL_ADD: \u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5230 epoll \u5b9e\u4f8b\u4e2d\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0EPOLL_CTL_DEL: \u4ece epoll \u5b9e\u4f8b\u4e2d\u5220\u9664\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0EPOLL_CTL_MOD: \u4fee\u6539\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u63a9\u7801\u3002 \u2462. fd: \u9700\u8981\u64cd\u4f5c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002 \u2463. event: \u6307\u5411 epoll_event \u7ed3\u6784\u4f53\u7684\u6307\u9488&#xff0c;\u7528\u4e8e\u6307\u5b9a\u76d1\u542c\u7684\u4e8b\u4ef6\u7c7b\u578b\u548c\u5173\u8054\u6570\u636e\u3002<\/p>\n<p>struct epoll_event {<br \/>\n    uint32_t events;    \/\/ \u4e8b\u4ef6\u63a9\u7801<br \/>\n    epoll_data_t data;  \/\/ \u7528\u6237\u6570\u636e<br \/>\n}; <\/p>\n<p>events: \u6307\u5b9a\u9700\u8981\u76d1\u542c\u7684\u4e8b\u4ef6\u7c7b\u578b&#xff0c;\u4f8b\u5982 EPOLLIN&#xff08;\u53ef\u8bfb&#xff09;\u3001EPOLLOUT&#xff08;\u53ef\u5199&#xff09;\u7b49\u3002 data: \u7528\u6237\u6570\u636e&#xff0c;\u53ef\u4ee5\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u6307\u9488\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u6570\u636e\u3002<\/p>\n<h4>4.\u8fd4\u56de\u503c<\/h4>\n<p>\u2460.\u00a0\u6210\u529f\u65f6\u8fd4\u56de 0\u3002 \u2461. \u5931\u8d25\u65f6\u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6e errno \u4e3a\u76f8\u5e94\u7684\u9519\u8bef\u7801\u3002<\/p>\n<\/p>\n<h3>\u4e09\u3001\u76d1\u542cepoll_wait()<\/h3>\n<h4>1.\u529f\u80fd<\/h4>\n<p>\u662f Linux \u7cfb\u7edf\u4e2d\u7528\u4e8e\u7b49\u5f85 epoll \u5b9e\u4f8b\u4e2d\u4e8b\u4ef6\u53d1\u751f\u7684\u7cfb\u7edf\u8c03\u7528\u3002\u5b83\u662f epoll \u673a\u5236\u7684\u6838\u5fc3\u90e8\u5206&#xff0c;\u7528\u4e8e\u963b\u585e\u7b49\u5f85\u76f4\u5230\u6709\u4e8b\u4ef6\u53d1\u751f\u6216\u8d85\u65f6\u3002<\/p>\n<h4>2.\u51fd\u6570\u539f\u578b<\/h4>\n<p>int epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout); <\/p>\n<h4>3.\u53c2\u6570<\/h4>\n<p>\u2460.\u00a0epfd: \u901a\u8fc7 epoll_create \u6216 epoll_create1 \u521b\u5efa\u7684 epoll \u5b9e\u4f8b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002 \u2461. events: \u7528\u4e8e\u5b58\u50a8\u8fd4\u56de\u7684\u4e8b\u4ef6\u7684\u6570\u7ec4\u6307\u9488\u3002\u6bcf\u4e2a\u4e8b\u4ef6\u7531\u4e00\u4e2a epoll_event \u7ed3\u6784\u4f53\u8868\u793a\u3002 \u2462. maxevents: \u6307\u5b9a events \u6570\u7ec4\u7684\u6700\u5927\u5bb9\u91cf\u3002\u5fc5\u987b\u5927\u4e8e 0\u3002 \u2463. timeout: \u6307\u5b9a\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f\u7684\u8d85\u65f6\u65f6\u95f4&#xff08;\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d&#xff09;\u3002\u53ef\u4ee5\u662f\u4ee5\u4e0b\u503c&#xff1a; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6b63\u6570&#xff1a;\u7b49\u5f85\u6307\u5b9a\u7684\u6beb\u79d2\u6570\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a00&#xff1a;\u7acb\u5373\u8fd4\u56de&#xff0c;\u4e0d\u963b\u585e\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0-1&#xff1a;\u65e0\u9650\u671f\u963b\u585e&#xff0c;\u76f4\u5230\u6709\u4e8b\u4ef6\u53d1\u751f\u3002<\/p>\n<h4>4.\u8fd4\u56de\u503c<\/h4>\n<p>\u2460.\u00a0\u6210\u529f\u65f6\u8fd4\u56de\u89e6\u53d1\u4e8b\u4ef6\u7684\u6570\u91cf&#xff08;\u53ef\u80fd\u5c0f\u4e8e maxevents&#xff09;\u3002 \u2461. \u5931\u8d25\u65f6\u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6e errno \u4e3a\u76f8\u5e94\u7684\u9519\u8bef\u7801\u3002 \u2462. \u5982\u679c\u8d85\u65f6\u8fd4\u56de 0\u3002<\/p>\n<\/p>\n<h3>\u56db\u3001epoll\u7684\u4e24\u79cd\u89e6\u53d1\u6a21\u5f0f<\/h3>\n<h4>1.\u6c34\u5e73\u89e6\u53d1&#xff08;Level-Triggered&#xff0c;LT&#xff09;<\/h4>\n<p>\u00a0\u6c34\u5e73\u89e6\u53d1\u662fepoll\u7684\u9ed8\u8ba4\u6a21\u5f0f&#xff0c;\u5b83\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u5904\u4e8e\u5c31\u7eea\u72b6\u6001\u65f6\u6301\u7eed\u901a\u77e5\u5e94\u7528\u7a0b\u5e8f&#xff0c;\u76f4\u5230\u4e8b\u4ef6\u88ab\u5904\u7406\u5b8c\u6bd5\u3002 \u2460.\u00a0\u89e6\u53d1\u6761\u4ef6&#xff1a;\u53ea\u8981\u6587\u4ef6\u63cf\u8ff0\u7b26\u5904\u4e8e\u5c31\u7eea\u72b6\u6001&#xff08;\u4f8b\u5982\u53ef\u8bfb\u6216\u53ef\u5199&#xff09;&#xff0c;epoll_wait\u5c31\u4f1a\u6301\u7eed\u8fd4\u56de\u8be5\u4e8b\u4ef6\u3002 \u2461. \u4e8b\u4ef6\u5904\u7406&#xff1a;\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u6301\u7eed\u8f6e\u8be2\u4e8b\u4ef6&#xff0c;\u76f4\u5230\u4e8b\u4ef6\u4e0d\u518d\u5c31\u7eea\u3002 \u2462. \u9002\u7528\u573a\u666f&#xff1a;\u9002\u7528\u4e8e\u7b80\u5355\u7684\u573a\u666f&#xff0c;\u7279\u522b\u662f\u5f53\u4e8b\u4ef6\u5904\u7406\u8f83\u5feb\u65f6\u3002<\/p>\n<\/p>\n<h4>2.\u8fb9\u7f18\u89e6\u53d1&#xff08;Edge-Triggered&#xff0c;ET&#xff09;<\/h4>\n<p>\u8fb9\u7f18\u89e6\u53d1\u53ea\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u901a\u77e5\u5e94\u7528\u7a0b\u5e8f\u4e00\u6b21\u3002<\/p>\n<p>\u2460.\u00a0\u89e6\u53d1\u6761\u4ef6&#xff1a;\u53ea\u6709\u5f53\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u4ece\u975e\u5c31\u7eea\u53d8\u4e3a\u5c31\u7eea\u65f6&#xff0c;epoll_wait\u624d\u4f1a\u8fd4\u56de\u8be5\u4e8b\u4ef6\u3002 \u2461. \u4e8b\u4ef6\u5904\u7406&#xff1a;\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4e00\u6b21\u6027\u5904\u7406\u6240\u6709\u5c31\u7eea\u7684\u4e8b\u4ef6&#xff0c;\u56e0\u4e3a\u540e\u7eed\u5373\u4f7f\u4e8b\u4ef6\u4ecd\u7136\u5c31\u7eea&#xff0c;\u4e5f\u4e0d\u4f1a\u518d\u6b21\u901a\u77e5\u3002 \u2462. \u9002\u7528\u573a\u666f&#xff1a;\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u573a\u666f&#xff0c;\u56e0\u4e3a\u5b83\u51cf\u5c11\u4e86\u4e8b\u4ef6\u901a\u77e5\u7684\u6b21\u6570&#xff0c;\u4ece\u800c\u63d0\u9ad8\u4e86\u6027\u80fd\u3002<\/p>\n<\/p>\n<h3>\u4e94\u3001\u7a0b\u5e8f\u793a\u4f8b<\/h3>\n<h4>1.\u7ba1\u9053\u4e2d\u8fd0\u7528epoll<\/h4>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;sys\/epoll.h&gt;<br \/>\n#include &lt;unistd.h&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\n        int fd[2];<br \/>\n        pid_t pid;<br \/>\n        pipe(fd);<br \/>\n        pid &#061; fork();<br \/>\n        if(pid &lt; 0)<br \/>\n                perror(&#034;fork&#034;);<br \/>\n        else if(pid &#061;&#061; 0){<br \/>\n                close(fd[0]);<br \/>\n                char buf[5];<br \/>\n                char ch &#061; &#039;a&#039;;<br \/>\n                while(1){<br \/>\n                        sleep(3);<br \/>\n                        memset(buf,ch&#043;&#043;,sizeof(buf));<br \/>\n                        write(fd[1], buf, 5);<br \/>\n                }<br \/>\n        }<br \/>\n        else{<br \/>\n                close(fd[1]);<br \/>\n                int epfd &#061; epoll_create(1);<br \/>\n                struct epoll_event ev, evs[1];<br \/>\n                ev.data.fd &#061; fd[0];<br \/>\n                ev.events &#061; EPOLLIN;<br \/>\n                epoll_ctl(epfd, EPOLL_CTL_ADD, fd[0], &amp;ev);<br \/>\n                while(1){<br \/>\n                        int n &#061; epoll_wait(epfd, evs, 1, -1);<br \/>\n                        if(n &#061;&#061; 1){<br \/>\n                                char buf[128] &#061; &#034;&#034;;<br \/>\n                                int ret &#061; read(fd[0], buf, sizeof(buf));<br \/>\n                                if(ret &lt;&#061; 0){<br \/>\n                                        close(fd[0]);<br \/>\n                                        epoll_ctl(epfd, EPOLL_CTL_DEL,fd[0], &amp;ev);<br \/>\n                                        break;<br \/>\n                                }<br \/>\n                                else{<br \/>\n                                        printf(&#034;%s\\\\n&#034;, buf);<br \/>\n                                }<br \/>\n                        }<br \/>\n                }<br \/>\n        }<\/p>\n<p>        return 0;<br \/>\n} <\/p>\n<h4 style=\"background-color:transparent\">2.epoll\u9ad8\u5e76\u53d1\u670d\u52a1\u5668<\/h4>\n<p>\u2460.\u521b\u5efa\u5957\u63a5\u5b57\u3002Ifd &#061; tcp4bind&#061;&gt;socket, bind \u2461.\u5957\u63a5\u5b57\u76d1\u542c\u3002Listen \u2462.\u521b\u5efa\u6811\u3002epfd &#061; epoll_create \u2463.\u5c06Ifd\u4e0a\u6811\u3002\u521d\u59cb\u5316ev\u3001epoll_ctl \u2464.\u76d1\u542cwhile&#xff5b; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2465.\u76d1\u542cepfd\u3002nready &#061; epoll_wait \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2466.\u5982\u679c\u8fd4\u56de\u503cnready&#xff1c;0&#xff0c;break\u8df3\u51fa\u5faa\u73af\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u2467.\u5982\u679c\u8fd4\u56de\u503cnready &#061;0&#xff0c;contine\u7ee7\u51fa\u7eed\u5faa\u73af&#xff0c; \u00a0 \u00a0 \u00a0 \u00a0 \u2468. \u5982\u679c\u8fd4\u56de\u503cnready&gt;0&#xff0c;\u8bf4\u660eepfd\u76d1\u542c\u5230\u53d8\u5316 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u2469\u00a0. \u5982\u679c\u662fcfd\u53d8\u5316&#xff0c;\u8fde\u63a5cfd\u3002Accept\u246a.cfd\u4e0a\u6811\u3002epoll_ct \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u246b.\u5982\u679c\u662fcfd\u53d8\u5316 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u246c.\u68c0\u67e5\u6570\u636ewhile(1){ \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u246d.\u8bfb\u5ba2\u6237\u7aef\u7684\u6570\u636en &#061; read() \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u246e.\u5982\u679cn &#061;&#061; 0&#xff0c;\u5219\u5173\u95edcfd&#xff0c;\u5e76\u4e14\u4e0b\u6811 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u246f.\u5982\u679cn&gt;0,\u4f7f\u7528write\u5c06\u65e5\u5fd7\u6253\u5370\u5230\u7ec8\u7aef\u9875\u9762&#xff0c;\u5e76write\u5199\u56de\u5ba2\u6237\u7aef\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} \u00a0 \u00a0}<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;error.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &#034;wrap.h&#034;<br \/>\n#include &lt;sys\/epoll.h&gt;<\/p>\n<p>int main()<br \/>\n{<\/p>\n<p>        \/\/\u521b\u5efa\u5957\u63a5\u5b57<br \/>\n        int lfd &#061; tcp4bind(8000, NULL);<br \/>\n        \/\/\u5957\u63a5\u5b57\u76d1\u542c<br \/>\n        Listen(lfd, 128);<br \/>\n        \/\/\u521b\u5efa\u6811<br \/>\n        int epfd &#061; epoll_create(1);<br \/>\n        \/\/\u5c06lfd\u4e0a\u6811<br \/>\n        struct epoll_event ev, evs[1024];<br \/>\n        ev.data.fd &#061; lfd;<br \/>\n        ev.events &#061; EPOLLIN;<br \/>\n        epoll_ctl(epfd, EPOLL_CTL_ADD, lfd, &amp;ev);<br \/>\n        \/\/\u76d1\u542c<br \/>\n        while(1){<br \/>\n                int nready &#061; epoll_wait(epfd, evs, 1024, -1);<br \/>\n                printf(&#034;epoll_wait&#8212;&#8212;- \\\\n&#034;);<br \/>\n                if(nready &lt; 0){<br \/>\n                        perror(&#034;&#034;);<br \/>\n                        break;<br \/>\n                }<br \/>\n                else if(nready &#061;&#061; 0){<br \/>\n                        continue;<br \/>\n                }<br \/>\n                else{<br \/>\n                        int i;<br \/>\n                        for(i &#061; 0; i &lt;nready; i&#043;&#043;){<br \/>\n                                \/\/lfd\u53d8\u5316<br \/>\n                                if(evs[i].data.fd &#061;&#061; lfd &amp;&amp; evs[i].events &amp; EPOLLIN){<br \/>\n                                        struct sockaddr_in cliaddr;<br \/>\n                                        char ip[16] &#061; &#034;&#034;;<br \/>\n                                        socklen_t len &#061; sizeof(cliaddr);<br \/>\n                                        int cfd &#061; Accept(lfd,(struct sockaddr *)&amp;cliaddr, &amp;len);<br \/>\n                                        printf(&#034;new client ip &#061; %s, port &#061; %d\\\\n&#034;, inet_ntop(AF_INET, &amp;cliaddr.sin_addr.s_addr, ip, 16), ntohs(cliaddr.sin_port));<br \/>\n                                        \/\/\u8bbe\u7f6ecfd\u975e\u5835\u585e<br \/>\n                                        int flags &#061; fcntl(cfd, F_GETFL);<br \/>\n                                        flags |&#061; O_NONBLOCK;<br \/>\n                                        fcntl(cfd, F_SETFL,flags);<br \/>\n                                        \/\/cfd\u4e0a\u6811<br \/>\n                                        ev.data.fd &#061; cfd;<br \/>\n                                        ev.events &#061; EPOLLIN | EPOLLET; \/\/\u8bbe\u7f6e\u4e3a\u8fb9\u7f18\u89e6\u53d1<br \/>\n                                        epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &amp;ev);<br \/>\n                                }<br \/>\n                                else if(evs[i].events &amp; EPOLLIN){<br \/>\n                                \/\/cfd\u53d8\u5316<br \/>\n                                        while(1){<br \/>\n                                                char buf[4] &#061; &#034;&#034;;<br \/>\n                                                \/\/\u5982\u679c\u8bfb\u4e00\u4e2a\u7f13\u51b2\u533a,\u7f13\u51b2\u533a\u6ca1\u6709\u6570\u636e,\u5982\u679c\u662f\u5e26\u963b\u585e,\u5c31\u963b\u585e\u7b49\u5f85,\u5982\u679c<br \/>\n                                                \/\/\u662f\u975e\u963b\u585e,\u8fd4\u56de\u503c\u7b49\u4e8e-1,\u5e76\u4e14\u4f1a\u5c06errno \u503c\u8bbe\u7f6e\u4e3aEAGAIN<br \/>\n                                                int n &#061; read(evs[i].data.fd, buf, sizeof(buf));<br \/>\n                                                if(n &lt; 0){<br \/>\n                                                        if(errno &#061;&#061; EAGAIN){<br \/>\n                                                                break;<br \/>\n                                                        }<\/p>\n<p>                                                        \/\/\u666e\u901a\u9519\u8bef<br \/>\n                                                        perror(&#034;&#034;);<br \/>\n                                                        close(evs[i].data.fd);\/\/\u5173\u95edcfd<br \/>\n                                                        epoll_ctl(epfd, EPOLL_CTL_DEL, evs[i].data.fd, &amp;evs[i]);\/\/\u5c06cfd\u4e0b\u6811<br \/>\n                                                        break;<br \/>\n                                                }<br \/>\n                                                else if(n &#061;&#061; 0){<br \/>\n                                                        printf(&#034;client colse\\\\n&#034;);<br \/>\n                                                        close(evs[i].data.fd);\/\/\u5173\u95edcfd<br \/>\n                            epoll_ctl(epfd, EPOLL_CTL_DEL, evs[i].data.fd, &amp;evs[i]);\/\/\u5c06cfd\u4e0b\u6811<br \/>\n                            break;<br \/>\n                                                }<br \/>\n                                                else{<br \/>\n                                                        write(STDOUT_FILENO, buf, 4);<br \/>\n                                                        write(evs[i].data.fd, buf, n);<br \/>\n                                                }<br \/>\n                                        }<br \/>\n                                }<br \/>\n                        }<br \/>\n                }<br \/>\n        }<br \/>\n        return 0;<br \/>\n} <\/p>\n<h2 style=\"background-color:transparent\">\u516d\u3001epoll\u673a\u5236<\/h2>\n<p>\u53c2\u8003&#xff1a;<\/p>\n<p>Linux epoll\u5b8c\u5168\u56fe\u89e3&#xff0c;\u5f7b\u5e95\u641e\u61c2epoll\u673a\u5236 &#8211; \u77e5\u4e4e<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb985\u6b21\uff0c\u70b9\u8d5e21\u6b21\uff0c\u6536\u85cf19\u6b21\u3002\u662f Linux \u7cfb\u7edf\u4e2d\u7528\u4e8e\u521b\u5efa\u4e00\u4e2aepoll\u5b9e\u4f8b\u7684\u7cfb\u7edf\u8c03\u7528\u3002epoll\u662f\u4e00\u79cd\u9ad8\u6548\u7684 I\/O \u4e8b\u4ef6\u901a\u77e5\u673a\u5236\uff0c\u901a\u5e38\u7528\u4e8e\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u5668\u6216\u9700\u8981\u5904\u7406\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,43,470],"topic":[],"class_list":["post-10679","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-linux","tag-43","tag-470"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u9ad8\u5e76\u53d1\u670d\u52a1\u5668-epoll\u76f8\u5173\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/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:\/\/www.wsisp.com\/helps\/10679.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668-epoll\u76f8\u5173\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb985\u6b21\uff0c\u70b9\u8d5e21\u6b21\uff0c\u6536\u85cf19\u6b21\u3002\u662f Linux \u7cfb\u7edf\u4e2d\u7528\u4e8e\u521b\u5efa\u4e00\u4e2aepoll\u5b9e\u4f8b\u7684\u7cfb\u7edf\u8c03\u7528\u3002epoll\u662f\u4e00\u79cd\u9ad8\u6548\u7684 I\/O \u4e8b\u4ef6\u901a\u77e5\u673a\u5236\uff0c\u901a\u5e38\u7528\u4e8e\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u5668\u6216\u9700\u8981\u5904\u7406\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u573a\u666f\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/10679.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T10:08:01+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/10679.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/10679.html\",\"name\":\"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668-epoll\u76f8\u5173\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T10:08:01+00:00\",\"dateModified\":\"2025-04-18T10:08:01+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/10679.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/10679.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/10679.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668--epoll\u76f8\u5173\u51fd\u6570\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668-epoll\u76f8\u5173\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","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:\/\/www.wsisp.com\/helps\/10679.html","og_locale":"zh_CN","og_type":"article","og_title":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668-epoll\u76f8\u5173\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb985\u6b21\uff0c\u70b9\u8d5e21\u6b21\uff0c\u6536\u85cf19\u6b21\u3002\u662f Linux \u7cfb\u7edf\u4e2d\u7528\u4e8e\u521b\u5efa\u4e00\u4e2aepoll\u5b9e\u4f8b\u7684\u7cfb\u7edf\u8c03\u7528\u3002epoll\u662f\u4e00\u79cd\u9ad8\u6548\u7684 I\/O \u4e8b\u4ef6\u901a\u77e5\u673a\u5236\uff0c\u901a\u5e38\u7528\u4e8e\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u5668\u6216\u9700\u8981\u5904\u7406\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u573a\u666f\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/10679.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T10:08:01+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/10679.html","url":"https:\/\/www.wsisp.com\/helps\/10679.html","name":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668-epoll\u76f8\u5173\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T10:08:01+00:00","dateModified":"2025-04-18T10:08:01+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/10679.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/10679.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/10679.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u9ad8\u5e76\u53d1\u670d\u52a1\u5668--epoll\u76f8\u5173\u51fd\u6570"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/10679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=10679"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/10679\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=10679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=10679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=10679"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=10679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}