{"id":77685,"date":"2023-09-06T22:02:07","date_gmt":"2023-09-06T14:02:07","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=77685"},"modified":"2023-09-06T22:02:07","modified_gmt":"2023-09-06T14:02:07","slug":"linux-io-multiplexing","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-io-multiplexing.html","title":{"rendered":"Linux I\/O\u591a\u8def\u590d\u7528"},"content":{"rendered":"

<\/strong><\/p>\n\n\n\n
\u5bfc\u8bfb<\/td>\nI\/O\u591a\u8def\u590d\u7528\u901a\u8fc7\u4e00\u79cd\u673a\u5236\uff0c\u53ef\u4ee5\u76d1\u89c6\u591a\u4e2a\u63cf\u8ff0\u7b26\uff0c\u4e00\u65e6\u67d0\u4e2a\u63cf\u8ff0\u7b26\u5c31\u7eea\uff08\u4e00\u822c\u662f\u8bfb\u5c31\u7eea\u6216\u8005\u5199\u5c31\u7eea\uff09\uff0c\u80fd\u591f\u901a\u77e5\u7a0b\u5e8f\u8fdb\u884c\u76f8\u5e94\u7684\u8bfb\u5199\u64cd\u4f5c\u3002I\/O \u591a\u8def\u590d\u7528\u6280\u672f\u662f\u4e3a\u4e86\u89e3\u51b3\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u963b\u585e\u5230\u67d0\u4e2aI\/O\u7cfb\u7edf\u8c03\u7528\u800c\u51fa\u73b0\u7684\u6280\u672f\uff0c\u4f7f\u8fdb\u7a0b\u4e0d\u963b\u585e\u4e8e\u67d0\u4e2a\u7279\u5b9a\u7684 I\/O \u7cfb\u7edf\u8c03\u7528\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
I\/O\u591a\u8def\u590d\u7528select<\/strong><\/div>\n

\u8be5\u51fd\u6570\u51c6\u8bb8\u8fdb\u7a0b\u6307\u793a\u5185\u6838\u7b49\u5f85\u591a\u4e2a\u4e8b\u4ef6\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u53d1\u9001\uff0c\u5e76\u53ea\u5728\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u4e8b\u4ef6\u53d1\u751f\u6216\u7ecf\u5386\u4e00\u6bb5\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u624d\u5524\u9192\u3002<\/p>\n

select\u51fd\u6570<\/strong><\/span><\/div>\n

1.1 \u9700\u8981\u5934\u6587\u4ef6<\/strong><\/p>\n

\r\n#include <sys\/select.h>\r\n\r\n#include <sys\/time.h>\r\n\r\n#include <sys\/types.h>\r\n\r\n#include <unistd.h\/>\r\n<\/pre>\n

1.2 \u58f0\u660e\u548c\u8fd4\u56de\u503c<\/strong><\/p>\n

1. \u58f0\u660e<\/p>\n

\r\nint select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);\r\n<\/pre>\n

2. \u8fd4\u56de\u503c<\/p>\n

\u6210\u529f\uff1a\u5c31\u7eea\u63cf\u8ff0\u7b26\u7684\u6570\u76ee\uff0c\u8d85\u65f6\u8fd4\u56de 0\u3002<\/p>\n

\u51fa\u9519\uff1a-1\u3002<\/p>\n

1.3 \u529f\u80fd<\/strong><\/p>\n

\u76d1\u89c6\u5e76\u7b49\u5f85\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5c5e\u6027\u53d8\u5316\uff08\u53ef\u8bfb\u3001\u53ef\u5199\u6216\u9519\u8bef\u5f02\u5e38\uff09\u3002select()\u51fd\u6570\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5206 3 \u7c7b\uff0c\u5206\u522b\u662fwritefds\u3001readfds\u3001\u548cexceptfds\u3002\u8c03\u7528\u540eselect() \u51fd\u6570\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u6709\u63cf\u8ff0\u7b26\u5c31\u7eea\uff08\u6709\u6570\u636e\u53ef\u8bfb\u3001\u53ef\u5199\u3001\u6216\u8005\u6709\u9519\u8bef\u5f02\u5e38\uff09\uff0c\u6216\u8005\u8d85\u65f6\uff08 timeout \u6307\u5b9a\u7b49\u5f85\u65f6\u95f4\uff09\uff0c\u51fd\u6570\u624d\u8fd4\u56de\u3002\u5f53select()\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386 fdset\uff0c\u6765\u627e\u5230\u5c31\u7eea\u7684\u63cf\u8ff0\u7b26\u3002<\/p>\n

1.4 \u53c2\u6570<\/strong><\/p>\n

1. nfds: \u8981\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u8303\u56f4\uff0c\u4e00\u822c\u53d6\u76d1\u89c6\u7684\u63cf\u8ff0\u7b26\u6570\u7684\u6700\u5927\u503c+1\uff0c\u5982\u8fd9\u91cc\u5199 10\uff0c \u8fd9\u6837\u7684\u8bdd\uff0c\u63cf\u8ff0\u7b26 0\uff0c1, 2 \u2026\u2026 9 \u90fd\u4f1a\u88ab\u76d1\u89c6\uff0c\u5728 Linux \u4e0a\u6700\u5927\u503c\u4e00\u822c\u4e3a1024\u3002<\/p>\n

2. readfd: \u76d1\u89c6\u7684\u53ef\u8bfb\u63cf\u8ff0\u7b26\u96c6\u5408\uff0c\u53ea\u8981\u6709\u6587\u4ef6\u63cf\u8ff0\u7b26\u5373\u5c06\u8fdb\u884c\u8bfb\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u5b58\u50a8\u5230\u8fd9\u3002<\/p>\n

3. writefds: \u76d1\u89c6\u7684\u53ef\u5199\u63cf\u8ff0\u7b26\u96c6\u5408\u3002<\/p>\n

4. exceptfds: \u76d1\u89c6\u7684\u9519\u8bef\u5f02\u5e38\u63cf\u8ff0\u7b26\u96c6\u5408\u3002<\/p>\n

