{"id":1951,"date":"2019-09-23T08:30:20","date_gmt":"2019-09-23T00:30:20","guid":{"rendered":"https:\/\/coderbee.net\/?p=1951"},"modified":"2019-09-25T16:02:31","modified_gmt":"2019-09-25T08:02:31","slug":"linux-select-poll-epoll-%e5%8e%9f%e7%90%86%ef%bc%88%e4%ba%8c%ef%bc%89select-%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/linux\/20190923\/1951","title":{"rendered":"Linux select\/poll\/epoll \u539f\u7406\uff08\u4e8c\uff09select \u5b9e\u73b0"},"content":{"rendered":"<p>\u9605\u8bfb\u6e90\u7801\u65f6\u770b\u5230\u4e00\u4e2a\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u4e0d\u77e5\u9053\u5728\u54ea\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<a href=\"https:\/\/elixir.bootlin.com\">\u8fd9\u4e2a\u7f51\u7ad9<\/a>\u6765\u67e5\u627e\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><code>__user<\/code> \u662f\u4e00\u4e2a\u5b8f\u5b9a\u4e49\uff0c\u8868\u793a\u7528\u6237\u7a7a\u95f4\u7684\uff0c\u5185\u6838\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\uff0c\u9700\u8981\u4f7f\u7528\u51fd\u6570 <code>copy_from_user\/copy_to_user<\/code> \u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h1>0. \u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6<\/h1>\n<p>\u8fdb\u7a0b\u7684\u8868\u793a\uff1a<\/p>\n<pre><code class=\"c\">\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1ainclude\/linux\/sched.h\nstruct task_struct {\n    \/\/ ....\u7701\u7565\u5176\u4ed6\u5c5e\u6027\n\n    \/* \u6587\u4ef6\u7cfb\u7edf\u4fe1\u606f: *\/\n    struct fs_struct        *fs;\n\n    \/* \u6253\u5f00\u7684\u6587\u4ef6\u4fe1\u606f: *\/\n    struct files_struct     *files;\n\n    \/\/ ....\u7701\u7565\u5176\u4ed6\u5c5e\u6027\n}\n<\/code><\/pre>\n<p>\u8fdb\u7a0b\u7ef4\u62a4\u6253\u5f00\u7684\u6587\u4ef6\u7684\u6570\u636e\u7ed3\u6784\uff1a<\/p>\n<pre><code class=\"c\">\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1ainclude\/linux\/fdtable.h\nstruct files_struct {\n  \/*\n   * read mostly part\n   *\/\n    atomic_t count;\n    bool resize_in_progress;\n    wait_queue_head_t resize_wait;\n\n    struct fdtable __rcu *fdt;\n    struct fdtable fdtab;\n  \/*\n   * written part on a separate cache line in SMP\n   *\/\n    spinlock_t file_lock ____cacheline_aligned_in_smp;\n    unsigned int next_fd;\n    unsigned long close_on_exec_init[1];\n    unsigned long open_fds_init[1];\n    unsigned long full_fds_bits_init[1];\n    struct file __rcu * fd_array[NR_OPEN_DEFAULT];\n};\n\nstruct fdtable {\n    \/\/ \u8fdb\u7a0b\u80fd\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\n    unsigned int max_fds;\n    struct file __rcu **fd;         \/* current fd array *\/\n    unsigned long *close_on_exec;\n\n    \/\/ \u5f53\u524d\u6253\u5f00\u7684\u4e00\u7ec4\u6587\u4ef6\n    unsigned long *open_fds; \n    unsigned long *full_fds_bits;\n    struct rcu_head rcu;\n};\n\nstatic inline bool fd_is_open(unsigned int fd, const struct fdtable *fdt)\n{\n    return test_bit(fd, fdt-&gt;open_fds);\n}\n<\/code><\/pre>\n<p>\u5c0f\u7ed3\uff1a\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u7ef4\u62a4\u5728\u4f4d\u56fe <code>fdtable.open_fds<\/code> \u91cc\uff0c\u5bf9\u5e94\u7684\u6bd4\u7279\u4f4d\u4e3a 1 \u8868\u793a\u6587\u4ef6\u6253\u5f00\uff0c\u4e3a 0 \u662f\u5173\u95ed\u3002<\/p>\n<p>select \u91cc\u4f20\u9012\u4e8b\u4ef6\u4e5f\u501f\u9274\u4e86\u8fd9\u79cd\u601d\u60f3\uff0c\u901a\u8fc7\u4f4d\u56fe\u6765\u4f20\u9012\uff0cFD \u5bf9\u5e94\u7684\u6bd4\u7279\u4f4d\u4e3a 1 \u8868\u793a\u5bf9\u4e8b\u4ef6\u611f\u5174\u8da3\u6216\u6709\u4e8b\u4ef6\u53d1\u751f\u3002<\/p>\n<p><!--more--><\/p>\n<h1>1. \u57fa\u672c\u6570\u636e\u7ed3\u6784<\/h1>\n<pre><code class=\"c\">\/\/ include\/uapi\/linux\/posix_types.h\n#define __FD_SETSIZE    1024\ntypedef struct {\n    unsigned long fds_bits[__FD_SETSIZE \/ (8 * sizeof(long))];\n} __kernel_fd_set;\n\n\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1a include\/linux\/types.h\ntypedef __kernel_fd_set     fd_set;\n\n\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1a fs\/select.c\n\/\/ \u7528\u4e8e\u4f20\u9012 select \u7684\u8f93\u5165\u4e8b\u4ef6\u3001\u8f93\u51fa\u7ed3\u679c\uff0c\u662f fd_set \u7684\u6269\u5c55\u7248\u3002\ntypedef struct {\n    unsigned long *in, *out, *ex;\n    unsigned long *res_in, *res_out, *res_ex;\n} fd_set_bits;\n<\/code><\/pre>\n<p>\u5c0f\u7ed3\uff1a\u4ece\u4e0a\u8ff0\u5b9a\u4e49\u53ef\u4ee5\u770b\u5230\uff0cfd_set \u5c31\u662f\u4e00\u4e2a\u4f4d\u56fe\uff0c\u9650\u5236\u4e86 select \u64cd\u4f5c\u5c31\u591a\u53ef\u4ee5 poll 1024 \u4e2a\u6587\u4ef6\uff0c\u5982\u679c\u8981\u652f\u6301 poll \u66f4\u591a\u7684\u6587\u4ef6\uff0c\u9700\u8981\u4fee\u6539\u6e90\u7801\u3001\u91cd\u65b0\u7f16\u8bd1\u3002<\/p>\n<p><code>fd_set_bits<\/code> \u91cc\u76846\u4e2a\u53d8\u91cf\u662f6\u4e2a\u6307\u9488\uff0c\u6307\u5411\u4e0d\u540c\u7684\u4f4d\u56fe\u8d77\u59cb\u5730\u5740\uff0c\u89c1\u4e0b\u6587\u91cc\u7684\u6ce8\u91ca\u8bf4\u660e\u3002<\/p>\n<h1>2. select \u4e3b\u903b\u8f91<\/h1>\n<p>select \u8c03\u7528\u5229\u7528 fd_set \u8fd9\u4e2a\u4f4d\u56fe\u6765\u4f20\u9012\u8f93\u5165\u7684\u8981\u76d1\u542c\u7684\u4e8b\u4ef6\u548c\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"c\">\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1a fs\/select.c\n\n\/\/ select \u7cfb\u7edf\u8c03\u7528\u539f\u578b\nSYSCALL_DEFINE5(select, int, n, fd_set __user *, inp, fd_set __user *, outp,\n        fd_set __user *, exp, struct timeval __user *, tvp)\n{\n    struct timespec64 end_time, *to = NULL;\n    struct timeval tv;\n    int ret;\n\n    if (tvp) {\n        if (copy_from_user(&amp;tv, tvp, sizeof(tv)))\n            return -EFAULT;\n\n        to = &amp;end_time;\n        if (poll_select_set_timeout(to,\n                tv.tv_sec + (tv.tv_usec \/ USEC_PER_SEC),\n                (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))\n            return -EINVAL;\n    }\n\n    ret = core_sys_select(n, inp, outp, exp, to);\n    ret = poll_select_copy_remaining(&amp;end_time, tvp, 1, ret);\n\n    return ret;\n}\n\nint core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,\n               fd_set __user *exp, struct timespec64 *end_time)\n{\n    fd_set_bits fds;\n    void *bits;\n    int ret, max_fds;\n    size_t size, alloc_size;\n    struct fdtable *fdt;\n    \/* Allocate small arguments on the stack to save memory and be faster *\/\n    \/\/ \u5728\u6808\u4e0a\u5206\u914d\u7684\u5c0f\u6bb5\u53c2\u6570\uff0c\u7528\u4e8e\u8282\u7701\u5185\u5b58\u548c\u63d0\u5347\u901f\u5ea6\n    \/\/ SELECT_STACK_ALLOC=256\n    long stack_fds[SELECT_STACK_ALLOC\/sizeof(long)];\n\n    ret = -EINVAL;\n    if (n &lt; 0)\n        goto out_nofds;\n\n    \/* max_fds can increase, so grab it once to avoid race *\/\n    rcu_read_lock();\n    fdt = files_fdtable(current-&gt;files);\n    max_fds = fdt-&gt;max_fds;\n    rcu_read_unlock();\n\n    \/\/ poll \u7684\u6700\u5927 FD \u4e0d\u80fd\u8d85\u8fc7 \u8fdb\u7a0b\u6253\u5f00\u7684\u6700\u5927FD\n    if (n &gt; max_fds)\n        n = max_fds;\n\n\n    \/\/ \u6bcf\u4e2a\u6587\u4ef6\u67093\u79cd\u8f93\u5165\u30013\u79cd\u8f93\u51fa\uff0c\u56e0\u6b64\u6bcf\u4e2a\u6587\u4ef6\u9700\u89816\u4e2a\u4f4d\u56fe\u6765\u8868\u793a\u4e8b\u4ef6\n    \/*\n     * We need 6 bitmaps (in\/out\/ex for both incoming and outgoing),\n     * since we used fdset we need to allocate memory in units of\n     * long-words. \n     *\/\n    \/\/ n \u4e2a\u6587\u4ef6\u9700\u8981\u7684\u5b57\u8282\u6570\uff0c\u4e5f\u662f\u6bcf\u4efd\u4f4d\u56fe\u7684\u5927\u5c0f\n    size = FDS_BYTES(n);\n    bits = stack_fds;\n\n    \/\/ sizeof(stack_fds) \/ 6 \u662f\u628a\u6808\u4e0a\u5206\u914d\u7684\u5185\u5b58\u5757\u5212\u5206\u4e3a 6 \u4efd\u505a\u4f4d\u56fe\n    if (size &gt; sizeof(stack_fds) \/ 6 ) {\n        \/* Not enough space in on-stack array; must use kmalloc *\/\n        \/\/ \u6808\u4e0a\u5206\u914d\u7684\u7a7a\u95f4\u4e0d\u591f\uff0c\u8981\u4f7f\u7528 kmalloc \n        ret = -ENOMEM;\n        if (size &gt; (SIZE_MAX \/ 6))\n            goto out_nofds;\n\n        alloc_size = 6 * size;\n        bits = kvmalloc(alloc_size, GFP_KERNEL);\n        if (!bits)\n            goto out_nofds;\n    }\n\n    \/\/ \u628a fds \u91cc\u7684\u6307\u9488\u6307\u5411\u4e0d\u540c\u4f4d\u56fe\u7684\u8d77\u59cb\u5730\u5740\n    fds.in      = bits;\n    fds.out     = bits +   size;\n    fds.ex      = bits + 2*size;\n    fds.res_in  = bits + 3*size;\n    fds.res_out = bits + 4*size;\n    fds.res_ex  = bits + 5*size;\n\n    \/\/ \u628a\u7528\u6237\u7a7a\u95f4\u7684\u4e8b\u4ef6\u62f7\u8d1d\u5230\u5185\u6838\u7a7a\u95f4\n    if ((ret = get_fd_set(n, inp, fds.in)) ||\n        (ret = get_fd_set(n, outp, fds.out)) ||\n        (ret = get_fd_set(n, exp, fds.ex)))\n        goto out;\n\n    \/\/ \u6e05\u96f6\u8f93\u51fa\u7ed3\u679c\n    zero_fd_set(n, fds.res_in);\n    zero_fd_set(n, fds.res_out);\n    zero_fd_set(n, fds.res_ex);\n\n    ret = do_select(n, &amp;fds, end_time);\n\n    if (ret &lt; 0)\n        goto out;\n    if (!ret) {\n        ret = -ERESTARTNOHAND;\n        if (signal_pending(current))\n            goto out;\n        ret = 0;\n    }\n\n    \/\/ \u901a\u8fc7 __copy_to_user \u62f7\u8d1d\u7ed3\u679c\u5230\u7528\u6237\u7a7a\u95f4\n    if (set_fd_set(n, inp, fds.res_in) ||\n        set_fd_set(n, outp, fds.res_out) ||\n        set_fd_set(n, exp, fds.res_ex))\n        ret = -EFAULT;\n\nout:\n    if (bits != stack_fds)\n        kvfree(bits);\nout_nofds:\n    return ret;\n}\n\n\nstatic int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)\n{\n    ktime_t expire, *to = NULL;\n    \/\/ \u6784\u5efa\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\uff0c\u8be5\u961f\u5217\u7ef4\u62a4\u7740\u5bf9\u6240\u6709\u6dfb\u52a0\u5230\u6587\u4ef6\u7684\u7b49\u5f85\u961f\u5217\u7684\u8282\u70b9\u7684\u6307\u9488\n    struct poll_wqueues table;\n\n    \/\/ \u7b49\u5f85\u8282\u70b9\u7684\u6570\u636e\u539f\u578b\uff0c\u4e3b\u8981\u7528\u4e8e\u4f20\u9012\u53c2\u6570\n    poll_table *wait;\n    int retval, i, timed_out = 0;\n    u64 slack = 0;\n    unsigned int busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;\n    unsigned long busy_start = 0;\n\n    rcu_read_lock();\n    retval = max_select_fd(n, fds);\n    rcu_read_unlock();\n\n    if (retval &lt; 0)\n        return retval;\n    n = retval;\n\n    \/\/ \u8bbe\u7f6e wait._qproc = __pollwait\n    poll_initwait(&amp;table);\n    wait = &amp;table.pt;\n    if (end_time &amp;&amp; !end_time-&gt;tv_sec &amp;&amp; !end_time-&gt;tv_nsec) {\n        wait-&gt;_qproc = NULL;\n        timed_out = 1;\n    }\n\n    if (end_time &amp;&amp; !timed_out)\n        slack = select_estimate_accuracy(end_time);\n\n    retval = 0;\n    for (;;) {\n        unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;\n        bool can_busy_loop = false;\n\n        inp = fds-&gt;in; outp = fds-&gt;out; exp = fds-&gt;ex;\n        rinp = fds-&gt;res_in; routp = fds-&gt;res_out; rexp = fds-&gt;res_ex;\n\n        \/\/ \u5206\u6279\u8f6e\u8be2\n        for (i = 0; i &lt; n; ++rinp, ++routp, ++rexp) {\n            unsigned long in, out, ex, all_bits, bit = 1, mask, j;\n            unsigned long res_in = 0, res_out = 0, res_ex = 0;\n\n            in = *inp++; out = *outp++; ex = *exp++;\n            all_bits = in | out | ex;           \/\/ \n            if (all_bits == 0) {\n                \/\/ \u6ca1\u6709\u6562\u5174\u8da3\u7684\u4e8b\u4ef6\uff0c\u8df3\u8fc7 BITS_PER_LONG \u4e2a\u6587\u4ef6\n                i += BITS_PER_LONG;\n                continue;\n            }\n\n            \/\/ \u6279\u6b21\u5185\u9010\u4e2a\u8f6e\u8be2\n            for (j = 0; j &lt; BITS_PER_LONG; ++j, ++i, bit &lt;&lt;= 1) {   \/\/ bit \u5de6\u79fb\u662f\u4e3a\u4e86\u7ed9\u6b63\u786e\u7684\u6587\u4ef6\u8bbe\u7f6e\u4e8b\u4ef6\u7ed3\u679c\n                struct fd f;\n                if (i &gt;= n)\n                    break;\n                if (!(bit &amp; all_bits))\n                    continue;\n                f = fdget(i);\n                if (f.file) {\n                    \/\/ \u627e\u5230\u4e86\u6587\u4ef6\n                    const struct file_operations *f_op;\n                    f_op = f.file-&gt;f_op;\n                    mask = DEFAULT_POLLMASK;\n                    if (f_op-&gt;poll) {\n                        wait_key_set(wait, in, out,\n                                 bit, busy_flag);\n\n                        \/\/ \u8c03\u7528\u6587\u4ef6\u7684 poll \u51fd\u6570\uff0c\u6700\u7ec8\u4f1a\u8c03\u7528\u5230  __pollwait \u51fd\u6570\n                        \/\/ __pollwait \n                        mask = (*f_op-&gt;poll)(f.file, wait);\n                    }\n                    fdput(f);\n\n                    \/\/ \u4e0b\u9762\u7684 if \u8bed\u53e5\u5757\u5185\uff0c\u662f\u5df2\u7ecf\u68c0\u6d4b\u5230\u4e8b\u4ef6\u53d1\u751f\u4e86\uff0c\u8fdb\u7a0b\u4e0d\u9700\u8981\u8fdb\u884c\u7b49\u5f85\u548c\u5524\u9192\n                    \/\/ \u628a _qproc \u8bbe\u7f6e\u4e3a NULL \u662f\u4e3a\u4e86\u907f\u514d\u5f80\u540e\u7eed poll \u672a\u5c31\u7eea\u7684\u6587\u4ef6\u65f6\u88ab\u52a0\u5165\u7b49\u5f85\u961f\u5217\n                    \/\/ \u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u65e0\u6548\u7684\u5524\u9192\n                    if ((mask &amp; POLLIN_SET) &amp;&amp; (in &amp; bit)) {\n                        res_in |= bit;\n                        retval++;\n                        wait-&gt;_qproc = NULL;\n                    }\n                    if ((mask &amp; POLLOUT_SET) &amp;&amp; (out &amp; bit)) {\n                        res_out |= bit;\n                        retval++;\n                        wait-&gt;_qproc = NULL;\n                    }\n                    if ((mask &amp; POLLEX_SET) &amp;&amp; (ex &amp; bit)) {\n                        res_ex |= bit;\n                        retval++;\n                        wait-&gt;_qproc = NULL;\n                    }\n\n                    \/\/ \n                    \/* got something, stop busy polling *\/\n                    if (retval) {\n                        can_busy_loop = false;\n                        busy_flag = 0;\n\n                    \/*\n                     * only remember a returned\n                     * POLL_BUSY_LOOP if we asked for it\n                     *\/\n                    } else if (busy_flag &amp; mask)\n                        can_busy_loop = true;\n\n                }\n            }\n\n            \/\/ \u5c0f\u6279\u6b21\u8f6e\u8be2\u5b8c\uff0c\u628a\u7ed3\u679c\u8bb0\u5f55\u4e0b\u6765\n            if (res_in)\n                *rinp = res_in;\n            if (res_out)\n                *routp = res_out;\n            if (res_ex)\n                *rexp = res_ex;\n\n            \/\/  \u8fdb\u5165\u7761\u7720\uff0c\u7b49\u5f85\u8d85\u65f6\u6216\u5524\u9192\n            cond_resched();\n        }\n\n        \/\/ \u6240\u6709\u6587\u4ef6\u90fd\u8f6e\u8be2\u4e86\u4e00\u904d\uff0c\u8981\u52a0\u5165\u6587\u4ef6\u7b49\u5f85\u961f\u5217\u7684\u90fd\u5df2\u7ecf\u52a0\u4e86\uff0c\u907f\u514d\u4e0b\u6b21\u8f6e\u8be2\u91cd\u590d\u6dfb\u52a0\n        wait-&gt;_qproc = NULL;\n\n        \/\/ \u6709\u4e8b\u4ef6\u3001\u6216\u8d85\u65f6\u3001\u6216\u6709\u4fe1\u53f7\u8981\u5904\u7406\n        if (retval || timed_out || signal_pending(current))\n            break;\n        if (table.error) {\n            retval = table.error;\n            break;\n        }\n\n        \/* only if found POLL_BUSY_LOOP sockets &amp;&amp; not out of time *\/\n        if (can_busy_loop &amp;&amp; !need_resched()) {\n            if (!busy_start) {\n                busy_start = busy_loop_current_time();\n                continue;\n            }\n            if (!busy_loop_timeout(busy_start))\n                continue;\n        }\n        busy_flag = 0;\n\n        \/*\n         * If this is the first loop and we have a timeout\n         * given, then we convert to ktime_t and set the to\n         * pointer to the expiry value.\n         *\/\n        if (end_time &amp;&amp; !to) {\n            expire = timespec64_to_ktime(*end_time);\n            to = &amp;expire;\n        }\n\n        \/\/ \u8fdb\u7a0b\u72b6\u6001\u8bbe\u7f6e\u4e3a TASK_INTERRUPTIBLE\uff0c\u8fdb\u5165\u7761\u7720\u76f4\u5230\u8d85\u65f6\n        if (!poll_schedule_timeout(&amp;table, TASK_INTERRUPTIBLE,\n                       to, slack))\n            timed_out = 1;\n    }\n\n    \/\/ \u91ca\u653e\u7b49\u5f85\u8282\u70b9\uff0c\u91cd\u70b9\u662f\u628a\u7b49\u5f85\u8282\u70b9\u4ece\u6587\u4ef6\u7684\u7b49\u5f85\u961f\u5217\u5220\u9664\u6389\n    poll_freewait(&amp;table);\n\n    return retval;\n}\n<\/code><\/pre>\n<h1>3. \u7b49\u5f85\u4e0e\u5524\u9192<\/h1>\n<h2>3.0 \u76f8\u5173\u6570\u636e\u7ed3\u6784<\/h2>\n<pre><code class=\"c\">\/\/ \u6dfb\u52a0\u5230\u6587\u4ef6\u7b49\u5f85\u961f\u5217\u7684\u8282\u70b9\u7c7b\u578b\nstruct poll_table_entry {\n    \/\/ \u8981\u76d1\u542c\u7684\u76ee\u6807\u6587\u4ef6\n    struct file *filp;\n\n    \/\/ \u8981\u76d1\u542c\u7684\u4e8b\u4ef6\n    unsigned long key;\n\n    wait_queue_entry_t wait;\n\n    \/\/ \u7b49\u5f85\u961f\u5217\u5934\n    wait_queue_head_t *wait_address;\n};\n\n\n\/\/ \u6bcf\u6b21\u6267\u884c select \u8c03\u7528\u65f6\u7ef4\u62a4\u7684\u7b49\u5f85\u961f\u5217\nstruct poll_wqueues {\n    \/\/ \u8c03\u7528 poll \u64cd\u4f5c\u65f6\u7528\u4e8e\u4f20\u9012\u4fe1\u606f\u7684\u5bf9\u8c61\n    poll_table pt;\n\n    \/\/ inline_entries \u7a7a\u95f4\u4e0d\u591f\u7528\u7533\u8bf7\u7684\u7b49\u5f85\u8282\u70b9\u5217\u8868\n    struct poll_table_page *table;\n\n    \/\/ \u6267\u884c select \u7684\u8fdb\u7a0b\u4fe1\u606f\n    struct task_struct *polling_task;\n\n    \/\/ \u662f\u5426\u5df2\u5524\u9192\n    int triggered;\n    int error;\n\n    \/\/ \u6307\u5411 inline_entries \u91cc\u672a\u4f7f\u7528\u7684\u8282\u70b9\u4e0b\u6807\n    int inline_index;\n\n    \/\/ \u9884\u7533\u8bf7\u7684\u7b49\u5f85\u8282\u70b9\n    struct poll_table_entry inline_entries[N_INLINE_POLL_ENTRIES];\n};\n\n\/\/ \u5982\u679c inline_entries \u4e0d\u591f\u7528\uff0c\u5219\u4ee5 poll_table_page \u94fe\u8868\u7684\u5f62\u5f0f\u5b58\u8d77\u6765\nstruct poll_table_page {\n    struct poll_table_page * next;\n    struct poll_table_entry * entry;\n    struct poll_table_entry entries[0];\n};\n<\/code><\/pre>\n<h2>3.1 \u7b49\u5f85<\/h2>\n<pre><code class=\"c\">static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)\n{\n    \/\/ p-&gt;_qproc \u5bf9\u5e94\u4e86\u4e0a\u9762 wait-&gt;_qproc = NULL; \u7684\u4f18\u5316\n    if (p &amp;&amp; p-&gt;_qproc &amp;&amp; wait_address)\n        p-&gt;_qproc(filp, wait_address, p);\n}\n\nvoid poll_initwait(struct poll_wqueues *pwq)\n{\n    \/\/ \u8bbe\u7f6e poll \u7b49\u5f85\u961f\u5217\u7684\u5904\u7406\u51fd\u6570\u4e3a __pollwait\n    init_poll_funcptr(&amp;pwq-&gt;pt, __pollwait);\n    pwq-&gt;polling_task = current;\n    pwq-&gt;triggered = 0;\n    pwq-&gt;error = 0;\n    pwq-&gt;table = NULL;\n    pwq-&gt;inline_index = 0;\n}\nEXPORT_SYMBOL(poll_initwait);\n\nstatic void __pollwait(struct file *filp, wait_queue_head_t *wait_address,\n                poll_table *p)\n{\n    struct poll_wqueues *pwq = container_of(p, struct poll_wqueues, pt);\n    \/\/ \u83b7\u53d6\u4e00\u4e2a\u7b49\u5f85\u8282\u70b9\n    struct poll_table_entry *entry = poll_get_entry(pwq);\n    if (!entry)\n        return;\n    entry-&gt;filp = get_file(filp);\n    entry-&gt;wait_address = wait_address;\n\n    \/\/ \u8bbe\u7f6e\u6562\u5174\u8da3\u7684\u4e8b\u4ef6\n    entry-&gt;key = p-&gt;_key;\n\n    \/\/ \u8bbe\u7f6e\u7b49\u5f85\u8282\u70b9\u7684\u56de\u8c03\u51fd\u6570\u4e3a pollwake\uff0c\u4e5f\u5373\u5524\u9192\u51fd\u6570\n    init_waitqueue_func_entry(&amp;entry-&gt;wait, pollwake);\n\n    \/\/ \u6307\u5411\u672c\u6b21 select \u64cd\u4f5c\u7684 \u7b49\u5f85\u961f\u5217\n    entry-&gt;wait.private = pwq;\n\n    \/\/ \u628a\u7b49\u5f85\u8282\u70b9\u6dfb\u52a0\u5230\u6587\u4ef6\u7684\u7b49\u5f85\u961f\u5217\u4e0a\n    add_wait_queue(wait_address, &amp;entry-&gt;wait);\n}\n\nstatic inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)\n{\n    pt-&gt;_qproc = qproc;\n    pt-&gt;_key   = ~0UL; \/* all events enabled *\/\n}\n<\/code><\/pre>\n<h2>3.2 \u5524\u9192<\/h2>\n<pre><code class=\"c\">static int pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)\n{\n    struct poll_table_entry *entry;\n\n    \/\/ \u627e\u51fa\u7b49\u5f85\u8282\u70b9 poll_table_entry\n    entry = container_of(wait, struct poll_table_entry, wait);\n\n    \/\/ \u68c0\u6d4b\u662f\u5426\u53d1\u751f\u4e86\u6562\u5174\u8da3\u7684\u4e8b\u4ef6\n    if (key &amp;&amp; !((unsigned long)key &amp; entry-&gt;key))\n        return 0;\n\n    \/\/ \u6267\u884c\u5524\u9192\n    return __pollwake(wait, mode, sync, key);\n}\n\n\/\/ \u5177\u4f53\u7684\u5524\u9192\u903b\u8f91\u5c31\u4e0d\u5c55\u5f00\u4e86\uff0c\u6d89\u53ca\u8fdb\u7a0b\u8c03\u5ea6\u90a3\u4e9b\u3002\nstatic int __pollwake(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)\n{\n    struct poll_wqueues *pwq = wait-&gt;private;\n    DECLARE_WAITQUEUE(dummy_wait, pwq-&gt;polling_task);\n\n    \/*\n     * Although this function is called under waitqueue lock, LOCK\n     * doesn't imply write barrier and the users expect write\n     * barrier semantics on wakeup functions.  The following\n     * smp_wmb() is equivalent to smp_wmb() in try_to_wake_up()\n     * and is paired with smp_store_mb() in poll_schedule_timeout.\n     *\/\n    smp_wmb();\n    pwq-&gt;triggered = 1;     \/\/ \u8bbe\u7f6e\u4e3a\u5df2\u89e6\u53d1\n\n    \/*\n     * Perform the default wake up operation using a dummy\n     * waitqueue.\n     *\n     * TODO: This is hacky but there currently is no interface to\n     * pass in @sync.  @sync is scheduled to be removed and once\n     * that happens, wake_up_process() can be used directly.\n     *\/\n    return default_wake_function(&amp;dummy_wait, mode, sync, key);\n}\n<\/code><\/pre>\n<h1>4. \u5c0f\u7ed3<\/h1>\n<h2>4.0 \u6982\u8981\u8bf4\u660e<\/h2>\n<ol>\n<li>select \u8c03\u7528\u4f7f\u7528 fd_set \u8fd9\u4e2a\u4f4d\u56fe\u6570\u636e\u7ed3\u6784\u6765\u4f20\u9012\u8f93\u5165\u4e8b\u4ef6\u3001\u7ed3\u679c\u3002<\/li>\n<li>select\u6267\u884c\u65f6\uff0c\u7531\u4e8e\u5185\u6838\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u7528\u6237\u7a7a\u95f4\uff0c\u56e0\u6b64\u9700\u8981\u7528 3 \u4e2a\u4f4d\u56fe\u6765\u5b58\u653e\u8f93\u5165\u7684\u4e8b\u4ef6\uff0c\u518d\u75283\u4e2a\u4f4d\u56fe\u6765\u5b58\u653e poll \u7684\u7ed3\u679c\u3002<code>fd_set_bits<\/code> \u91cc\u76846\u4e2a\u6307\u9488\u5206\u522b\u6307\u5411\u8fd9 6 \u4e2a\u4f4d\u56fe\u7684\u8d77\u59cb\u5730\u5740\u3002<\/li>\n<li>\u4ece <code>do_select<\/code> \u7684\u903b\u8f91\u53ef\u4ee5\u770b\u5230\uff1a\n<ul>\n<li>select \u5b9e\u73b0\u9996\u5148\u5c1d\u8bd5\u5728\u6808\u4e0a\u5206\u914d\u4e00\u5757\u5185\u5b58\u6765\u5b58\u653e\u4e0a\u8ff06\u4e2a\u4f4d\u56fe\uff0c\u5982\u679c\u5b58\u653e\u4e0d\u4e0b\u5219\u7533\u8bf7\u65b0\u7684\u5185\u5b58\u5757\u6765\u5b58\u653e\u3002<\/li>\n<li>\u6bcf\u6b21 select \u8c03\u7528\u90fd\u5c1d\u8bd5\u5bf9\u6240\u6709\u7684\u6587\u4ef6\u5206\u6279\u6b21\u8c03\u7528 poll\uff0c\u5728\u6bcf\u4e2a\u6279\u6b21\u4e4b\u95f4\u4f1a\u7761\u7720\u3002<\/li>\n<li>\u5982\u679c\u8fd8\u6ca1\u6709\u6587\u4ef6\u6709\u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u53d1\u751f\uff0c\u5219\u5728\u88ab poll \u7684\u6587\u4ef6\u7684\u7b49\u5f85\u961f\u5217\u4e0a\u52a0\u5165\u7b49\u5f85\u8282\u70b9\u3002\u4e00\u65e6\u67d0\u4e2a\u6587\u4ef6\u6709\u4e8b\u4ef6\u53d1\u751f\uff0c\u5219\u540e\u7eed\u7684\u6587\u4ef6\u4e0d\u7ba1\u662f\u5426\u6709\u4e8b\u4ef6\u53d1\u751f\uff0c\u90fd\u4e0d\u518d\u52a0\u5165\u7b49\u5f85\u8282\u70b9\u3002<\/li>\n<li>\u5982\u679c\u524d\u4e00\u8f6e poll \u6ca1\u6709\u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u53d1\u751f\uff0c\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\uff0c\u7b49\u5f85\u5524\u9192\u6216\u76f4\u81f3\u8d85\u65f6\u6216\u6709\u4fe1\u53f7\u8981\u5904\u7406\uff1b\u8fdb\u7a0b\u518d\u6b21\u8fd0\u884c\u65f6\uff0c\u4ecd\u7136\u9700\u8981 poll \u6240\u6709\u7684\u6587\u4ef6\u3002<\/li>\n<li>select \u8c03\u7528\u7ed3\u675f\u524d\uff0c\u628a\u672c\u6b21\u8c03\u7528\u6dfb\u52a0\u5230\u6587\u4ef6\u7b49\u5f85\u961f\u5217\u4e0a\u7684\u8282\u70b9\u90fd\u5220\u9664\u6389\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>4.1 \u4e0d\u8db3<\/h2>\n<ol>\n<li>\u6700\u591a poll 1024 \u4e2a\u6587\u4ef6\u7684\u9650\u5236\u3002<\/li>\n<li>\u8981 poll \u7684\u6587\u4ef6\u6570\u91cf\u5f88\u5927\u65f6\uff0c\u4e3a\u4f20\u9012\u8f93\u5165\u7684\u611f\u5174\u8da3\u7684\u4e8b\u4ef6\u548c\u8f93\u51fa\u7ed3\u679c\uff0c\u5728\u7528\u6237\u7a7a\u95f4\u4e0e\u5185\u6838\u7a7a\u95f4\u9700\u8981\u62f7\u8d1d\u5f88\u5927\u7684\u5185\u5b58\u3002<\/li>\n<li>\u6548\u7387\u53d7\u9650\u4e8e\u8981 poll \u7684\u6587\u4ef6\u6570\u91cf\uff0c\u6570\u91cf\u8d8a\u591a\u9700\u8981\u7684\u65f6\u95f4\u5c31\u8d8a\u591a\u3002<\/li>\n<\/ol>\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>\u9605\u8bfb\u6e90\u7801\u65f6\u770b\u5230\u4e00\u4e2a\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u4e0d\u77e5\u9053\u5728\u54ea\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u7f51\u7ad9\u6765\u67e5\u627e\uff0c\u975e\u5e38\u65b9\u4fbf\u3002  &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/linux\/20190923\/1951\">\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,323],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1951"}],"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=1951"}],"version-history":[{"count":6,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1951\/revisions"}],"predecessor-version":[{"id":1964,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1951\/revisions\/1964"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}