{"rsdb":{"rid":"2605","subhead":"","postdate":"0","aid":"1087","fid":"56","uid":"1","topic":"1","content":" <\/span>         <\/font><\/span>\u5728<\/span> linux<\/span> \u7684\u7f51\u7edc\u7f16\u7a0b<\/a>(www.cppentry.com)\u4e2d\uff0c\u5f88\u957f\u7684\u65f6\u95f4\u90fd\u5728\u4f7f\u7528<\/span> select<\/span> \u6765\u505a\u4e8b\u4ef6\u89e6\u53d1\u3002\u5728<\/span> linux<\/span> \u65b0\u7684\u5185\u6838\u4e2d\uff0c\u6709\u4e86\u4e00\u79cd\u66ff\u6362\u5b83\u7684\u673a\u5236\uff0c\u5c31\u662f<\/span> epoll<\/span> \u3002<\/span><\/span>

    \u76f8\u6bd4\u4e8e<\/span> select<\/span> \uff0c<\/span> epoll<\/span> \u6700\u5927\u7684\u597d\u5904\u5728\u4e8e\u5b83\u4e0d\u4f1a\u968f\u7740\u76d1\u542c<\/span> fd<\/span> \u6570\u76ee\u7684\u589e\u957f\u800c\u964d\u4f4e\u6548\u7387\u3002\u56e0\u4e3a\u5728\u5185\u6838\u4e2d\u7684<\/span> select<\/span> \u5b9e\u73b0\u4e2d\uff0c\u5b83\u662f\u91c7\u7528\u8f6e\u8be2\u6765\u5904\u7406\u7684\uff0c\u8f6e\u8be2\u7684<\/span> fd<\/span> \u6570\u76ee\u8d8a\u591a\uff0c\u81ea\u7136\u8017\u65f6\u8d8a\u591a\u3002\u5e76\u4e14\uff0c\u5728<\/span> linux\/posix_types.h<\/span> \u5934\u6587\u4ef6\u6709\u8fd9\u6837\u7684\u58f0\u660e\uff1a<\/span><\/p>

#define __FD_SETSIZE    <\/span>1024<\/span><\/p>

\u8868\u793a<\/span> select<\/span> \u6700\u591a\u540c\u65f6\u76d1\u542c<\/span> 1024<\/span> \u4e2a<\/span> fd<\/span> \uff0c\u5f53\u7136\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u5934\u6587\u4ef6\u518d\u91cd\u7f16\u8bd1\u5185\u6838\u6765\u6269\u5927\u8fd9\u4e2a\u6570\u76ee\uff0c\u4f46\u8fd9\u4f3c\u4e4e\u5e76\u4e0d\u6cbb\u672c\u3002<\/span><\/p>

 <\/span><\/p>

epoll<\/span> <\/strong>\u7684\u63a5\u53e3\u975e\u5e38\u7b80\u5355\uff0c\u4e00\u5171\u5c31\u4e09\u4e2a\u51fd\u6570\uff1a<\/span><\/strong><\/p>

1. int epoll_create(int size);<\/span><\/strong><\/p>

\u521b\u5efa\u4e00\u4e2a<\/span> epoll<\/span> \u7684\u53e5\u67c4\uff0c<\/span> size<\/span> \u7528\u6765\u544a\u8bc9\u5185\u6838\u8fd9\u4e2a\u76d1\u542c\u7684\u6570\u76ee\u4e00\u5171\u6709\u591a\u5927\u3002\u8fd9\u4e2a\u53c2\u6570\u4e0d\u540c\u4e8e<\/span> select()<\/span> \u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u7ed9\u51fa\u6700\u5927\u76d1\u542c\u7684<\/span> fd+1<\/span> \u7684\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u521b\u5efa\u597d<\/span> epoll<\/span> \u53e5\u67c4\u540e\uff0c\u5b83\u5c31\u662f\u4f1a\u5360\u7528\u4e00\u4e2a<\/span> fd<\/span> \u503c\uff0c\u5728<\/span> linux<\/span> \u4e0b\u5982\u679c\u67e5\u770b<\/span> \/proc\/<\/span> \u8fdb\u7a0b<\/span> id\/fd\/<\/span> \uff0c\u662f\u80fd\u591f\u770b\u5230\u8fd9\u4e2a<\/span> fd<\/span> \u7684\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u5b8c<\/span> epoll<\/span> \u540e\uff0c\u5fc5\u987b\u8c03\u7528<\/span> close()<\/span> \u5173\u95ed\uff0c\u5426\u5219\u53ef\u80fd\u5bfc\u81f4<\/span> fd<\/span> \u88ab\u8017\u5c3d\u3002<\/span><\/p>

 <\/span><\/p>

2. int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);<\/span><\/strong><\/p>

epoll<\/span> \u7684\u4e8b\u4ef6\u6ce8\u518c\u51fd\u6570\uff0c\u5b83\u4e0d\u540c\u4e0e<\/span> select()<\/span> \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<\/span><\/p>

\u7b2c\u4e00\u4e2a<\/span> <\/strong>\u53c2\u6570\u662f<\/span> epoll_create()<\/span> \u7684\u8fd4\u56de\u503c\uff0c<\/span><\/p>

\u7b2c\u4e8c\u4e2a<\/span> <\/strong>\u53c2\u6570\u8868\u793a\u52a8\u4f5c\uff0c\u7528\u4e09\u4e2a\u5b8f\u6765\u8868\u793a\uff1a<\/span><\/p>

EPOLL_CTL_ADD<\/span> \uff1a\u6ce8\u518c\u65b0\u7684<\/span> fd<\/span> \u5230<\/span> epfd<\/span> \u4e2d\uff1b<\/span><\/p>

EPOLL_CTL_MOD<\/span> \uff1a\u4fee\u6539\u5df2\u7ecf\u6ce8\u518c\u7684<\/span> fd<\/span> \u7684\u76d1\u542c\u4e8b\u4ef6\uff1b<\/span><\/p>

EPOLL_CTL_DEL<\/span> \uff1a\u4ece<\/span> epfd<\/span> \u4e2d\u5220\u9664\u4e00\u4e2a<\/span> fd<\/span> \uff1b<\/span><\/p>

\u7b2c\u4e09\u4e2a<\/span> <\/strong>\u53c2\u6570\u662f\u9700\u8981\u76d1\u542c\u7684<\/span> fd<\/span> \uff0c<\/span><\/p>

\u7b2c\u56db\u4e2a<\/span> <\/strong>\u53c2\u6570\u662f\u544a\u8bc9\u5185\u6838\u9700\u8981\u76d1\u542c\u4ec0\u4e48\u4e8b\uff0c<\/span> struct epoll_event<\/span> \u7ed3\u6784\u5982\u4e0b\uff1a<\/span><\/p>

struct epoll_event {<\/span><\/p>

  <\/span>__uint32_t events;  <\/span>\/* Epoll events *\/<\/span><\/p>

  <\/span>epoll_data_t data;  <\/span>\/* User data variable *\/<\/span><\/p>

};<\/span><\/p>

 <\/span><\/p>

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

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

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

EPOLLPRI<\/span> \uff1a<\/span>       <\/span><\/span>\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<\/span><\/p>

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

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

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

EPOLLONESHOT<\/span> \uff1a<\/span> \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<\/span> socket<\/span> \u7684\u8bdd\uff0c\u9700\u8981\u518d\u6b21\u628a\u8fd9\u4e2a<\/span> socket<\/span> \u52a0\u5165\u5230<\/span> EPOLL<\/span> \u961f\u5217\u91cc<\/span><\/p>

 <\/span><\/p>

3. int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);<\/span><\/strong><\/p>

\u7b49\u5f85\u4e8b\u4ef6\u7684\u4ea7\u751f\uff0c\u7c7b\u4f3c\u4e8e<\/span> select()<\/span> \u8c03\u7528\u3002\u53c2\u6570<\/span> events<\/span> \u7528\u6765\u4ece\u5185\u6838\u5f97\u5230\u4e8b\u4ef6\u7684\u96c6\u5408\uff0c<\/span> maxevents<\/span> \u544a\u4e4b\u5185\u6838\u8fd9\u4e2a<\/span>events<\/span> \u6709\u591a\u5927\uff0c\u8fd9\u4e2a<\/span> maxevents<\/span> \u7684\u503c\u4e0d\u80fd\u5927\u4e8e\u521b\u5efa<\/span> epoll_create()<\/span> \u65f6\u7684<\/span> size<\/span> \uff0c\u53c2\u6570<\/span> timeout<\/span> \u662f\u8d85\u65f6\u65f6\u95f4\uff08\u6beb\u79d2\uff0c<\/span> 0<\/span> \u4f1a\u7acb\u5373\u8fd4\u56de\uff0c<\/span> -1<\/span> \u5c06\u4e0d\u786e\u5b9a\uff0c\u4e5f\u6709\u8bf4\u6cd5\u8bf4\u662f\u6c38\u4e45\u963b\u585e\uff09\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u9700\u8981\u5904\u7406\u7684\u4e8b\u4ef6\u6570\u76ee\uff0c\u5982\u8fd4\u56de<\/span> 0<\/span> \u8868\u793a\u5df2\u8d85\u65f6\u3002<\/span><\/p>

 <\/span><\/p>

 <\/span><\/p>

\u4ece<\/span> man<\/span> \u624b\u518c\u4e2d\uff0c\u5f97\u5230<\/span> ET<\/span> \u548c<\/span> LT<\/span> \u7684\u5177\u4f53\u63cf\u8ff0\u5982\u4e0b<\/span><\/p>

 <\/span><\/p>

EPOLL<\/span> <\/strong>\u4e8b\u4ef6\u6709\u4e24\u79cd\u6a21\u578b\uff1a<\/span><\/strong><\/p>

Edge Triggered (ET)  <\/span><\/span><\/strong>\u8fb9\u7f18\u89e6\u53d1<\/span> <\/span>\u53ea\u6709\u6570\u636e\u5230\u6765\uff0c\u624d\u89e6\u53d1\uff0c\u4e0d\u7ba1\u7f13\u5b58\u533a\u4e2d\u662f\u5426\u8fd8\u6709\u6570\u636e\u3002<\/span><\/strong><\/strong>

Level Triggered (LT)  <\/span><\/span><\/strong>\u6c34\u5e73\u89e6\u53d1<\/span> <\/span>\u53ea\u8981\u6709\u6570\u636e\u90fd\u4f1a\u89e6\u53d1\u3002<\/span><\/strong><\/strong>

 <\/span><\/p>

\u5047\u5982\u6709\u8fd9\u6837\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/span><\/p>

1. <\/span>\u6211\u4eec\u5df2\u7ecf\u628a\u4e00\u4e2a\u7528\u6765\u4ece\u7ba1\u9053\u4e2d\u8bfb\u53d6\u6570\u636e\u7684\u6587\u4ef6\u53e5\u67c4<\/span> (RFD)<\/span> \u6dfb\u52a0\u5230<\/span> epoll<\/span> \u63cf\u8ff0\u7b26<\/span><\/p>

2. <\/span>\u8fd9\u4e2a\u65f6\u5019\u4ece\u7ba1\u9053\u7684\u53e6\u4e00\u7aef\u88ab\u5199\u5165\u4e86<\/span> 2KB<\/span> \u7684\u6570\u636e<\/span><\/p>

3. <\/span>\u8c03\u7528<\/span> epoll_wait(2)<\/span> \uff0c\u5e76\u4e14\u5b83\u4f1a\u8fd4\u56de<\/span> RFD<\/span> \uff0c\u8bf4\u660e\u5b83\u5df2\u7ecf\u51c6\u5907\u597d\u8bfb\u53d6\u64cd\u4f5c<\/span><\/p>

4. <\/span>\u7136\u540e\u6211\u4eec\u8bfb\u53d6\u4e86<\/span> 1KB<\/span> \u7684\u6570\u636e<\/span><\/p>

5. ","orderid":"0","title":"Epoll\u6a21\u578b\u8be6\u89e3(\u4e00)","smalltitle":"","mid":"0","fname":"\u7f51\u7edc\u7f16\u7a0b","special_id":"0","bak_id":"0","info":"0","hits":"14270","pages":"13","comments":"0","posttime":"2011-12-14 13:02:01","list":"1323838921","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"http:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Epoll<\/A> \u6a21\u578b<\/A> \u8be6\u89e3<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"121.14.96.125","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":" \u5728linux\u7684\u7f51\u7edc\u7f16\u7a0b(www.cppentry.com)\u4e2d\uff0c\u5f88\u957f\u7684\u65f6\u95f4\u90fd\u5728\u4f7f\u7528select\u6765\u505a\u4e8b\u4ef6\u89e6\u53d1\u3002\u5728linux\u65b0\u7684\u5185\u6838\u4e2d\uff0c\u6709\u4e86\u4e00\u79cd\u66ff\u6362\u5b83\u7684\u673a\u5236\uff0c\u5c31\u662fepoll\u3002 \u76f8\u6bd4\u4e8eselect\uff0cepoll\u6700\u5927\u7684\u597d\u5904\u5728\u4e8e\u5b83\u4e0d\u4f1a\u968f\u7740\u76d1\u542cfd\u6570\u76ee\u7684\u589e\u957f\u800c\u964d\u4f4e\u6548\u7387\u3002\u56e0\u4e3a\u5728\u5185\u6838\u4e2d\u7684select\u5b9e\u73b0\u4e2d\uff0c\u5b83\u662f\u91c7\u7528\u8f6e\u8be2..","lastview":"1487684028","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"|3~~\u6b20\u6241|1","htmlname":"","city_id":"0"},"page":"1"}