{"id":1942,"date":"2019-09-19T08:01:43","date_gmt":"2019-09-19T00:01:43","guid":{"rendered":"https:\/\/coderbee.net\/?p=1942"},"modified":"2019-09-18T22:39:30","modified_gmt":"2019-09-18T14:39:30","slug":"linux-select-poll-epoll-%e5%8e%9f%e7%90%86%ef%bc%88%e4%b8%80%ef%bc%89%e5%ae%9e%e7%8e%b0%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/linux\/20190919\/1942","title":{"rendered":"Linux select\/poll\/epoll \u539f\u7406\uff08\u4e00\uff09\u5b9e\u73b0\u57fa\u7840"},"content":{"rendered":"<p>\u672c\u5e8f\u5217\u6d89\u53ca\u7684 Linux \u6e90\u7801\u90fd\u662f\u57fa\u4e8e linux-4.14.143 \u3002<\/p>\n<h1>1. \u6587\u4ef6\u62bd\u8c61 \u4e0e poll \u64cd\u4f5c<\/h1>\n<h2>1.1 \u6587\u4ef6\u62bd\u8c61<\/h2>\n<p>\u5728 Linux \u5185\u6838\u91cc\uff0c\u6587\u4ef6\u662f\u4e00\u4e2a\u62bd\u8c61\uff0c\u8bbe\u5907\u662f\u4e2a\u6587\u4ef6\uff0c\u7f51\u7edc\u5957\u63a5\u5b57\u4e5f\u662f\u4e2a\u6587\u4ef6\u3002<\/p>\n<p>\u6587\u4ef6\u62bd\u8c61\u5fc5\u987b\u652f\u6301\u7684\u80fd\u529b\u5b9a\u4e49\u5728 file_operations \u7ed3\u6784\u4f53\u91cc\u3002<\/p>\n<p>\u5728 Linux \u91cc\uff0c\u4e00\u4e2a\u6253\u5f00\u7684\u6587\u4ef6\u5bf9\u5e94\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26 file descriptor\/FD\uff0cFD \u5176\u5b9e\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u5185\u6838\u628a\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u7ef4\u62a4\u5728\u4e00\u4e2a\u6570\u7ec4\u91cc\uff0cFD \u5bf9\u5e94\u7684\u662f\u6570\u7ec4\u7684\u4e0b\u6807\u3002<\/p>\n<p>\u6587\u4ef6\u62bd\u8c61\u7684\u80fd\u529b\u5b9a\u4e49\uff1a<\/p>\n<pre><code class=\"c\">\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1ainclude\/linux\/fs.h\nstruct file_operations {\n    struct module *owner;\n    loff_t (*llseek) (struct file *, loff_t, int);\n    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);\n    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);\n    ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);\n    ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);\n    int (*iterate) (struct file *, struct dir_context *);\n    int (*iterate_shared) (struct file *, struct dir_context *);\n\n    \/\/ \u5bf9\u4e8e select\/poll\/epoll \u6700\u91cd\u8981\u7684\u5b9e\u73b0\u57fa\u7840\n    \/\/ \u975e\u963b\u585e\u7684\u8f6e\u8be2\u6587\u4ef6\u72b6\u6001\u7684\u51fd\u6570\n    unsigned int (*poll) (struct file *, struct poll_table_struct *);\n\n    \/\/ \u7701\u7565\u5176\u4ed6\u51fd\u6570\u6307\u9488\n} __randomize_layout;\n\n\n\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1ainclude\/linux\/poll.h\ntypedef struct poll_table_struct {\n    \/\/ \u6587\u4ef6\u7684 file_operations.poll \u5b9e\u73b0\u4e00\u5b9a\u4f1a\u8c03\u7528\u7684\u961f\u5217\u5904\u7406\u51fd\u6570\n    poll_queue_proc _qproc;\n\n    \/\/ poll \u64cd\u4f5c\u6562\u5174\u8da3\u7684\u4e8b\u4ef6\n    unsigned long _key;\n} poll_table;\n\n\/\/ poll \u961f\u5217\u5904\u7406\u51fd\u6570\ntypedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct poll_table_struct *);\n<\/code><\/pre>\n<p><!--more--><\/p>\n<h2>1.2 \u6587\u4ef6 poll \u64cd\u4f5c<\/h2>\n<p>poll \u51fd\u6570\u7684\u539f\u578b\uff1a<\/p>\n<pre><code class=\"c\">unsigned int (*poll) (struct file *, poll_table *);\n\n\/**\n * \u5982\u679c poll_table \u6709\u56de\u8c03\u51fd\u6570\uff0c\u5219\u56de\u8c03\u5b83\u3002\n * \n * @filp \u8981\u76d1\u542c\u7684\u76ee\u6807\u6587\u4ef6\n * @wait_address \u8981\u76d1\u542c\u4e8b\u4ef6\u7684\u7b49\u5f85\u961f\u5217\u5934\n * @p select\/poll\/epoll \u8c03\u7528\u91cc\u4f20\u5165\u91cc\u7684\u7b49\u5f85\u8282\u70b9\n *\/\nstatic inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)\n{\n    if (p &amp;&amp; p-&gt;_qproc &amp;&amp; wait_address)\n        p-&gt;_qproc(filp, wait_address, p);\n}\n<\/code><\/pre>\n<p>\u6587\u4ef6\u62bd\u8c61 poll \u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\u5fc5\u987b\u5b8c\u6210\u4e24\u4ef6\u4e8b\uff08\u8fd9\u4e24\u70b9\u7b97\u662f\u89c4\u8303\u4e86\uff09\uff1a<br \/>\n1. \u5728 poll \u51fd\u6570\u6562\u5174\u8da3\u7684\u7b49\u5f85\u961f\u5217\u4e0a\u8c03\u7528 <code>poll_wait<\/code> \u51fd\u6570\uff0c\u4ee5\u63a5\u6536\u5230\u5524\u9192\uff1b\u5177\u4f53\u7684\u5b9e\u73b0\u5fc5\u987b\u628a <code>poll_table<\/code> \u7c7b\u578b\u7684\u53c2\u6570\u4f5c\u4e3a\u900f\u660e\u5bf9\u8c61\u6765\u4f7f\u7528\uff0c\u4e0d\u9700\u8981\u77e5\u9053\u5b83\u7684\u5177\u4f53\u7ed3\u6784\u3002<br \/>\n2. \u8fd4\u56de\u6bd4\u7279\u63a9\u7801\uff0c\u8868\u793a\u5f53\u524d\u53ef\u7acb\u5373\u6267\u884c\u800c\u4e0d\u4f1a\u963b\u585e\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u67d0\u4e2a\u9a71\u52a8\u7684 poll \u5b9e\u73b0\u793a\u4f8b\uff0c\u6765\u81ea\uff1a<a href=\"https:\/\/www.oreilly.com\/library\/view\/linux-device-drivers\/0596000081\/ch05s03.html\">https:\/\/www.oreilly.com\/library\/view\/linux-device-drivers\/0596000081\/ch05s03.html<\/a>\uff1a<\/p>\n<pre><code class=\"c\">unsigned int scull_p_poll(struct file *filp, poll_table *wait)\n{\n  Scull_Pipe *dev = filp-&gt;private_data;\n  unsigned int mask = 0;\n\n  \/*\n   * The buffer is circular; it is considered full\n   * if \"wp\" is right behind \"rp\". \"left\" is 0 if the\n   * buffer is empty, and it is \"1\" if it is completely full.\n   *\/\n  int left = (dev-&gt;rp + dev-&gt;buffersize - dev-&gt;wp) % dev-&gt;buffersize;\n\n  \/\/ \u5728\u4e0d\u540c\u7684\u7b49\u5f85\u961f\u5217\u4e0a\u8c03\u7528 poll_wait \u51fd\u6570\n  poll_wait(filp, &amp;dev-&gt;inq, wait);\n  poll_wait(filp, &amp;dev-&gt;outq, wait);\n\n  \/* readable *\/\n  if (dev-&gt;rp != dev-&gt;wp) mask |= POLLIN | POLLRDNORM;\n\n  \/* writable *\/\n  if (left != 1) mask |= POLLOUT | POLLWRNORM;\n\n  return mask;\n}\n<\/code><\/pre>\n<h1>2. poll \u7684\u7b49\u5f85\u4e0e\u5524\u9192<\/h1>\n<p>poll \u51fd\u6570\u63a5\u6536\u7684 <code>poll_table<\/code> \u53ea\u6709\u4e00\u4e2a\u961f\u5217\u5904\u7406\u51fd\u6570 <code>_qproc<\/code> \u548c\u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u5c5e\u6027 <code>_key<\/code>\u3002<\/p>\n<p>\u6587\u4ef6\u62bd\u8c61\u7684\u5177\u4f53\u5b9e\u73b0\u5728\u6784\u5efa\u65f6\u4f1a\u521d\u59cb\u5316\u4e00\u4e2a\u6216\u591a\u4e2a <code>wait_queue_head_t<\/code> \u7c7b\u578b\u7684\u4e8b\u4ef6\u7b49\u5f85\u961f\u5217 \u3002<\/p>\n<p>poll \u7b49\u5f85\u7684\u8fc7\u7a0b\uff1a<\/p>\n<blockquote>\n<ol>\n<li>poll \u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5176\u5b9e\u73b0\u80af\u5b9a\u4f1a\u8c03\u7528 <code>poll_wait<\/code>\uff0c\u8fdb\u800c\u8c03\u7528\u5230 <code>_qproc<\/code> \u51fd\u6570\u3002<\/li>\n<li><code>_qproc<\/code> \u8d1f\u8d23\u6784\u5efa\u5305\u542b <code>wait_queue_entry<\/code> \u7ed3\u6784\u4f53\u7684\u7b49\u5f85\u8282\u70b9\uff08\u6bd4\u5982 select \u64cd\u4f5c\u662f <code>poll_table_entry<\/code> \u7ed3\u6784\u4f53\uff09\uff0c\u5e76\u628a <code>wait_queue_entry<\/code> \u6dfb\u52a0\u5230\u8981\u76d1\u542c\u6587\u4ef6\u7684\u7b49\u5f85\u961f\u5217 <code>wait_address<\/code> \u4e0a\uff08<code>wait_queue_entry<\/code> \u7ed3\u6784\u4f53\u6307\u5b9a\u4e86\u4e8b\u4ef6\u53d1\u751f\u65f6\u7684\u5524\u9192\u51fd\u6570\uff0c\u6bd4\u5982 select \u64cd\u4f5c\u91cc\u6307\u5b9a\u7684\u662f <code>pollwake<\/code> \u51fd\u6570\uff09\u3002<\/li>\n<li>poll \u51fd\u6570\u8fd4\u56de\u6587\u4ef6\u5f53\u524d\u53ef\u7acb\u5373\u6267\u884c\u800c\u4e0d\u963b\u585e\u7684\u64cd\u4f5c\u8868\u793a\u7801\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<p>\u4e8b\u4ef6\u53d1\u751f\u65f6\u7684\u5524\u9192\u8fc7\u7a0b\uff1a<\/p>\n<blockquote>\n<ol>\n<li>\u5f53\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u6587\u4ef6\u7684\u5177\u4f53\u5b9e\u73b0\u904d\u5386\u7b49\u5f85\u961f\u5217\uff0c\u8c03\u7528\u5176\u5524\u9192\u51fd\u6570\uff0c\u7531\u5524\u9192\u51fd\u6570\u8fdb\u884c\u5177\u4f53\u7684\u5524\u9192\u64cd\u4f5c\uff0c\u5524\u9192\u51fd\u6570\u7684\u7c7b\u578b\u4e3a <code>typedef int (*wait_queue_func_t)(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key)<\/code>\u3002<\/li>\n<li>\u5177\u4f53\u7684\u5524\u9192\u51fd\u6570\u5b9e\u73b0\u6839\u636e <code>wait_queue_entry<\/code> \u627e\u5230 <code>_qproc<\/code> \u51fd\u6570\u91cc\u6784\u5efa\u7684\u7b49\u5f85\u8282\u70b9\uff0c\u5229\u7528\u5176\u6570\u636e\u5224\u65ad\u662f\u5426\u9700\u8981\u5524\u9192\uff0c\u662f\u5219\u5524\u9192\u7b49\u5f85\u8fdb\u7a0b\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<p>\u4e00\u4e2a\u5c0f\u56f0\u60d1\uff1a<\/p>\n<blockquote>\n<p>\u5524\u9192\u51fd\u6570\u662f\u5982\u4f55\u6839\u636e <code>wait_queue_entry<\/code> \u627e\u5230\u771f\u5b9e\u7684\u7b49\u5f85\u8282\u70b9\u5462\uff1f\uff1f<br \/>\n  \u8fd9\u662f\u501f\u52a9\u5185\u6838\u7684\u4e00\u4e2a\u5b8f <code>container_of<\/code> \u5b9e\u73b0\u7684\uff0c<code>container_of<\/code> \u662f\u6307\u9488\u7684\u4e00\u4e2a\u7075\u6d3b\u5e94\u7528\uff0c\u4f5c\u7528\u662f\u901a\u8fc7\u7ed3\u6784\u4f53\u53d8\u91cf\u4e2d\u67d0\u4e2a\u6210\u5458\u7684\u9996\u5730\u5740\u8fdb\u800c\u83b7\u5f97\u6574\u4e2a\u7ed3\u6784\u4f53\u53d8\u91cf\u7684\u9996\u5730\u5740\u3002<\/p>\n<\/blockquote>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u5e8f\u5217\u6d89\u53ca\u7684 Linux \u6e90\u7801\u90fd\u662f\u57fa\u4e8e linux-4.14.143 \u3002 1.  &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/linux\/20190919\/1942\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[230,324,323],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1942"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1942"}],"version-history":[{"count":7,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1942\/revisions"}],"predecessor-version":[{"id":1950,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1942\/revisions\/1950"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}