5. timeout\u544a\u77e5\u5185\u6838\u7b49\u5f85\u6240\u6307\u5b9a\u63cf\u8ff0\u5b57\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u5c31\u7eea\u53ef\u82b1\u591a\u5c11\u65f6\u95f4\u3002 \u5176timeval\u7ed3\u6784\u7528\u4e8e\u6307\u5b9a\u8fd9\u6bb5\u65f6\u95f4\u7684\u79d2\u6570\u548c\u5fae\u79d2\u6570\u3002<\/p>\n

\r\nstruct timeval{\r\n\r\nlong tv_sec;   \/\/seconds\r\n\r\nlong tv_usec;  \/\/microseconds\r\n\r\n};\r\n<\/pre>\n

timeout\u53ef\u4ee5\u8bbe\u7f6e\u7684\u503c\uff1a<\/p>\n

1\u3001\u628a\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3a\u7a7a\u6307\u9488NULL\u3002\u8868\u793a\u6c38\u8fdc\u7b49\u5f85\u4e0b\u53bb\uff0c\u5f53\u6709\u4e00\u4e2a\u63cf\u8ff0\u5b57\u51c6\u5907\u597dI\/O\u65f6\u624d\u8fd4\u56de\u3002<\/p>\n

2\u3001\u628a\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u4e86timeval\u7ed3\u6784\u4e2d\u7684\u79d2\u6570\u548c\u5fae\u79d2\u6570\u7684\u503c\u3002\u8868\u793a\u7b49\u5f85\u6307\u5b9a\u4e86\u8d85\u65f6\u65f6\u95f4\uff0c\u5f53\u8d85\u65f6\u540e\u8fd8\u6ca1\u6709\u63cf\u8ff0\u5b57\u51c6\u5907\u597dI\/O\u65f6\u76f4\u63a5\u8fd4\u56de\u3002<\/p>\n

3\u3001\u628a\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u4e86timeval\u7ed3\u6784\u4e2d\u7684\u79d2\u6570\u548c\u5fae\u79d2\u6570\u7684\u503c\uff0c\u800c\u4e14\u79d2\u6570\u548c\u5fae\u79d2\u90fd\u4e3a0\u3002\u8868\u793a\u4e0d\u68c0\u67e5\u63cf\u8ff0\u5b57\u662f\u5426\u51c6\u5907\u597dI\/O\u540e\u7acb\u5373\u8fd4\u56de\uff0c\u8fd9\u79f0\u4e3a\u8f6e\u8be2\u3002<\/p>\n

1.5 fd_set<\/strong><\/p>\n

fd_set\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u96c6\u5408\uff0c\u8fd9\u4e2a\u96c6\u5408\u4e2d\u5b58\u653e\u7684\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u56db\u4e2a\u5b8f\u8fdb\u884c\u8bbe\u7f6e\uff1a<\/p>\n

\r\n1.  void FD_ZERO(fd_set *fdset);       \/\/\u6e05\u7a7a\u96c6\u5408\r\n\r\n2.  void FD_SET(int fd, fd_set *fdset);   \/\/\u5c06\u4e00\u4e2a\u7ed9\u5b9a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u52a0\u5165\u96c6\u5408\u4e4b\u4e2d\r\n\r\n3.  void FD_CLR(int fd, fd_set *fdset);   \/\/\u5c06\u4e00\u4e2a\u7ed9\u5b9a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4ece\u96c6\u5408\u4e2d\u5220\u9664\r\n\r\n4.  int FD_ISSET(int fd, fd_set *fdset);   \/\/\u68c0\u67e5\u96c6\u5408\u4e2d\u6307\u5b9a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u53ef\u4ee5\u8bfb\u5199\r\n<\/pre>\n
select\u4f18\u70b9\u548c\u7f3a\u70b9<\/strong><\/span><\/div>\n

2.1 \u4f18\u70b9<\/strong><\/p>\n

select()\u76ee\u524d\u51e0\u4e4e\u5728\u6240\u6709\u7684\u5e73\u53f0\u4e0a\u652f\u6301\uff0c\u5176\u826f\u597d\u8de8\u5e73\u53f0\u652f\u6301\u4e5f\u662f\u5b83\u7684\u4e00\u4e2a\u4f18\u70b9\u3002<\/p>\n

2.2 \u7f3a\u70b9<\/strong><\/p>\n

1\u3001\u6bcf\u6b21\u8c03\u7528 select()\uff0c\u90fd\u9700\u8981\u628afd\u96c6\u5408\u4ece\u7528\u6237\u6001\u62f7\u8d1d\u5230\u5185\u6838\u6001\uff0c\u8fd9\u4e2a\u5f00\u9500\u5728fd\u5f88\u591a\u65f6\u4f1a\u5f88\u5927\uff0c\u540c\u65f6\u6bcf\u6b21\u8c03\u7528select()\u90fd\u9700\u8981\u5728\u5185\u6838\u904d\u5386\u4f20\u9012\u8fdb\u6765\u7684\u6240\u6709fd\uff0c\u8fd9\u4e2a\u5f00\u9500\u5728fd\u5f88\u591a\u65f6\u4e5f\u5f88\u5927\u3002<\/p>\n

2\u3001\u5355\u4e2a\u8fdb\u7a0b\u80fd\u591f\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6570\u91cf\u5b58\u5728\u6700\u5927\u9650\u5236\uff0c\u5728Linux\u4e0a\u4e00\u822c\u4e3a1024\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u5b8f\u5b9a\u4e49\u751a\u81f3\u91cd\u65b0\u7f16\u8bd1\u5185\u6838\u7684\u65b9\u5f0f\u63d0\u5347\u8fd9\u4e00\u9650\u5236\uff0c\u4f46\u662f\u8fd9\u6837\u4e5f\u4f1a\u9020\u6210\u6548\u7387\u7684\u964d\u4f4e\u3002<\/p>\n

