Linux poll机制

Posted 天地有大美而不言

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux poll机制相关的知识,希望对你有一定的参考价值。

1.用户空间调用(参考 poll(2) - Linux man page

 int poll(struct pollfd *fds, nfds_t nfds, int timeout);

it waits for one of a set of file descriptors to become ready to perform I/O.

The set of file descriptors to be monitored is specified in the fds argument, which is an array of structures of the following form:

struct pollfd {
    int   fd;         /* file descriptor */
    short events;     /* requested events */
    short revents;    /* returned events */
};

关于timeout参数的说明:

  timeout>0,设置超时时间为timeout

  timeout=0,直接返回

  timeout<0,无限长超时时间

返回值说明:

On success, a positive number is returned; this is the number of structures which have nonzero revents fields (in other words, those descriptors with events or errors reported).

A value of 0 indicates that the call timed out and no file descriptors were ready. On error, -1 is returned, and errno is set appropriately.

2.内核调用

asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,long timeout_msecs)
    -->ret = do_sys_poll(ufds, nfds, &timeout);
        -->struct poll_wqueues table;
        -->poll_initwait(&table);
            -->init_poll_funcptr(&pwq->pt, __pollwait);
        -->fdcount = do_poll(nfds, head, &table, timeout);
            -->do_pollfd(pfd, pt)
                -->if (file->f_op && file->f_op->poll)

2.1

以上是关于Linux poll机制的主要内容,如果未能解决你的问题,请参考以下文章

Linux poll机制

Linux-应用程序中的poll机制

013_Linux驱动之_poll机制

Linux通信之poll机制分析

Linux poll机制

Linux嵌入式驱动学习之路按键驱动-poll机制