{"id":291398,"date":"2024-05-04T21:21:54","date_gmt":"2024-05-04T13:21:54","guid":{"rendered":"https:\/\/lrxjmw.cn\/txlhjxdcybij.html"},"modified":"2024-05-04T21:21:54","modified_gmt":"2024-05-04T13:21:54","slug":"txlhjxdcybij","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/txlhjxdcybij.html","title":{"rendered":"\u63a2\u6790Linux\u73af\u5883\u4e0b\u7684C\/C++\u5f02\u6b65IO\u6280\u672f\uff1a\u63d0\u5347\u7cfb\u7edf\u6027\u80fd\u4e0e\u4e8b\u52a1\u5904\u7406\u80fd\u529b"},"content":{"rendered":"
\u672c\u7bc7\u6587\u7ae0\u91cd\u70b9\u5206\u6790\u4e86Linux<\/a>\u73af\u5883\u4e0b\u7684C\/C++\u5f02\u6b65IO\u6280\u672f\u7684\u57fa\u672c\u539f\u7406\u548c\u91cd\u8981\u4f5c\u7528\u3002\u8fd9\u79cd\u6280\u672f\u4e3b\u8981\u63d0\u5347\u4e86\u7cfb\u7edf<\/a>\u6027\u80fd\u5e76\u6709\u6548\u63d0\u9ad8\u4e86\u4e8b\u52a1\u5904\u7406\u80fd\u529b\u3002\u7ecf\u8fc7\u6df1\u5165\u63a2\u8ba8\uff0c\u6211\u4eec\u5e0c\u671b\u5e7f\u5927\u516c\u5171\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u5f02\u6b65IO\u6280\u672f\u7684\u7cbe\u9ad3\u3002<\/p>\n \u4e00\u3001\u5f02\u6b65IO\u6982\u8ff0<\/b><\/p>\n \u5f02\u6b65IO\uff0c\u5373\u4e0d\u5fc5\u7b49\u5f85IO\u5b8c\u6210\u4fbf\u53ef\u7ee7\u7eed\u6267\u884c\u7684\u6280\u672f\uff0c\u76f8\u8f83\u4e8e\u540c\u6b65IO,\u80fd\u591f\u66f4\u52a0\u6709\u6548\u5730\u5229\u7528\u7cfb\u7edf\u8d44\u6e90\uff0c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u4e0e\u8fd0\u884c\u6548\u7387\u3002\u5728Linux\u73af\u5883\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528`epoll`\u6216`aio`\u7b49\u5185\u6838API\u4ee5\u53ca\u76f8\u5e94\u7684C\/C++\u5e93\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5f02\u6b65IO\u3002<\/p>\n \u5f02\u6b65IO\u7684\u6838\u5fc3\u7406\u5ff5\u662f\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u7b49\u65b9\u5f0f\uff0c\u5728I\/O\u64cd\u4f5c\u7ed3\u675f\u540e\u4e0d\u7ecf\u963b\u585e\u5373\u53ef\u542f\u52a8\u540e\u7eed\u7a0b\u5e8f\u6d41\u7a0b\uff0c\u4ece\u800c\u6700\u5927\u9650\u5ea6\u5730\u5229\u7528\u5904\u7406\u5668\u8d44\u6e90\u5e76\u63d0\u5347\u6574\u4f53\u7cfb\u7edf\u7684\u8fd0\u884c\u6548\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n \u4e8c\u3001Linux\u4e0b\u7684\u5f02\u6b65IO\u6a21\u578b<\/b><\/p>\n Linux\u5185\u6838\u4e3a\u7528\u6237\u63d0\u4f9b\u591a\u6837\u7684\u5f02\u6b65IO\u6a21\u5f0f\uff0c\u5305\u62ec\u4e8b\u4ef6\u9a71\u52a8\u548c\u4fe1\u53f7\u9a71\u52a8\u4e24\u79cd\u5178\u578b\u3002\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u6784\u5efa\u4e8e\u4e8b\u4ef6\u73af\u8def\uff0c\u4ee5`epoll`\u4e3a\u4ee3\u8868\uff1b\u800c\u4fe1\u53f7\u9a71\u52a8\u5219\u901a\u8fc7\u4fe1\u53f7\u901a\u77e5\u5b8c\u6210IO\u64cd\u4f5c\uff0c\u5176\u4e2d`aio`\u662f\u5178\u578b\u793a\u4f8b\u3002<\/p>\n 1.\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b<\/b><\/p>\n \u91c7\u7528\u4e8b\u4ef6\u5faa\u73af\u673a\u5236\u4e0e\u5b9e\u65f6\u76d1\u63a7\u6587\u4ef6\u63cf\u8ff0\u7b26IO\u4e8b\u4ef6\uff0c\u53ef\u5b9e\u73b0\u5728Linux\u7cfb\u7edf\u73af\u5883\u4e0b\u7684\u5f02\u6b65IO\u529f\u80fd\u3002\u5176\u4e2d\uff0c\u4e1a\u754c\u5e7f\u6cdb\u8fd0\u7528\u7684\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u5373\u4e3a`epoll`linux\u7cfb\u7edf\u65e5\u5fd7\uff0c\u51ed\u501f\u5353\u8d8a\u7684IO\u591a\u8def\u590d\u7528\u6548\u80fd\uff0c\u53ef\u540c\u65f6\u76d1\u6d4b\u5e76\u53ca\u65f6\u56de\u5e94\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684IO\u72b6\u6001\u53d8\u5316\u3002<\/p>\n \"EPoll\"\u4e3b\u8981\u4ee5\"epoll_create\"\u5b89\u88c5epoll\u5b9e\u4f53\u8bbe\u65bd\uff0c\u63a5\u7740\u8fd0\u7528\"epoll_ctl\"\u6765\u5339\u914d\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0e\u76f8\u5173\u4e8b\u4ef6\u4ec0\u4e48\u662flinux\uff0c\u501f\u52a9\"epoll_wait\"\u7cfb\u7edf\u6307\u4ee4\u5b9e\u73b0\u5bf9I\/O\u6d3b\u52a8\u7684\u76d1\u63a7\u548c\u53cd\u5e94\u3002\u4f9d\u636e\u4e8b\u4ef6\u9a71\u52a8\u673a\u5236\u8bbe\u8ba1\u7684\"EPol\"\uff0c\u80fd\u591f\u5e94\u5bf9\u5927\u89c4\u6a21\u5e76\u53d1\u8fde\u63a5linux c\/c++\u5f02\u6b65io<\/strong>\uff0c\u662f\u6784\u5efa\u9ad8\u6548\u7f51\u7edc\u670d\u52a1\u7684\u4f18\u9009\u7b56\u7565\u4e4b\u4e00\u3002<\/p>\n 2.\u4fe1\u53f7\u9a71\u52a8\u6a21\u578b<\/b><\/p>\n \u4fe1\u53f7\u9a71\u52a8\u6a21\u578b\u5229\u7528\u4fe1\u53f7\u4f20\u9012\u673a\u5236\u63a7\u5236IO\u64cd\u4f5c\u7684\u5f00\u59cb\u548c\u7ed3\u675f\uff0c\u5176\u4e2d\u5178\u578b\u4ee3\u8868\u662f\u5f02\u6b65IO\u5e93\u51fd\u6570`aio`\u3002\u7528\u6237\u53ef\u4f7f\u7528\u5982`aio_read`\u3001`aio_write`\u7b49\u51fd\u6570\u542f\u52a8\u5f02\u6b65IO\u64cd\u4f5c\uff0c\u540c\u65f6\u501f\u52a9\u4fe1\u53f7`SIGIO`\u6765\u83b7\u53d6\u64cd\u4f5c\u72b6\u6001\u7684\u53cd\u9988\u4fe1\u606f\u3002<\/p>\n \u867d\u7136`aio`\u6a21\u5f0f\u5728\u5b9e\u73b0\u5f02\u6b65\u64cd\u4f5c\u4e0a\u8868\u73b0\u5353\u8d8a\uff0c\u4f46\u5728\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u4e0e\u6587\u4ef6\u7cfb\u7edf\u652f\u6301\u4e0a\u4ecd\u6709\u4e0d\u8db3\u4e4b\u5904\u3002\u5728\u5b9e\u8df5\u5e94\u7528\u4e2d\uff0c\u9700\u6839\u636e\u5b9e\u9645\u9700\u6c42\u5ba1\u614e\u9009\u53d6\u5408\u9002\u7684IO\u6a21\u578b\u3002<\/p>\n \u4e09\u3001C\/C++\u4e2d\u7684\u5f02\u6b65IO\u7f16\u7a0b<\/b><\/p>\n \u672c\u7bc7\u6587\u7ae0\u7740\u91cd\u89e3\u6790Linux\u73af\u5883\u4e0b\u5f02\u6b65I\/O\u6280\u672f\u5e26\u6765\u7684\u4f18\u52bf(\u9ad8\u6548\u6027\u4e0e\u9ad8\u5e76\u53d1\u6027)\uff0c\u4ee5\u53ca\u5728C\/C++\u7f16\u7a0b\u4e2d\u7684\u5177\u4f53\u5b9e\u8df5\u4e0e\u7f16\u7801\u65b9\u5f0f\uff0c\u52a9\u529b\u76f8\u5173\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002<\/p>\n 1.\u5229\u7528epoll\u5b9e\u73b0\u7f51\u7edc\u670d\u52a1\u5668<\/b><\/p>\n Epoll\u4f5c\u4e3aLinux\u7cfb\u7edf\u9ad8\u6548\u7f51\u7edc\u7f16\u7a0b\u4e4b\u5229\u5668\uff0c\u52a9\u5efa\u5e76\u53d1\u6027\u5f3a\u4e4b\u7f51\u7edc\u670d\u52a1\u3002\u9996\u5148\uff0c\u521b\u5efaEpoll\u5bf9\u8c61\u5e76\u8bbe\u7f6e\u9700\u76d1\u63a7\u4e4b\u6587\u4ef6\u63cf\u8ff0\u7b26\uff1b\u5176\u6b21linux c\/c++\u5f02\u6b65io<\/strong>\uff0c\u4e8e\u4e8b\u4ef6\u5faa\u73af\u4e2d\u7b49\u5f85IO\u4e8b\u4ef6\u54cd\u94c3\uff1b\u6700\u540e\uff0c\u5229\u7528\u5468\u906d\u56de\u8c03\u51fd\u6570\u59a5\u5584\u5904\u7406IO\u4e8b\u4ef6\u3002<\/p>\n \n 2.\u5229\u7528aio\u5b9e\u73b0\u5f02\u6b65\u6587\u4ef6IO<\/b><\/p>\n \u5728C\/C++\u7684\u7f16\u7a0b\u73af\u5883\u4e2d\uff0c\u901a\u8fc7\u8fd0\u7528\u5f02\u6b65IO\u5e93\u51fd\u6570aio\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u6587\u4ef6I\/O\u529f\u80fd\u3002\u4ee5aio_read\u548caio_write\u4e3a\u4f8b\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u80fd\u591f\u542f\u52a8\u5f02\u6b65IO\u64cd\u4f5c\uff0c\u540c\u65f6\u501f\u7531SIGIO\u4fe1\u53f7\u6765\u8ffd\u8e2aIO\u64cd\u4f5c\u72b6\u6001\u7684\u53d8\u5316\u60c5\u51b5\u3002<\/p>\n <\/p>\ncpp\n#include<sys\/epoll.h>\nintmain(){\n\u5b9a\u4e49\u4e00\u4e2a\u6574\u578b\u53d8\u91cfepoll_fd\u5e76\u5c06\u5176\u8d4b\u503c\u4e3a\u901a\u8fc7\u8c03\u7528epoll_create\u6240\u521b\u5efa\u4e14\u521d\u59cb\u5927\u5c0f\u8bbe\u5b9a\u4e3a1024\u7684epoll\u5bf9\u8c61\u3002\n\/\/\u6ce8\u518c\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\nstructepoll_eventev;\n\u5c06ev.events\u914d\u7f6e\u4e3aEPOLLIN\u4e0eEPOLLET\u7ed3\u5408\u5f62\u5f0f\uff0c\u5b9e\u65f6\u76d1\u6d4b\u8bfb\u53d6\u4e8b\u4ef6\u5e76\u542f\u52a8\u8fb9\u7f18\u89e6\u53d1\u6a21\u5f0f\u3002\nev.data.fd=listen_fd;\n\u5728\u8fdb\u7a0b\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u5c06listen_fd\u6dfb\u52a0\u81f3\u540d\u4e3aev\u7684epoll\u5b9e\u4f8b\u7684\u76d1\u542c\u961f\u5217\u4e4b\u4e2d\u3002\n\/\/\u4e8b\u4ef6\u5faa\u73af\n\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\"epoll_event\"\u7684\u5927\u5c0f\u4e3a1024\u7684\u6570\u7ec4\u3002\nwhile(true){\n\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u8fd0\u7528epoll_wait\u51fd\u6570\uff0c\u5bf9\u6587\u4ef6\u63cf\u8ff0\u7b26epoll_fd\u8fdb\u884c\u914d\u7f6e\uff0c\u8bbe\u5b9a\u5176\u76f8\u5e94\u7684\u4e8b\u4ef6\u53c2\u6570\u4e3aevents\uff08\u5305\u542b1024\u4e2a\u5143\u7d20\uff09\uff0c\u540c\u65f6\u5c06\u5f85\u5904\u7406\u7684\u4e8b\u4ef6\u6570\u91cf\u8bbe\u4e3a-1\uff0c\u4ee5\u4fbf\u7b49\u5f85\u6240\u6709\u53ef\u80fd\u7684\u4e8b\u4ef6\u5b8c\u6210\u540e\u518d\u8fd4\u56de\u3002\u6700\u540e\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u5c06\u88ab\u8d4b\u503c\u5230\u53d8\u91cfnfds\u4e4b\u4e2d\u3002\n\u5faa\u73af\u8ba1\u6570\u65b9\u5f0f\u5728\u6b64\u5448\u73b0\uff0c\u76f4\u81f3\u62b5\u8fbe\u53d8\u91cf'nfds'\u8bbe\u5b9a\u7684\u503c\u3002\u6bcf\u6b21\u72ec\u7acb\u6d41\u7a0b\u7684\u6210\u529f\u6267\u884c\u90fd\u5c06\u63a8\u52a8\u53d8\u91cf'i'\u5411\u540e\u9012\u589e\uff0c\u56e0\u6b64\uff0c'++i'\u7684\u8fd0\u7b97\u7b26\u4f1a\u88ab\u91cd\u590d\u6267\u884cnfds\u6b21\u3002\n\u82e5\u4f20\u5165\u4e8b\u4ef6[i]\u7684FD\u503c\u4e0e\u76d1\u542c\u7aef\u53e3\u76f8\u5339\u914d\uff0c\u6267\u884c\u76f8\u5e94\u64cd\u4f5c\u3002\n\/\/\u5904\u7406\u65b0\u8fde\u63a5\u4e8b\u4ef6\naccept_connection(listen_fd);\n}else{\n\/\/\u5904\u7406\u5176\u4ed6IO\u4e8b\u4ef6\n\u5904\u7406\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u4e8b\u4ef6\u7f16\u53f7i\u7684\u6570\u636e\u4e8b\u4ef6\u3002\n}\n}\n}\nreturn0;\n}\n<\/pre>\n<\/p>\n