I\/O\u591a\u8def\u590d\u7528poll<\/strong><\/div>\n

select()\u548cpoll()\u7cfb\u7edf\u8c03\u7528\u7684\u672c\u8d28\u4e00\u6837\uff0c\u524d\u8005\u5728BSD UNIX\u4e2d\u5f15\u5165\u7684\uff0c\u540e\u8005\u5728System V\u4e2d\u5f15\u5165\u7684\u3002poll()\u7684\u673a\u5236\u4e0e select() \u7c7b\u4f3c\uff0c\u4e0e select() \u5728\u672c\u8d28\u4e0a\u6ca1\u6709\u591a\u5927\u5dee\u522b\uff0c\u7ba1\u7406\u591a\u4e2a\u63cf\u8ff0\u7b26\u4e5f\u662f\u8fdb\u884c\u8f6e\u8be2\uff0c\u6839\u636e\u63cf\u8ff0\u7b26\u7684\u72b6\u6001\u8fdb\u884c\u5904\u7406\uff0c\u4f46\u662f poll() \u6ca1\u6709\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u7684\u9650\u5236\uff08\u4f46\u662f\u6570\u91cf\u8fc7\u5927\u540e\u6027\u80fd\u4e5f\u662f\u4f1a\u4e0b\u964d\uff09\u3002poll() \u548c select() \u540c\u6837\u5b58\u5728\u4e00\u4e2a\u7f3a\u70b9\u5c31\u662f\uff0c\u5305\u542b\u5927\u91cf\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6570\u7ec4\u88ab\u6574\u4f53\u590d\u5236\u4e8e\u7528\u6237\u6001\u548c\u5185\u6838\u7684\u5730\u5740\u7a7a\u95f4\u4e4b\u95f4\uff0c\u800c\u4e0d\u8bba\u8fd9\u4e9b\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u5c31\u7eea\uff0c\u5b83\u7684\u5f00\u9500\u968f\u7740\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u7684\u589e\u52a0\u800c\u7ebf\u6027\u589e\u5927\u3002<\/p>\n

poll\u51fd\u6570<\/strong><\/span><\/div>\n

1.1 \u9700\u8981\u5934\u6587\u4ef6<\/strong><\/p>\n

#include <poll.h\/><\/pre>\n

1.2 \u58f0\u660e\u548c\u8fd4\u56de\u503c<\/strong><\/p>\n

1. \u58f0\u660e<\/p>\n

\r\nint poll(struct pollfd *fds, nfds_t nfds, int timeout);\r\n<\/pre>\n

2. \u8fd4\u56de\u503c<\/p>\n

\u6210\u529f\u65f6\uff0cpoll()\u8fd4\u56de\u7ed3\u6784\u4f53\u4e2drevents\u57df\u4e0d\u4e3a0\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2a\u6570\uff1b\u5982\u679c\u5728\u8d85\u65f6\u524d\u6ca1\u6709\u4efb\u4f55\u4e8b\u4ef6\u53d1\u751f\uff0cpoll()\u8fd4\u56de 0\uff1b<\/p>\n

\u5931\u8d25\u65f6\uff0cpoll()\u8fd4\u56de -1\uff0c\u5e76\u8bbe\u7f6e errno \u4e3a\u4e0b\u5217\u503c\u4e4b\u4e00\uff1a<\/p>\n

EBADF\uff1a\u4e00\u4e2a\u6216\u591a\u4e2a\u7ed3\u6784\u4f53\u4e2d\u6307\u5b9a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u65e0\u6548\u3002<\/p>\n

EFAULT\uff1afds \u6307\u9488\u6307\u5411\u7684\u5730\u5740\u8d85\u51fa\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u3002<\/p>\n

EINTR\uff1a\u8bf7\u6c42\u7684\u4e8b\u4ef6\u4e4b\u524d\u4ea7\u751f\u4e00\u4e2a\u4fe1\u53f7\uff0c\u8c03\u7528\u53ef\u4ee5\u91cd\u65b0\u53d1\u8d77\u3002<\/p>\n

EINVAL\uff1anfds\u53c2\u6570\u8d85\u51fa PLIMIT_NOFILE \u503c\u3002<\/p>\n

ENOMEM\uff1a\u53ef\u7528\u5185\u5b58\u4e0d\u8db3\uff0c\u65e0\u6cd5\u5b8c\u6210\u8bf7\u6c42\u3002<\/p>\n

1.3 \u529f\u80fd<\/strong><\/p>\n

\u76d1\u89c6\u5e76\u7b49\u5f85\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5c5e\u6027\u53d8\u5316\u3002<\/p>\n

1.4 \u53c2\u6570<\/strong><\/p>\n

1. fds \u4e0d\u540c\u4e0eselect()\u4f7f\u7528\u4e09\u4e2a\u4f4d\u56fe\u6765\u8868\u793a\u4e09\u4e2a fdset \u7684\u65b9\u5f0f\uff0cpoll()\u4f7f\u7528\u4e00\u4e2apollfd\u7684\u6307\u9488\u5b9e\u73b0\u3002\u4e00\u4e2apollfd \u7ed3\u6784\u4f53\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u62ec\u4e86\u4f60\u60f3\u6d4b\u8bd5\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u4e8b\u4ef6, \u4e8b\u4ef6\u7531\u7ed3\u6784\u4e2d\u4e8b\u4ef6\u57df events \u6765\u786e\u5b9a\uff0c\u8c03\u7528\u540e\u5b9e\u9645\u53d1\u751f\u7684\u65f6\u95f4\u5c06\u88ab\u586b\u5199\u5728\u7ed3\u6784\u4f53\u7684revents \u57df\u3002<\/p>\n

\r\nstruct pollfd{\r\n\r\nint fd;         \/\/\u6587\u4ef6\u63cf\u8ff0\u7b26\r\n\r\nshort events;   \/\/\u7b49\u5f85\u7684\u4e8b\u4ef6\r\n\r\nshort revents;  \/\/\u5b9e\u9645\u53d1\u751f\u4e86\u7684\u4e8b\u4ef6\r\n\r\n};\r\n<\/pre>\n

