{"id":38969,"date":"2023-10-03T06:06:09","date_gmt":"2023-10-02T22:06:09","guid":{"rendered":"https:\/\/www.techphant.cn\/?p=38969"},"modified":"2023-10-03T06:06:09","modified_gmt":"2023-10-02T22:06:09","slug":"phsyzgxdwlimx","status":"publish","type":"post","link":"https:\/\/www.techphant.cn\/blog\/38969.html","title":{"rendered":"Poll\u51fd\u6570\uff1a\u4e00\u79cd\u9ad8\u6548\u7684\u7f51\u7edcI\/O\u6a21\u578b"},"content":{"rendered":"

  Poll\u51fd\u6570\u662f\u4e00\u79cd\u666e\u904d\u5b58\u5728\u7684\u7f51\u7edcI\/O\u6a21\u578b\uff0c\u5b83\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5927\u91cf\u7684I\/O\u4e8b\u4ef6\uff0c\u4f7f\u7f51\u7edc\u7a0b\u5e8f\u7684\u6548\u7387\u5f97\u5230\u6781\u5927\u7684\u63d0\u5347\u3002\u672c\u6587\u5c06\u4ecb\u7ecdpoll\u51fd\u6570\u7684\u7528\u6cd5\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u5b83\u3002<\/p>\n

poll\u51fd\u6570\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n

  Poll\u51fd\u6570\u662f\u4e00\u79cd\u9ad8\u6548\u7684I\/O\u591a\u8def\u590d\u7528\u6280\u672f\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u76d1\u63a7\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4ee5\u4fbf\u68c0\u6d4b\u5176\u4e2d\u662f\u5426\u6709I\/O\u64cd\u4f5c\u53ef\u4ee5\u8fdb\u884c\uff0c\u4ece\u800c\u8282\u7701\u7cfb\u7edf\u8d44\u6e90\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/p>\n

  Poll\u51fd\u6570\u7684\u57fa\u672c\u539f\u7406\u662f\uff1a\u9996\u5148\uff0c\u7a0b\u5e8f\u5c06\u8981\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u653e\u5165\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u7136\u540e\u8c03\u7528poll\u51fd\u6570\uff0cpoll\u51fd\u6570\u5c06\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u8fdb\u884c\u68c0\u6d4b\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u8fdb\u884cI\/O\u64cd\u4f5c\uff0c\u5219\u8fd4\u56de\u5176\u5728\u6570\u7ec4\u4e2d\u7684\u4e0b\u6807\uff0c\u5426\u5219\uff0cpoll\u51fd\u6570\u5c06\u4f1a\u4e00\u76f4\u7b49\u5f85\uff0c\u76f4\u5230\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u8fdb\u884cI\/O\u64cd\u4f5c\u3002<\/p>\n

poll\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n

  poll\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u6570\u7ec4\uff0c\u5c06\u8981\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u653e\u5165\u6570\u7ec4\u4e2d\uff0c\u7136\u540e\u8c03\u7528poll\u51fd\u6570\uff0cpoll\u51fd\u6570\u5c06\u4f1a\u8fd4\u56de\u53ef\u4ee5\u8fdb\u884cI\/O\u64cd\u4f5c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e0b\u6807\u3002<\/p>\n

  poll\u51fd\u6570\u7684\u5b9e\u4f8b\u4ee3\u7801<\/h3>\n

  \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528poll\u51fd\u6570\u7684\u5b9e\u4f8b\u4ee3\u7801\uff1a<\/p>\n

  #include <\/p>

  int main()<\/p>

  {<\/p>

   struct pollfd fds[2];<\/p>

   int timeout_msecs = 1000;<\/p>

   int retval;<\/p>

   \/* watch stdin for input *\/<\/p>

   fds[0].fd = STDIN_FILENO;<\/p>

   fds[0].events = POLLIN;<\/p>

   \/* watch stdout for ability to write (almost always true) *\/<\/p>

   fds[1].fd = STDOUT_FILENO;<\/p>

   fds[1].events = POLLOUT;<\/p>

   retval = poll(fds, 2, timeout_msecs);<\/p>

   if (retval == -1) {<\/p>

  perror(\"poll\");<\/p>

  return 1;<\/p>

   }<\/p>

   if (fds[0].revents & POLLIN)<\/p>

  printf(\"stdin is readablen\");<\/p>

   if (fds[1].revents & POLLOUT)<\/p>

  printf(\"stdout is writablen\");<\/p>

   return 0;<\/p>

  }<\/p><\/pre>\n

  \u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528poll\u51fd\u6570\u6765\u76d1\u63a7\u6807\u51c6\u8f93\u5165\u548c\u6807\u51c6\u8f93\u51fa\uff0c\u5982\u679c\u53ef\u4ee5\u8fdb\u884cI\/O\u64cd\u4f5c\uff0c\u5219\u4f1a\u6253\u5370\u76f8\u5e94\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n

\u603b\u7ed3<\/h2>\n

  \u672c\u6587\u4ecb\u7ecd\u4e86poll\u51fd\u6570\u7684\u57fa\u672c\u6982\u5ff5\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u5b83\u662f\u4e00\u79cd\u9ad8\u6548\u7684I\/O\u591a\u8def\u590d\u7528\u6280\u672f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5927\u91cf\u7684I\/O\u4e8b\u4ef6\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/strong>\u4f7f\u7528poll\u51fd\u6570\u53ea\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u6570\u7ec4\uff0c\u5c06\u8981\u76d1\u63a7\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u653e\u5165\u6570\u7ec4\u4e2d\uff0c\u7136\u540e\u8c03\u7528poll\u51fd\u6570\uff0cpoll\u51fd\u6570\u5c06\u4f1a\u8fd4\u56de\u53ef\u4ee5\u8fdb\u884cI\/O\u64cd\u4f5c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e0b\u6807\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Poll\u51fd\u6570\u662f\u4e00\u79cd\u666e\u904d\u5b58\u5728\u7684\u7f51\u7edcI\/O\u6a21\u578b\uff0c\u5b83\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5927\u91cf\u7684I\/O\u4e8b\u4ef6\uff0c\u4f7f\u7f51\u7edc\u7a0b\u5e8f\u7684\u6548\u7387\u5f97\u5230\u6781\u5927\u7684\u63d0\u5347\u3002\u672c\u6587\u5c06\u4ecb\u7ecdpoll\u51fd\u6570\u7684\u7528\u6cd5\uff0c\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u5b83\u3002<\/p>\n","protected":false},"author":1,"featured_media":4132,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[36],"tags":[305],"class_list":["post-38969","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-dzjs"],"_links":{"self":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/38969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/comments?post=38969"}],"version-history":[{"count":0,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/38969\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media\/4132"}],"wp:attachment":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media?parent=38969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/categories?post=38969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/tags?post=38969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}