1. 程式人生 > >Linux poll機制

Linux poll機制

log 空間 num queue truct int with ive cif

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機制