fd<\/b> \u6bcf\u4e00\u4e2a pollfd \u7ed3\u6784\u4f53\u6307\u5b9a\u4e86\u4e00\u4e2a\u88ab\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u53ef\u4ee5\u4f20\u9012\u591a\u4e2a\u7ed3\u6784\u4f53\uff0c\u6307\u793apoll()\u76d1\u89c6\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n

events\uff1a\u6bcf\u4e2a\u7ed3\u6784\u4f53\u7684 events \u57df\u662f\u76d1\u89c6\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u63a9\u7801\uff0c\u7531\u7528\u6237\u6765\u8bbe\u7f6e\u8fd9\u4e2a\u57df\u3002events \u7b49\u5f85\u4e8b\u4ef6\u7684\u63a9\u7801\u53d6\u503c\u5982\u4e0b\uff1a<\/p>\n

\u5904\u7406\u8f93\u5165\uff1a<\/p>\n

POLLIN \u666e\u901a\u6216\u4f18\u5148\u7ea7\u5e26\u6570\u636e\u53ef\u8bfb<\/p>\n

POLLRDNORM \u666e\u901a\u6570\u636e\u53ef\u8bfb<\/p>\n

POLLRDBAND \u4f18\u5148\u7ea7\u5e26\u6570\u636e\u53ef\u8bfb<\/p>\n

POLLPRI \u9ad8\u4f18\u5148\u7ea7\u6570\u636e\u53ef\u8bfb<\/p>\n

\u5904\u7406\u8f93\u51fa\uff1a<\/p>\n

POLLOUT \u666e\u901a\u6216\u4f18\u5148\u7ea7\u5e26\u6570\u636e\u53ef\u5199<\/p>\n

POLLWRNORM \u666e\u901a\u6570\u636e\u53ef\u5199<\/p>\n

POLLWRBAND \u4f18\u5148\u7ea7\u5e26\u6570\u636e\u53ef\u5199<\/p>\n

\u5904\u7406\u9519\u8bef\uff1a<\/p>\n

POLLERR\u53d1\u751f\u9519\u8bef<\/p>\n

POLLHUP\u53d1\u751f\u6302\u8d77<\/p>\n

POLLVAL \u63cf\u8ff0\u5b57\u4e0d\u662f\u4e00\u4e2a\u6253\u5f00\u7684\u6587\u4ef6<\/p>\n

poll() \u5904\u7406\u4e09\u4e2a\u7ea7\u522b\u7684\u6570\u636e\uff0c\u666e\u901anormal\uff0c\u4f18\u5148\u7ea7\u5e26priority band\uff0c\u9ad8\u4f18\u5148\u7ea7high priority\uff0c\u8fd9\u4e9b\u90fd\u662f\u51fa\u4e8e\u6d41\u7684\u5b9e\u73b0\u3002<\/p>\n

POLLIN | POLLPRI \u7b49\u4ef7\u4e8eselect()\u7684\u8bfb\u4e8b\u4ef6\u3002<\/p>\n

POLLOUT | POLLWRBAND\u7b49\u4ef7\u4e8eselect() \u7684\u5199\u4e8b\u4ef6\u3002<\/p>\n

POLLIN\u7b49\u4ef7\u4e8ePOLLRDNORM | POLLRDBAND\u3002<\/p>\n

POLLOUT\u7b49\u4ef7\u4e8ePOLLWRNORM\u3002<\/p>\n

\u4f8b\u5982\uff0c\u8981\u540c\u65f6\u76d1\u89c6\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5426\u53ef\u8bfb\u548c\u53ef\u5199\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6eevents\u4e3a POLLIN | POLLOUT\u3002<\/p>\n

revents<\/b>\u57df\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u64cd\u4f5c\u7ed3\u679c\u4e8b\u4ef6\u63a9\u7801\uff0c\u5185\u6838\u5728\u8c03\u7528\u8fd4\u56de\u65f6\u8bbe\u7f6e\u8fd9\u4e2a\u57df\u3002events \u57df\u4e2d\u8bf7\u6c42\u7684\u4efb\u4f55\u4e8b\u4ef6\u90fd\u53ef\u80fd\u5728revents\u57df\u4e2d\u8fd4\u56de\u3002\u6bcf\u4e2a\u7ed3\u6784\u4f53\u7684 events \u57df\u662f\u7531\u7528\u6237\u6765\u8bbe\u7f6e\uff0c\u544a\u8bc9\u5185\u6838\u6211\u4eec\u5173\u6ce8\u7684\u662f\u4ec0\u4e48\uff0c\u800crevents\u57df\u662f\u8fd4\u56de\u65f6\u5185\u6838\u8bbe\u7f6e\u7684\uff0c\u4ee5\u8bf4\u660e\u5bf9\u8be5\u63cf\u8ff0\u7b26\u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b\u4ef6\u3002<\/p>\n

2. nfds \u7528\u6765\u6307\u5b9a\u7b2c\u4e00\u4e2a\u53c2\u6570\u6570\u7ec4\u5143\u7d20\u4e2a\u6570\u3002<\/p>\n

3. timeout: \u6307\u5b9a\u7b49\u5f85\u7684\u6beb\u79d2\u6570\u3002<\/p>\n

\u5982\u679ctimeout\u8bbe\u7f6e\u4e3a\u7b49\u5f85\u7684\u6beb\u79d2\u6570\uff0c\u65e0\u8bbaI\/O\u662f\u5426\u51c6\u5907\u597d\uff0cpoll()\u90fd\u4f1a\u8fd4\u56de\u3002<\/p>\n

\u5982\u679ctimeout\u8bbe\u7f6e\u4e3a 0\u65f6\uff0cpoll() \u51fd\u6570\u7acb\u5373\u8fd4\u56de\u3002<\/p>\n

