{"id":208388,"date":"2025-08-29T15:49:26","date_gmt":"2025-08-29T07:49:26","guid":{"rendered":"https:\/\/www.server.hk\/blog\/?p=208388"},"modified":"2025-08-29T15:49:26","modified_gmt":"2025-08-29T07:49:26","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-linux-i-o-%e6%a8%a1%e5%9e%8b%ef%bc%9a%e5%bc%80%e5%8f%91%e8%80%85%e5%85%a8%e9%9d%a2%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208388\/","title":{"rendered":"\u6df1\u5165\u7406\u89e3 Linux I\/O \u6a21\u578b\uff1a\u5f00\u53d1\u8005\u5168\u9762\u6307\u5357"},"content":{"rendered":"<p dir=\"auto\">\u5728 Linux \u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684 I\/O \u6a21\u578b\u5bf9\u4e8e\u4f18\u5316\u6027\u80fd\u548c\u6269\u5c55\u6027\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u63a2\u8ba8\u4e86 Linux \u4e2d\u53ef\u7528\u7684\u4e94\u79cd\u4e3b\u8981 I\/O \u6a21\u578b\uff1a\u963b\u585e\u5f0f I\/O\u3001\u975e\u963b\u585e\u5f0f I\/O\u3001I\/O \u591a\u8def\u590d\u7528\u3001\u4fe1\u53f7\u9a71\u52a8\u5f0f I\/O \u548c\u5f02\u6b65 I\/O\u3002\u6bcf\u79cd\u6a21\u578b\u90fd\u6709\u5176\u72ec\u7279\u7684\u7279\u6027\uff0c\u9002\u7528\u4e8e\u7279\u5b9a\u7684\u4f7f\u7528\u573a\u666f\u3002\u672c\u6307\u5357\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u6280\u672f\u6982\u8ff0\uff0c\u5e2e\u52a9\u4ed6\u4eec\u4e3a\u5e94\u7528\u7a0b\u5e8f\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u3002<\/p>\n<h2 dir=\"auto\">Linux I\/O \u64cd\u4f5c\u6982\u8ff0<\/h2>\n<p dir=\"auto\">Linux I\/O \u64cd\u4f5c\u901a\u5e38\u5305\u62ec\u4e24\u4e2a\u9636\u6bb5\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>\u6570\u636e\u5c31\u7eea<\/strong>\uff1a\u7b49\u5f85\u6570\u636e\u53ef\u7528\uff08\u4f8b\u5982\uff0c\u7f51\u7edc\u6570\u636e\u5305\u5230\u8fbe\u6216\u78c1\u76d8\u6570\u636e\u51c6\u5907\u597d\uff09\u3002<\/li>\n<li><strong>\u6570\u636e\u4f20\u8f93<\/strong>\uff1a\u5c06\u6570\u636e\u4ece\u5185\u6838\u590d\u5236\u5230\u7528\u6237\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u5bf9\u4e8e\u7f51\u7edc I\/O\uff0c\u4f8b\u5982\u5957\u63a5\u5b57\u64cd\u4f5c\uff0c\u5185\u6838\u4f1a\u7b49\u5f85\u6570\u636e\u4ece\u7f51\u7edc\u5230\u8fbe\u5e76\u5c06\u5176\u590d\u5236\u5230\u5185\u6838\u7f13\u51b2\u533a\uff0c\u7136\u540e\u518d\u4f20\u8f93\u5230\u7528\u6237\u8fdb\u7a0b\u3002\u7136\u800c\uff0c\u78c1\u76d8 I\/O \u5229\u7528\u5185\u6838\u7684\u7f13\u51b2\u533a\u7f13\u5b58\uff0cwrite() \u64cd\u4f5c\u901a\u5e38\u7acb\u5373\u8fd4\u56de\uff0c\u9664\u975e\u8bbe\u7f6e\u4e86 O_SYNC \u6807\u5fd7\u3002\u5bf9\u4e8e read() \u64cd\u4f5c\uff0c\u5982\u679c\u6570\u636e\u4e0d\u5728\u7f13\u5b58\u4e2d\uff0c\u5185\u6838\u53ef\u80fd\u963b\u585e\u8fdb\u7a0b\u4ee5\u4ece\u78c1\u76d8\u83b7\u53d6\u6570\u636e\uff0c\u4f46\u7531\u4e8e\u65e0\u9700\u8fdc\u7a0b\u4f20\u8f93\u6570\u636e\uff0c\u8fd9\u901a\u5e38\u6bd4\u7f51\u7edc I\/O \u66f4\u5feb\u3002<\/p>\n<h2 dir=\"auto\">1. \u963b\u585e\u5f0f I\/O \u6a21\u578b<\/h2>\n<h3 dir=\"auto\">\u5de5\u4f5c\u539f\u7406<\/h3>\n<p dir=\"auto\">\u5728\u963b\u585e\u5f0f I\/O \u6a21\u578b\u4e2d\uff0cLinux \u5957\u63a5\u5b57\u7684\u9ed8\u8ba4\u884c\u4e3a\u662f\uff0c\u7cfb\u7edf\u8c03\u7528\uff08\u4f8b\u5982 read() \u6216 write()\uff09\u5728\u64cd\u4f5c\u5b8c\u6210\u6216\u53d1\u751f\u9519\u8bef\u4e4b\u524d\u4e0d\u4f1a\u8fd4\u56de\u3002\u5982\u679c\u6570\u636e\u672a\u5c31\u7eea\uff0c\u8c03\u7528\u7ebf\u7a0b\u4f1a\u88ab\u963b\u585e\uff0c\u6682\u505c\u8fdb\u4e00\u6b65\u6267\u884c\uff0c\u76f4\u5230\u64cd\u4f5c\u5b8c\u6210\u3002<\/p>\n<h3 dir=\"auto\">\u7279\u6027<\/h3>\n<ul dir=\"auto\">\n<li><strong>\u884c\u4e3a<\/strong>\uff1a\u8fdb\u7a0b\u7b49\u5f85 I\/O \u64cd\u4f5c\u5b8c\u5168\u5b8c\u6210\u3002<\/li>\n<li><strong>\u5f71\u54cd<\/strong>\uff1a\u963b\u585e\u4f1a\u6682\u505c\u7ebf\u7a0b\uff0c\u4f7f\u5176\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u4efb\u52a1\u6216\u8fde\u63a5\u3002<\/li>\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong>\uff1a\u9002\u5408\u5e76\u53d1\u9700\u6c42\u4f4e\u7684\u7b80\u5355\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u4f18\u5316\u7b56\u7565<\/h3>\n<p dir=\"auto\">\u4e3a\u4e86\u5728\u670d\u52a1\u5668\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7f13\u89e3\u963b\u585e\u5f0f I\/O \u7684\u5c40\u9650\u6027\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>\u591a\u7ebf\u7a0b<\/strong>\uff1a\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u5206\u914d\u4e13\u7528\u7ebf\u7a0b\u3002\u8fd9\u5141\u8bb8\u5176\u4ed6\u7ebf\u7a0b\u5728\u4e00\u4e2a\u7ebf\u7a0b\u963b\u585e\u65f6\u4fdd\u6301\u6d3b\u8dc3\u3002\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\uff0c\u7531\u4e8e\u7ebf\u7a0b\u5f00\u9500\u8f83\u4f4e\uff0c\u63a8\u8350\u4f7f\u7528\u591a\u7ebf\u7a0b\u3002<\/li>\n<li><strong>\u591a\u8fdb\u7a0b<\/strong>\uff1a\u5f53\u9700\u8981\u66f4\u9ad8\u7684\u9694\u79bb\u6027\u6216\u6267\u884c\u5927\u91cf CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\uff0c\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u4f7f\u7528\u5355\u72ec\u7684\u8fdb\u7a0b\u3002<\/li>\n<li><strong>\u7ebf\u7a0b\/\u8fde\u63a5\u6c60<\/strong>\uff1a\u5b9e\u73b0\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u6c60\u4ee5\u91cd\u7528\u8d44\u6e90\uff0c\u51cf\u5c11\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u5c40\u9650\u6027<\/h3>\n<ul dir=\"auto\">\n<li>\u5927\u91cf\u8fde\u63a5\u65f6\u8d44\u6e90\u6d88\u8017\u9ad8\u3002<\/li>\n<li>\u5728\u9ad8\u8d1f\u8f7d\u4e0b\u54cd\u5e94\u6027\u964d\u4f4e\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">2. \u975e\u963b\u585e\u5f0f I\/O \u6a21\u578b<\/h2>\n<h3 dir=\"auto\">\u5de5\u4f5c\u539f\u7406<\/h3>\n<p dir=\"auto\">\u975e\u963b\u585e\u5f0f I\/O \u5141\u8bb8\u8fdb\u7a0b\u542f\u52a8 I\/O \u64cd\u4f5c\u5e76\u7acb\u5373\u8fd4\u56de\uff0c\u5373\u4f7f\u64cd\u4f5c\u65e0\u6cd5\u5b8c\u6210\u3002\u901a\u8fc7\u8bbe\u7f6e\u5957\u63a5\u5b57\u4e3a\u975e\u963b\u585e\u6a21\u5f0f\uff08\u4f7f\u7528 O_NONBLOCK \u6807\u5fd7\uff09\uff0c\u50cf read() \u6216 write() \u8fd9\u6837\u7684\u7cfb\u7edf\u8c03\u7528\u4f1a\u5728\u64cd\u4f5c\u65e0\u6cd5\u8fdb\u884c\u65f6\u8fd4\u56de\u9519\u8bef\uff08\u4f8b\u5982 EAGAIN \u6216 EWOULDBLOCK\uff09\uff0c\u800c\u4e0d\u662f\u963b\u585e\u3002<\/p>\n<h3 dir=\"auto\">\u7279\u6027<\/h3>\n<ul dir=\"auto\">\n<li><strong>\u884c\u4e3a<\/strong>\uff1a\u8fdb\u7a0b\u5468\u671f\u6027\u5730\u8f6e\u8be2\u6587\u4ef6\u63cf\u8ff0\u7b26\u4ee5\u68c0\u67e5\u662f\u5426\u5c31\u7eea\u3002<\/li>\n<li><strong>\u5f71\u54cd<\/strong>\uff1a\u8f6e\u8be2\u53ef\u80fd\u6d6a\u8d39 CPU \u5468\u671f\uff0c\u7279\u522b\u662f\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u5f88\u5c11\u5c31\u7eea\u65f6\u3002<\/li>\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u800c\u4e0d\u963b\u585e\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u5b9e\u73b0\u65b9\u5f0f<\/h3>\n<p dir=\"auto\">\u975e\u963b\u585e\u5f0f I\/O \u901a\u5e38\u6d89\u53ca\uff1a<\/p>\n<ul dir=\"auto\">\n<li>\u4f7f\u7528 fcntl() \u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u975e\u963b\u585e\u6a21\u5f0f\uff08O_NONBLOCK\uff09\u3002<\/li>\n<li>\u5728\u5faa\u73af\u4e2d\u8f6e\u8be2\u63cf\u8ff0\u7b26\u4ee5\u68c0\u67e5\u5c31\u7eea\u72b6\u6001\u3002<\/li>\n<li>\u5904\u7406 EAGAIN \u7b49\u9519\u8bef\u4ee5\u4fbf\u7a0d\u540e\u91cd\u8bd5\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u5c40\u9650\u6027<\/h3>\n<ul dir=\"auto\">\n<li>\u8f6e\u8be2\u6548\u7387\u4f4e\uff0c\u589e\u52a0 CPU \u4f7f\u7528\u7387\u3002<\/li>\n<li>\u5728\u4f4e\u9891\u8f6e\u8be2\u573a\u666f\u4e0b\u5ef6\u8fdf\u8f83\u9ad8\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">3. I\/O \u591a\u8def\u590d\u7528\u6a21\u578b<\/h2>\n<h3 dir=\"auto\">\u5de5\u4f5c\u539f\u7406<\/h3>\n<p dir=\"auto\">I\/O \u591a\u8def\u590d\u7528\u5141\u8bb8\u8fdb\u7a0b\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\uff08\u5982 select()\u3001poll() \u6216 epoll()\uff09\u540c\u65f6\u76d1\u89c6\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u8fd9\u4e9b\u8c03\u7528\u6307\u793a\u54ea\u4e9b\u63cf\u8ff0\u7b26\u5df2\u51c6\u5907\u597d\u8fdb\u884c I\/O\uff0c\u4ece\u800c\u6709\u6548\u5904\u7406\u591a\u4e2a\u8fde\u63a5\u3002<\/p>\n<h3 dir=\"auto\">\u5173\u952e\u7cfb\u7edf\u8c03\u7528<\/h3>\n<h4 dir=\"auto\">select()<\/h4>\n<div dir=\"auto\">\n<div>\n<div>c<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>int select(int maxfdp, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<ul dir=\"auto\">\n<li>\u76d1\u89c6\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u8bfb\u3001\u5199\u6216\u9519\u8bef\u72b6\u6001\u3002<\/li>\n<li>maxfdp\uff1a\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u503c\u52a0\u4e00\u3002<\/li>\n<li>timeout\uff1a\u63a7\u5236\u963b\u585e\u884c\u4e3a\uff08NULL \u8868\u793a\u65e0\u9650\u963b\u585e\uff09\u3002<\/li>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\n<ul dir=\"auto\">\n<li>\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u6709\u9650\uff08FD_SETSIZE\uff0c\u901a\u5e38\u4e3a 1024\uff09\u3002<\/li>\n<li>\u8f93\u5165\/\u8f93\u51fa\u63cf\u8ff0\u7b26\u96c6\u4f1a\u88ab\u4fee\u6539\uff0c\u9700\u8981\u91cd\u65b0\u521d\u59cb\u5316\u3002<\/li>\n<li>\u5bf9\u5927\u91cf\u63cf\u8ff0\u7b26\u7684\u7ebf\u6027\u626b\u63cf\u964d\u4f4e\u6548\u7387\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4 dir=\"auto\">poll()<\/h4>\n<div dir=\"auto\">\n<div>\n<div>c<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>int poll(struct pollfd fds[], nfds_t nfds, int timeout);<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<ul dir=\"auto\">\n<li>\u76d1\u89c6 pollfd \u7ed3\u6784\u6570\u7ec4\uff0c\u6bcf\u4e2a\u7ed3\u6784\u6307\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u76d1\u89c6\u4e8b\u4ef6\u3002<\/li>\n<li>timeout\uff1a-1 \u8868\u793a\u65e0\u9650\u963b\u585e\uff0c0 \u8868\u793a\u975e\u963b\u585e\uff0c\u6b63\u503c\u8868\u793a\u6beb\u79d2\u8d85\u65f6\u3002<\/li>\n<li><strong>\u4f18\u52bf<\/strong>\uff1a\n<ul dir=\"auto\">\n<li>\u65e0\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\u9650\u5236\u3002<\/li>\n<li>\u72ec\u7acb\u7684 events\uff08\u8f93\u5165\uff09\u548c revents\uff08\u8f93\u51fa\uff09\u5b57\u6bb5\u907f\u514d\u91cd\u65b0\u521d\u59cb\u5316\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\n<ul dir=\"auto\">\n<li>\u8d85\u65f6\u7cbe\u5ea6\u8f83\u4f4e\uff08\u6beb\u79d2\uff0c\u76f8\u6bd4 select() \u7684\u5fae\u79d2\uff09\u3002<\/li>\n<li>\u7ebf\u6027\u626b\u63cf\u5bf9\u5927\u63cf\u8ff0\u7b26\u96c6\u7684\u6027\u80fd\u4ecd\u6709\u9650\u5236\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4 dir=\"auto\">epoll()<\/h4>\n<div dir=\"auto\">\n<div>\n<div>c<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>int epoll_create(int size);<\/div>\n<div>int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev);<\/div>\n<div>int epoll_wait(int epfd, struct epoll_event *evlist, int maxevents, int timeout);<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<ul dir=\"auto\">\n<li>Linux \u4e13\u6709\u7684\u9ad8\u6548 I\/O \u591a\u8def\u590d\u7528 API\u3002<\/li>\n<li>\u4f7f\u7528\u7ea2\u9ed1\u6811\u7ba1\u7406\u63cf\u8ff0\u7b26\u548c\u5c31\u7eea\u5217\u8868\u5904\u7406\u6d3b\u8dc3\u63cf\u8ff0\u7b26\u3002<\/li>\n<li>\u652f\u6301<strong>\u6c34\u5e73\u89e6\u53d1\uff08LT\uff09<\/strong>\uff08\u9ed8\u8ba4\uff09\u548c**\u8fb9\u7f18\u89e6\u53d1\uff08ET\uff09**\u6a21\u5f0f\uff1a\n<ul dir=\"auto\">\n<li><strong>LT<\/strong>\uff1a\u53ea\u8981\u63cf\u8ff0\u7b26\u5c31\u7eea\u5c31\u901a\u77e5\u3002<\/li>\n<li><strong>ET<\/strong>\uff1a\u4ec5\u5728\u65b0\u4e8b\u4ef6\u53d1\u751f\u65f6\u901a\u77e5\uff0c\u9700\u4e00\u6b21\u6027\u5904\u7406\u5168\u90e8\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4f18\u52bf<\/strong>\uff1a\n<ul dir=\"auto\">\n<li>\u65e0\u63cf\u8ff0\u7b26\u6570\u91cf\u9650\u5236\uff08\u53d7\u7cfb\u7edf\u5185\u5b58\u9650\u5236\uff09\u3002<\/li>\n<li>\u901a\u8fc7\u7ea2\u9ed1\u6811\u548c\u5c31\u7eea\u5217\u8868\u5b9e\u73b0\u5e38\u6570\u65f6\u95f4\u64cd\u4f5c\u3002<\/li>\n<li>\u4f7f\u7528\u5185\u5b58\u6620\u5c04\uff08mmap\uff09\u63d0\u9ad8\u5185\u6838\u4e0e\u7528\u6237\u7a7a\u95f4\u901a\u4fe1\u6548\u7387\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9650 Linux\uff0c\u964d\u4f4e\u53ef\u79fb\u690d\u6027\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u6027\u80fd\u6bd4\u8f83<\/h3>\n<div dir=\"auto\">\n<div>\n<div><\/div>\n<\/div>\n<table dir=\"auto\" data-wide=\"false\">\n<thead>\n<tr>\n<th data-col-size=\"md\">\u7279\u6027<\/th>\n<th data-col-size=\"md\">select()<\/th>\n<th data-col-size=\"md\">poll()<\/th>\n<th data-col-size=\"lg\">epoll()<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\"><strong>\u63cf\u8ff0\u7b26\u9650\u5236<\/strong><\/td>\n<td data-col-size=\"md\">\u56fa\u5b9a (FD_SETSIZE)<\/td>\n<td data-col-size=\"md\">\u65e0\u9650\u5236<\/td>\n<td data-col-size=\"lg\">\u65e0\u9650\u5236<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\"><strong>\u6548\u7387<\/strong><\/td>\n<td data-col-size=\"md\">\u7ebf\u6027\u626b\u63cf<\/td>\n<td data-col-size=\"md\">\u7ebf\u6027\u626b\u63cf<\/td>\n<td data-col-size=\"lg\">\u5e38\u6570\u65f6\u95f4\uff08\u7ea2\u9ed1\u6811\uff09<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\"><strong>\u8d85\u65f6\u7cbe\u5ea6<\/strong><\/td>\n<td data-col-size=\"md\">\u5fae\u79d2<\/td>\n<td data-col-size=\"md\">\u6beb\u79d2<\/td>\n<td data-col-size=\"lg\">\u6beb\u79d2<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\"><strong>\u53ef\u79fb\u690d\u6027<\/strong><\/td>\n<td data-col-size=\"md\">\u9ad8<\/td>\n<td data-col-size=\"md\">\u9ad8<\/td>\n<td data-col-size=\"lg\">\u4ec5\u9650 Linux<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\"><strong>\u89e6\u53d1\u6a21\u5f0f<\/strong><\/td>\n<td data-col-size=\"md\">\u6c34\u5e73\u89e6\u53d1<\/td>\n<td data-col-size=\"md\">\u6c34\u5e73\u89e6\u53d1<\/td>\n<td data-col-size=\"lg\">LT \u548c ET<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3 dir=\"auto\">\u4f7f\u7528\u573a\u666f<\/h3>\n<p dir=\"auto\">I\/O \u591a\u8def\u590d\u7528\u9002\u7528\u4e8e\u5904\u7406\u591a\u4e2a\u8fde\u63a5\u7684\u670d\u52a1\u5668\uff0c\u5982 Web \u670d\u52a1\u5668\u6216\u6d88\u606f\u7cfb\u7edf\uff0c\u7279\u522b\u662f\u5728 Linux \u73af\u5883\u4e0b\u4f7f\u7528 epoll() \u4ee5\u83b7\u5f97\u9ad8\u6027\u80fd\u3002<\/p>\n<h2 dir=\"auto\">4. \u4fe1\u53f7\u9a71\u52a8\u5f0f I\/O \u6a21\u578b<\/h2>\n<h3 dir=\"auto\">\u5de5\u4f5c\u539f\u7406<\/h3>\n<p dir=\"auto\">\u4fe1\u53f7\u9a71\u52a8\u5f0f I\/O \u901a\u8fc7\u4fe1\u53f7\uff08\u4f8b\u5982 SIGIO\uff09\u901a\u77e5\u8fdb\u7a0b\u6587\u4ef6\u63cf\u8ff0\u7b26\u5df2\u5c31\u7eea\u3002\u8fdb\u7a0b\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\uff0c\u4f7f\u7528 fcntl() \u7684 F_SETOWN \u8bbe\u7f6e\u5957\u63a5\u5b57\u5c5e\u4e3b\uff0c\u5e76\u901a\u8fc7 O_ASYNC \u542f\u7528\u5f02\u6b65 I\/O\u3002<\/p>\n<h3 dir=\"auto\">\u7279\u6027<\/h3>\n<ul dir=\"auto\">\n<li><strong>\u884c\u4e3a<\/strong>\uff1a\u8fdb\u7a0b\u5728\u7b49\u5f85 I\/O \u5c31\u7eea\u4fe1\u53f7\u65f6\u53ef\u7ee7\u7eed\u5176\u4ed6\u4efb\u52a1\u3002<\/li>\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong>\uff1a\u9002\u7528\u4e8e UDP \u5957\u63a5\u5b57\uff0c\u4fe1\u53f7\u8868\u793a\u6570\u636e\u5230\u8fbe\u6216\u9519\u8bef\u3002<\/li>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\n<ul dir=\"auto\">\n<li>\u5bf9 TCP \u5957\u63a5\u5b57\u6548\u679c\u4e0d\u4f73\uff0c\u56e0\u4fe1\u53f7\u9891\u7e41\u4e14\u610f\u4e49\u6a21\u7cca\uff08\u4f8b\u5982\uff0c\u6570\u636e\u5230\u8fbe\u4e0e\u786e\u8ba4\uff09\u3002<\/li>\n<li>\u4fe1\u53f7\u5904\u7406\u590d\u6742\u6027\u589e\u52a0\u7f16\u7a0b\u5f00\u9500\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u5b9e\u73b0\u6b65\u9aa4<\/h3>\n<ol dir=\"auto\">\n<li>\u5b9a\u4e49 SIGIO \u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\u3002<\/li>\n<li>\u4f7f\u7528 fcntl(fd, F_SETOWN, getpid()) \u8bbe\u7f6e\u5957\u63a5\u5b57\u5c5e\u4e3b\u3002<\/li>\n<li>\u4f7f\u7528 fcntl(fd, F_SETFL, O_ASYNC) \u542f\u7528\u5f02\u6b65 I\/O\u3002<\/li>\n<\/ol>\n<h3 dir=\"auto\">\u5c40\u9650\u6027<\/h3>\n<ul dir=\"auto\">\n<li>\u4fe1\u53f7\u7ba1\u7406\u590d\u6742\u6027\u9ad8\u3002<\/li>\n<li>\u9002\u7528\u573a\u666f\u6709\u9650\uff0c\u4e3b\u8981\u7528\u4e8e UDP \u6216\u76d1\u542c TCP \u5957\u63a5\u5b57\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">5. \u5f02\u6b65 I\/O \u6a21\u578b<\/h2>\n<h3 dir=\"auto\">\u5de5\u4f5c\u539f\u7406<\/h3>\n<p dir=\"auto\">\u5f02\u6b65 I\/O\uff08AIO\uff09\u5141\u8bb8\u8fdb\u7a0b\u542f\u52a8 I\/O \u64cd\u4f5c\u540e\u7acb\u5373\u7ee7\u7eed\uff0c\u65e0\u9700\u7b49\u5f85\u64cd\u4f5c\u5b8c\u6210\u3002\u5185\u6838\u5728\u6574\u4e2a\u64cd\u4f5c\uff08\u5305\u62ec\u6570\u636e\u4ece\u5185\u6838\u5230\u7528\u6237\u7a7a\u95f4\u7684\u4f20\u8f93\uff09\u5b8c\u6210\u540e\u901a\u77e5\u8fdb\u7a0b\u3002\u4f7f\u7528 POSIX AIO \u51fd\u6570\u5982 aio_read() \u548c aio_write()\u3002<\/p>\n<h3 dir=\"auto\">\u7279\u6027<\/h3>\n<ul dir=\"auto\">\n<li><strong>\u884c\u4e3a<\/strong>\uff1a\u975e\u963b\u585e\uff0c\u64cd\u4f5c\u5b8c\u5168\u5b8c\u6210\u540e\u901a\u77e5\u3002<\/li>\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong>\uff1a\u9700\u8981\u5e76\u884c\u4efb\u52a1\u6267\u884c\u7684\u9ad8\u6027\u80fd\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<li><strong>\u5b9e\u73b0<\/strong>\uff1a\u4f7f\u7528 aio_read() \u6307\u5b9a\u63cf\u8ff0\u7b26\u3001\u7f13\u51b2\u533a\u548c\u901a\u77e5\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u4e0e\u4fe1\u53f7\u9a71\u52a8\u5f0f I\/O \u7684\u533a\u522b<\/h3>\n<ul dir=\"auto\">\n<li><strong>\u4fe1\u53f7\u9a71\u52a8\u5f0f I\/O<\/strong>\uff1a\u901a\u77e5\u4f55\u65f6\u53ef\u4ee5\u5f00\u59cb I\/O \u64cd\u4f5c\u3002<\/li>\n<li><strong>\u5f02\u6b65 I\/O<\/strong>\uff1a\u901a\u77e5 I\/O \u64cd\u4f5c\u4f55\u65f6\u5b8c\u5168\u5b8c\u6210\uff0c\u5305\u62ec\u6570\u636e\u4f20\u8f93\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u5c40\u9650\u6027<\/h3>\n<ul dir=\"auto\">\n<li>\u8de8\u5e73\u53f0\u652f\u6301\u6709\u9650\u3002<\/li>\n<li>\u76f8\u6bd4\u540c\u6b65\u6a21\u578b\uff0c\u8bbe\u7f6e\u590d\u6742\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">\u9009\u62e9\u5408\u9002\u7684 I\/O \u6a21\u578b<\/h2>\n<h3 dir=\"auto\">\u51b3\u7b56\u56e0\u7d20<\/h3>\n<ul dir=\"auto\">\n<li><strong>\u5e76\u53d1\u9700\u6c42<\/strong>\uff1a\u963b\u585e\u5f0f I\/O \u9002\u5408\u4f4e\u5e76\u53d1\u5e94\u7528\uff0c\u800c I\/O \u591a\u8def\u590d\u7528\u6216\u5f02\u6b65 I\/O \u66f4\u9002\u5408\u9ad8\u5e76\u53d1\u573a\u666f\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1aepoll() \u5728 Linux \u9ad8\u6027\u80fd\u73af\u5883\u4e2d\u8868\u73b0\u4f18\u5f02\uff0c\u800c select() \u548c poll() \u63d0\u4f9b\u66f4\u597d\u7684\u53ef\u79fb\u690d\u6027\u3002<\/li>\n<li><strong>\u590d\u6742\u6027<\/strong>\uff1a\u963b\u585e\u548c\u975e\u963b\u585e I\/O \u5b9e\u73b0\u7b80\u5355\uff0c\u800c\u4fe1\u53f7\u9a71\u52a8\u548c\u5f02\u6b65 I\/O \u9700\u8981\u66f4\u590d\u6742\u5904\u7406\u3002<\/li>\n<li><strong>\u53ef\u79fb\u690d\u6027<\/strong>\uff1a\u4f7f\u7528 select() \u6216 poll() \u786e\u4fdd\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u6216\u4f7f\u7528 libevent \u7b49\u5e93\u62bd\u8c61\u5e73\u53f0\u7279\u5b9a\u673a\u5236\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u63a8\u8350<\/h3>\n<ul dir=\"auto\">\n<li><strong>\u5c0f\u89c4\u6a21\u5e94\u7528<\/strong>\uff1a\u4f7f\u7528\u5e26\u7ebf\u7a0b\u7684\u963b\u585e\u5f0f I\/O\uff0c\u7b80\u5355\u6613\u7528\u3002<\/li>\n<li><strong>\u9ad8\u5e76\u53d1\u670d\u52a1\u5668<\/strong>\uff1a\u5728 Linux \u4e0a\u4f18\u5148\u9009\u62e9 epoll()\uff0c\u6216\u4f7f\u7528 libevent \u786e\u4fdd\u53ef\u79fb\u690d\u6027\u3002<\/li>\n<li><strong>\u5b9e\u65f6\u7cfb\u7edf<\/strong>\uff1a\u5f02\u6b65 I\/O \u6216\u4fe1\u53f7\u9a71\u52a8\u5f0f I\/O \u4ee5\u6700\u5c0f\u5316\u963b\u585e\u3002<\/li>\n<li><strong>\u8de8\u5e73\u53f0\u5f00\u53d1<\/strong>\uff1a\u4f7f\u7528 select() \u6216 poll()\uff0c\u5e76\u63d0\u4f9b\u56de\u9000\u673a\u5236\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">\u89e6\u53d1\u6a21\u5f0f\uff1a\u6c34\u5e73\u89e6\u53d1 vs. \u8fb9\u7f18\u89e6\u53d1<\/h2>\n<h3 dir=\"auto\">\u6c34\u5e73\u89e6\u53d1\uff08LT\uff09<\/h3>\n<ul dir=\"auto\">\n<li>\u53ea\u8981\u63cf\u8ff0\u7b26\u5c31\u7eea\u5c31\u901a\u77e5\u3002<\/li>\n<li>\u5141\u8bb8\u65e0\u9700\u7acb\u5373\u6267\u884c I\/O \u7684\u91cd\u590d\u68c0\u67e5\u3002<\/li>\n<li>\u9002\u5408\u9700\u8981\u7075\u6d3b I\/O \u65f6\u95f4\u7684\u5e94\u7528\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u8fb9\u7f18\u89e6\u53d1\uff08ET\uff09<\/h3>\n<ul dir=\"auto\">\n<li>\u4ec5\u5728\u65b0 I\/O \u4e8b\u4ef6\u53d1\u751f\u65f6\u901a\u77e5\u3002<\/li>\n<li>\u9700\u4e00\u6b21\u6027\u5904\u7406\u5168\u90e8\u6570\u636e\u4ee5\u907f\u514d\u9519\u8fc7\u4e8b\u4ef6\u3002<\/li>\n<li>\u9002\u5408\u9ad8\u6027\u80fd\u573a\u666f\uff0c\u4f46\u9700\u5c06\u63cf\u8ff0\u7b26\u8bbe\u4e3a\u975e\u963b\u585e\u4ee5\u9632\u963b\u585e\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u5bf9\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5f71\u54cd<\/h3>\n<ul dir=\"auto\">\n<li><strong>LT<\/strong>\uff1a\u901a\u8fc7\u5141\u8bb8\u90e8\u5206 I\/O \u5904\u7406\u7b80\u5316\u7f16\u7a0b\u3002<\/li>\n<li><strong>ET<\/strong>\uff1a\u63d0\u9ad8\u6548\u7387\uff0c\u4f46\u9700\u5c0f\u5fc3\u7ba1\u7406\u4ee5\u907f\u514d\u6570\u636e\u9965\u997f\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">\u7ed3\u8bba<\/h2>\n<p dir=\"auto\">\u5728 Linux \u4e2d\u9009\u62e9\u5408\u9002\u7684 I\/O \u6a21\u578b\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u5bf9\u6269\u5c55\u6027\u3001\u6027\u80fd\u548c\u53ef\u79fb\u690d\u6027\u7684\u9700\u6c42\u3002\u963b\u585e\u5f0f I\/O \u7b80\u5355\u4f46\u5728\u9ad8\u5e76\u53d1\u4e0b\u53d7\u9650\u3002\u975e\u963b\u585e\u5f0f I\/O \u907f\u514d\u963b\u585e\u4f46\u8f6e\u8be2\u6548\u7387\u4f4e\u3002I\/O \u591a\u8def\u590d\u7528\uff0c\u7279\u522b\u662f\u5728 Linux \u73af\u5883\u4e0b\u4f7f\u7528 epoll()\uff0c\u63d0\u4f9b\u51fa\u8272\u7684\u6269\u5c55\u6027\u3002\u4fe1\u53f7\u9a71\u52a8\u5f0f I\/O \u9002\u7528\u4e8e UDP \u7b49\u7279\u5b9a\u573a\u666f\uff0c\u800c\u5f02\u6b65 I\/O \u4e3a\u9ad8\u6027\u80fd\u5e94\u7528\u63d0\u4f9b\u6700\u5927\u7075\u6d3b\u6027\u3002\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u6a21\u578b\u53ca\u5176\u6743\u8861\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8bbe\u8ba1\u51fa\u9002\u5408\u9700\u6c42\u7684\u5065\u58ee\u3001\u9ad8\u6548\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Linux \u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u9009\u62e9&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-208388","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208388"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208388\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}