\u5982\u679ctimeout\u8bbe\u7f6e\u4e3a -1\u65f6\uff0cpoll()\u4e00\u76f4\u963b\u585e\u5230\u4e00\u4e2a\u6307\u5b9a\u4e8b\u4ef6\u53d1\u751f\u3002<\/p>\n

I\/O\u591a\u8def\u590d\u7528epoll<\/strong><\/div>\n

epoll\u662f\u57282.6\u5185\u6838\u4e2d\u63d0\u51fa\u7684\uff0c\u662f\u4e4b\u524d\u7684 select()\u548c poll()\u7684\u589e\u5f3a\u7248\u672c\u3002\u76f8\u5bf9\u4e8e select()\u548c poll()\u6765\u8bf4\uff0cepoll\u66f4\u52a0\u7075\u6d3b\uff0c\u6ca1\u6709\u63cf\u8ff0\u7b26\u9650\u5236\u3002epoll\u4f7f\u7528\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7ba1\u7406\u591a\u4e2a\u63cf\u8ff0\u7b26\uff0c\u5c06\u7528\u6237\u5173\u7cfb\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u5b58\u653e\u5230\u5185\u6838\u7684\u4e00\u4e2a\u4e8b\u4ef6\u8868\u4e2d\uff0c\u8fd9\u6837\u5728\u7528\u6237\u7a7a\u95f4\u548c\u5185\u6838\u7a7a\u95f4\u7684copy\u53ea\u9700\u4e00\u6b21\u3002<\/p>\n

\u9700\u8981\u5934\u6587\u4ef6<\/strong><\/span><\/div>\n
\r\n#include <sys\/epoll.h>\r\n<\/pre>\n
\u58f0\u660e<\/strong><\/span><\/div>\n
\r\nint epoll_create(int size);\r\n\r\nint epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);\r\n\r\nint epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);\r\n<\/pre>\n
epoll_create \u51fd\u6570<\/strong><\/span><\/div>\n

int epoll_create(int size);
\n3.1 \u529f\u80fd<\/p>\n

\u8be5\u51fd\u6570\u751f\u6210\u4e00\u4e2a epoll \u4e13\u7528\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08\u521b\u5efa\u4e00\u4e2a epoll \u7684\u53e5\u67c4\uff09\u3002
\n3.2 \u53c2\u6570<\/p>\n

size<\/b> \u7528\u6765\u544a\u8bc9\u5185\u6838\u8fd9\u4e2a\u76d1\u542c\u7684\u6570\u76ee\u4e00\u5171\u6709\u591a\u5927\uff0c\u53c2\u6570size\u5e76\u4e0d\u662f\u9650\u5236\u4e86 epoll \u6240\u80fd\u76d1\u542c\u7684\u63cf\u8ff0\u7b26\u6700\u5927\u4e2a\u6570\uff0c\u53ea\u662f\u5bf9\u5185\u6838\u521d\u59cb\u5206\u914d\u5185\u90e8\u6570\u636e\u7ed3\u6784\u7684\u4e00\u4e2a\u5efa\u8bae\u3002<\/p>\n

\u81ea\u4ecelinux 2.6.8\u4e4b\u540e\uff0csize \u53c2\u6570\u662f\u88ab\u5ffd\u7565\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u53ef\u4ee5\u586b\u53ea\u6709\u5927\u4e8e0 \u7684\u4efb\u610f\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u521b\u5efa\u597depoll\u53e5\u67c4\u540e\uff0c\u5b83\u5c31\u662f\u4f1a\u5360\u7528\u4e00\u4e2afd\u503c\uff0c\u5728linux\u4e0b\u5982\u679c\u67e5\u770b \/proc\/ \u8fdb\u7a0b id\/fd\/\uff0c\u662f\u80fd\u591f\u770b\u5230\u8fd9\u4e2afd\u7684\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u5b8cepoll\u540e\uff0c\u5fc5\u987b\u8c03\u7528close()\u5173\u95ed\uff0c\u5426\u5219\u53ef\u80fd\u5bfc\u81f4fd\u88ab\u8017\u5c3d\u3002<\/p>\n

3.3 \u8fd4\u56de\u503c<\/strong><\/p>\n

\u6210\u529f\uff1aepoll\u4e13\u7528\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/p>\n

\u5931\u8d25\uff1a-1<\/p>\n

epoll_ctl\u51fd\u6570<\/strong><\/span><\/div>\n
\r\nint epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);\r\n<\/pre>\n

4.1 \u529f\u80fd<\/strong><\/p>\n

epoll\u7684\u4e8b\u4ef6\u6ce8\u518c\u51fd\u6570\uff0c\u5b83\u4e0d\u540c\u4e8eselect()\u662f\u5728\u76d1\u542c\u4e8b\u4ef6\u65f6\u544a\u8bc9\u5185\u6838\u8981\u76d1\u542c\u4ec0\u4e48\u7c7b\u578b\u7684\u4e8b\u4ef6\uff0c\u800c\u662f\u5728\u8fd9\u91cc\u5148\u6ce8\u518c\u8981\u76d1\u542c\u7684\u4e8b\u4ef6\u7c7b\u578b\u3002<\/p>\n

4.2 \u53c2\u6570<\/strong><\/p>\n

1. epfd<\/b> epoll \u4e13\u7528\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0cepoll_create()\u7684\u8fd4\u56de\u503c<\/p>\n

2. op<\/b> \u8868\u793a\u52a8\u4f5c\uff0c\u7528\u4e09\u4e2a\u5b8f\u6765\u8868\u793a\uff1a<\/p>\n

\r\nEPOLL_CTL_ADD\uff1a\u6ce8\u518c\u65b0\u7684 fd \u5230 epfd \u4e2d\uff1b\r\n\r\nEPOLL_CTL_MOD\uff1a\u4fee\u6539\u5df2\u7ecf\u6ce8\u518c\u7684fd\u7684\u76d1\u542c\u4e8b\u4ef6\uff1b\r\n\r\nEPOLL_CTL_DEL\uff1a\u4ece epfd \u4e2d\u5220\u9664\u4e00\u4e2a fd\uff1b\r\n<\/pre>\n

3. fd<\/b> \u9700\u8981\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/p>\n

4. event<\/b> \u544a\u8bc9\u5185\u6838\u8981\u76d1\u542c\u4ec0\u4e48\u4e8b\u4ef6\uff0cstruct epoll_event \u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n

\r\n\/\/ \u4fdd\u5b58\u89e6\u53d1\u4e8b\u4ef6\u7684\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u76f8\u5173\u7684\u6570\u636e\uff08\u4e0e\u5177\u4f53\u4f7f\u7528\u65b9\u5f0f\u6709\u5173\uff09\r\n\r\ntypedef union epoll_data {\r\n\r\nvoid *ptr;\r\n\r\nint fd;\r\n\r\n__uint32_t u32;\r\n\r\n__uint64_t u64;\r\n\r\n} epoll_data_t;\r\n\r\n\r\n\/\/ \u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u548c\u88ab\u89e6\u53d1\u7684\u4e8b\u4ef6\r\n\r\nstruct epoll_event {\r\n\r\n__uint32_t events; \/* Epoll events *\/\r\n\r\nepoll_data_t data; \/* User data variable *\/\r\n\r\n};\r\n<\/pre>\n

events \u53ef\u4ee5\u662f\u4ee5\u4e0b\u51e0\u4e2a\u5b8f\u7684\u96c6\u5408\uff1a<\/p>\n

EPOLLIN \uff1a\u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u8bfb\uff08\u5305\u62ec\u5bf9\u7aef SOCKET \u6b63\u5e38\u5173\u95ed\uff09\uff1b<\/p>\n

EPOLLOUT\uff1a\u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u5199\uff1b<\/p>\n

EPOLLPRI\uff1a\u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6709\u7d27\u6025\u7684\u6570\u636e\u53ef\u8bfb\uff08\u8fd9\u91cc\u5e94\u8be5\u8868\u793a\u6709\u5e26\u5916\u6570\u636e\u5230\u6765\uff09\uff1b<\/p>\n

EPOLLERR\uff1a\u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53d1\u751f\u9519\u8bef\uff1b<\/p>\n

EPOLLHUP\uff1a\u8868\u793a\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u88ab\u6302\u65ad\uff1b<\/p>\n

EPOLLET \uff1a\u5c06 EPOLL \u8bbe\u4e3a\u8fb9\u7f18\u89e6\u53d1(Edge Triggered)\u6a21\u5f0f\uff0c\u8fd9\u662f\u76f8\u5bf9\u4e8e\u6c34\u5e73\u89e6\u53d1(Level Triggered)\u6765\u8bf4\u7684\u3002<\/p>\n

EPOLLONESHOT\uff1a\u53ea\u76d1\u542c\u4e00\u6b21\u4e8b\u4ef6\uff0c\u5f53\u76d1\u542c\u5b8c\u8fd9\u6b21\u4e8b\u4ef6\u4e4b\u540e\uff0c\u5982\u679c\u8fd8\u9700\u8981\u7ee7\u7eed\u76d1\u542c\u8fd9\u4e2a socket \u7684\u8bdd\uff0c\u9700\u8981\u518d\u6b21\u628a\u8fd9\u4e2a socket \u52a0\u5165\u5230 EPOLL \u961f\u5217\u91cc<\/p>\n

4.3 \u8fd4\u56de\u503c<\/strong><\/p>\n

\u6210\u529f\uff1a0<\/p>\n

\u5931\u8d25\uff1a-1<\/p>\n

epoll_wait\u51fd\u6570<\/strong><\/span><\/div>\n
\r\nint epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);\r\n<\/pre>\n

5.1 \u529f\u80fd<\/strong><\/p>\n

\u7b49\u5f85\u4e8b\u4ef6\u7684\u4ea7\u751f\uff0c\u6536\u96c6\u5728 epoll \u76d1\u63a7\u7684\u4e8b\u4ef6\u4e2d\u5df2\u7ecf\u53d1\u9001\u7684\u4e8b\u4ef6\uff0c\u7c7b\u4f3c\u4e8e select() \u8c03\u7528\u3002<\/p>\n

5.2 \u53c2\u6570<\/strong><\/p>\n

1. epfd<\/b> epoll \u4e13\u7528\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0cepoll_create()\u7684\u8fd4\u56de\u503c<\/p>\n

2. events<\/b> \u5206\u914d\u597d\u7684 epoll_event \u7ed3\u6784\u4f53\u6570\u7ec4\uff0cepoll \u5c06\u4f1a\u628a\u53d1\u751f\u7684\u4e8b\u4ef6\u8d4b\u503c\u5230events \u6570\u7ec4\u4e2d\uff08events \u4e0d\u53ef\u4ee5\u662f\u7a7a\u6307\u9488\uff0c\u5185\u6838\u53ea\u8d1f\u8d23\u628a\u6570\u636e\u590d\u5236\u5230\u8fd9\u4e2a events \u6570\u7ec4\u4e2d\uff0c\u4e0d\u4f1a\u53bb\u5e2e\u52a9\u6211\u4eec\u5728\u7528\u6237\u6001\u4e2d\u5206\u914d\u5185\u5b58\uff09\u3002<\/p>\n

3. maxevents<\/b> maxevents \u544a\u4e4b\u5185\u6838\u8fd9\u4e2a events \u6709\u591a\u5927 \u3002<\/p>\n

4. timeout<\/b> \u8d85\u65f6\u65f6\u95f4\u3002<\/p>\n

\u5982\u679ctimeout\u8bbe\u7f6e\u4e3a\u7b49\u5f85\u7684\u6beb\u79d2\u6570\uff0c\u65e0\u8bbaI\/O\u662f\u5426\u51c6\u5907\u597d\uff0c\u90fd\u4f1a\u8fd4\u56de\u3002<\/p>\n

\u5982\u679ctimeout\u8bbe\u7f6e\u4e3a 0\u65f6\uff0c\u51fd\u6570\u7acb\u5373\u8fd4\u56de\u3002<\/p>\n

\u5982\u679ctimeout\u8bbe\u7f6e\u4e3a -1\u65f6\uff0c\u4e00\u76f4\u963b\u585e\u5230\u4e00\u4e2a\u6307\u5b9a\u4e8b\u4ef6\u53d1\u751f\u3002<\/p>\n

5.3 \u8fd4\u56de\u503c<\/strong><\/p>\n

\u6210\u529f\uff1a\u8fd4\u56de\u9700\u8981\u5904\u7406\u7684\u4e8b\u4ef6\u6570\u76ee\uff0c\u5982\u8fd4\u56de 0 \u8868\u793a\u5df2\u8d85\u65f6\u3002<\/p>\n

\u5931\u8d25\uff1a-1<\/p>\n

LT\u6a21\u5f0f\u4e0eET\u6a21\u5f0f<\/strong><\/span><\/div>\n

epoll \u5bf9\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u64cd\u4f5c\u6709\u4e24\u79cd\u6a21\u5f0f\uff1aLT\uff08level trigger\uff09\u548c ET\uff08edge trigger\uff09\u3002LT \u6a21\u5f0f\u662f\u9ed8\u8ba4\u6a21\u5f0f\u3002<\/p>\n

6.1 LT\u6a21\u5f0f<\/strong><\/p>\n

\u5f53 epoll_wait \u68c0\u6d4b\u5230\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u53d1\u751f\u5e76\u5c06\u6b64\u4e8b\u4ef6\u901a\u77e5\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u4e0d\u7acb\u5373\u5904\u7406\u8be5\u4e8b\u4ef6\u3002\u4e0b\u6b21\u8c03\u7528 epoll_wait \u65f6\uff0c\u4f1a\u518d\u6b21\u54cd\u5e94\u5e94\u7528\u7a0b\u5e8f\u5e76\u901a\u77e5\u6b64\u4e8b\u4ef6\u3002<\/p>\n

6.2 ET\u6a21\u5f0f<\/strong><\/p>\n

\u5f53 epoll_wait \u68c0\u6d4b\u5230\u63cf\u8ff0\u7b26\u4e8b\u4ef6\u53d1\u751f\u5e76\u5c06\u6b64\u4e8b\u4ef6\u901a\u77e5\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e94\u7528\u7a0b\u5e8f\u5fc5\u987b\u7acb\u5373\u5904\u7406\u8be5\u4e8b\u4ef6\u3002\u5982\u679c\u4e0d\u5904\u7406\uff0c\u4e0b\u6b21\u8c03\u7528 epoll_wait \u65f6\uff0c\u4e0d\u4f1a\u518d\u6b21\u54cd\u5e94\u5e94\u7528\u7a0b\u5e8f\u5e76\u901a\u77e5\u6b64\u4e8b\u4ef6\u3002<\/p>\n

6.3 LT\u6a21\u5f0f\u4e0eET\u6a21\u5f0f\u6bd4\u8f83<\/strong><\/p>\n

ET\u6a21\u5f0f\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u51cf\u5c11\u4e86epoll \u4e8b\u4ef6\u88ab\u91cd\u590d\u89e6\u53d1\u7684\u6b21\u6570\uff0c\u56e0\u6b64\u6548\u7387\u8981\u6bd4 LT \u6a21\u5f0f\u9ad8\u3002epoll \u5de5\u4f5c\u5728 ET \u6a21\u5f0f\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u4f7f\u7528\u975e\u963b\u585e\u5957\u63a5\u53e3\uff0c\u4ee5\u907f\u514d\u7531\u4e8e\u4e00\u4e2a\u6587\u4ef6\u53e5\u67c4\u7684\u963b\u585e\u8bfb\/\u963b\u585e\u5199\u64cd\u4f5c\u628a\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4efb\u52a1\u997f\u6b7b\u3002<\/p>\n

epoll \u7684\u4f18\u70b9<\/strong><\/span><\/div>\n

1\u3001\u5728 select\/poll\u4e2d\uff0c\u8fdb\u7a0b\u53ea\u6709\u5728\u8c03\u7528\u4e00\u5b9a\u7684\u65b9\u6cd5\u540e\uff0c\u5185\u6838\u624d\u5bf9\u6240\u6709\u76d1\u89c6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8fdb\u884c\u626b\u63cf\uff0c\u800cepoll()\u4e8b\u5148\u901a\u8fc7epoll_ctl()\u6765\u6ce8\u518c\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4e00\u65e6\u57fa\u4e8e\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u7eea\u65f6\uff0c\u5185\u6838\u4f1a\u91c7\u7528\u7c7b\u4f3ccallback\u7684\u56de\u8c03\u673a\u5236(\u8f6f\u4ef6\u4e2d\u65ad )\uff0c\u8fc5\u901f\u6fc0\u6d3b\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5f53\u8fdb\u7a0b\u8c03\u7528 epoll_wait()\u65f6\u4fbf\u5f97\u5230\u901a\u77e5\u3002<\/p>\n

2\u3001\u76d1\u89c6\u7684\u63cf\u8ff0\u7b26\u6570\u91cf\u4e0d\u53d7\u9650\u5236\uff0c\u5b83\u6240\u652f\u6301\u7684 FD \u4e0a\u9650\u662f\u6700\u5927\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u7684\u6570\u76ee\uff0c\u8fd9\u4e2a\u6570\u5b57\u4e00\u822c\u8fdc\u5927\u4e8e2048\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5728 1GB \u5185\u5b58\u7684\u673a\u5668\u4e0a\u5927\u7ea6\u662f 10 \u4e07\u5de6\u53f3\uff0c\u5177\u4f53\u6570\u76ee\u53ef\u4ee5 cat \/proc\/sys\/fs\/file-max \u5bdf\u770b\uff0c\u4e00\u822c\u6765\u8bf4\u8fd9\u4e2a\u6570\u76ee\u548c\u7cfb\u7edf\u5185\u5b58\u5173\u7cfb\u5f88\u5927\u3002select()\u7684\u6700\u5927\u7f3a\u70b9\u5c31\u662f\u8fdb\u7a0b\u6253\u5f00\u7684 fd \u662f\u6709\u6570\u91cf\u9650\u5236\u7684\u3002\u8fd9\u5bf9\u4e8e\u8fde\u63a5\u6570\u91cf\u6bd4\u8f83\u5927\u7684\u670d\u52a1\u5668\u6765\u8bf4\u6839\u672c\u4e0d\u80fd\u6ee1\u8db3\u3002\u867d\u7136\u4e5f\u53ef\u4ee5\u9009\u62e9\u591a\u8fdb\u7a0b\u7684\u89e3\u51b3\u65b9\u6848( Apache \u5c31\u662f\u8fd9\u6837\u5b9e\u73b0\u7684)\uff0c\u4e0d\u8fc7\u867d\u7136 Linux \u4e0a\u9762\u521b\u5efa\u8fdb\u7a0b\u7684\u4ee3\u4ef7\u6bd4\u8f83\u5c0f\uff0c\u4f46\u4ecd\u65e7\u662f\u4e0d\u53ef\u5ffd\u89c6\u7684\uff0c\u52a0\u4e0a\u8fdb\u7a0b\u95f4\u6570\u636e\u540c\u6b65\u8fdc\u6bd4\u4e0d\u4e0a\u7ebf\u7a0b\u95f4\u540c\u6b65\u7684\u9ad8\u6548\uff0c\u6240\u4ee5\u4e5f\u4e0d\u662f\u4e00\u79cd\u5b8c\u7f8e\u7684\u65b9\u6848\u3002<\/p>\n

3\u3001I\/O \u7684\u6548\u7387\u4e0d\u4f1a\u968f\u7740\u76d1\u89c6 fd \u7684\u6570\u91cf\u7684\u589e\u957f\u800c\u4e0b\u964d\u3002select()\uff0cpoll() \u5b9e\u73b0\u9700\u8981\u81ea\u5df1\u4e0d\u65ad\u8f6e\u8be2\u6240\u6709 fd \u96c6\u5408\uff0c\u76f4\u5230\u8bbe\u5907\u5c31\u7eea\uff0c\u671f\u95f4\u53ef\u80fd\u8981\u7761\u7720\u548c\u5524\u9192\u591a\u6b21\u4ea4\u66ff\u3002\u800c epoll \u5176\u5b9e\u4e5f\u9700\u8981\u8c03\u7528 epoll_wait() \u4e0d\u65ad\u8f6e\u8be2\u5c31\u7eea\u94fe\u8868\uff0c\u671f\u95f4\u4e5f\u53ef\u80fd\u591a\u6b21\u7761\u7720\u548c\u5524\u9192\u4ea4\u66ff\uff0c\u4f46\u662f\u5b83\u662f\u8bbe\u5907\u5c31\u7eea\u65f6\uff0c\u8c03\u7528\u56de\u8c03\u51fd\u6570\uff0c\u628a\u5c31\u7eea fd \u653e\u5165\u5c31\u7eea\u94fe\u8868\u4e2d\uff0c\u5e76\u5524\u9192\u5728 epoll_wait() \u4e2d\u8fdb\u5165\u7761\u7720\u7684\u8fdb\u7a0b\u3002\u867d\u7136\u90fd\u8981\u7761\u7720\u548c\u4ea4\u66ff\uff0c\u4f46\u662f select() \u548c poll() \u5728\u201c\u9192\u7740\u201d\u7684\u65f6\u5019\u8981\u904d\u5386\u6574\u4e2afd\u96c6\u5408\uff0c\u800c epoll \u5728\u201c\u9192\u7740\u201d\u7684\u65f6\u5019\u53ea\u8981\u5224\u65ad\u4e00\u4e0b\u5c31\u7eea\u94fe\u8868\u662f\u5426\u4e3a\u7a7a\u5c31\u884c\u4e86\uff0c\u8fd9\u8282\u7701\u4e86\u5927\u91cf\u7684 CPU \u65f6\u95f4\u3002\u8fd9\u5c31\u662f\u56de\u8c03\u673a\u5236\u5e26\u6765\u7684\u6027\u80fd\u63d0\u5347\u3002<\/p>\n

4\u3001select()\uff0cpoll() \u6bcf\u6b21\u8c03\u7528\u90fd\u8981\u628a fd \u96c6\u5408\u4ece\u7528\u6237\u6001\u5f80\u5185\u6838\u6001\u62f7\u8d1d\u4e00\u6b21\uff0c\u800cepoll\u53ea\u8981\u4e00\u6b21\u62f7\u8d1d\uff0c\u8fd9\u4e5f\u80fd\u8282\u7701\u4e0d\u5c11\u7684\u5f00\u9500\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/blog.chinaunix.net\/uid-31429544-id-5762602.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1a http:\/\/lrxjmw.cn\/linux-io-multiplexing.html<\/a>\u7f16\u8f91\uff1a\u90d1\u5e05\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb I\/O\u591a\u8def\u590d\u7528\u901a\u8fc7\u4e00\u79cd\u673a\u5236\uff0c\u53ef\u4ee5\u76d1\u89c6\u591a\u4e2a\u63cf\u8ff0\u7b26\uff0c\u4e00\u65e6\u67d0\u4e2a\u63cf\u8ff0\u7b26\u5c31\u7eea\uff08\u4e00\u822c\u662f\u8bfb\u5c31\u7eea\u6216\u8005\u5199\u5c31\u7eea\uff09\uff0c\u80fd\u591f\u901a\u77e5\u7a0b […]<\/p>\n","protected":false},"author":63,"featured_media":77688,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-77685","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\/77685","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=77685"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/77685\/revisions"}],"predecessor-version":[{"id":282020,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/77685\/revisions\/282020"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/77688"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=77685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=77685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=77685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}