Why do we need to call poll_wait in poll?

前端 未结 3 599
野趣味
野趣味 2021-01-11 15:05

In LDD3, i saw such codes

static unsigned int scull_p_poll(struct file *filp, poll_table *wait)
{
    struct scull_pipe *dev = filp->private_data;
    uns         


        
3条回答
  •  误落风尘
    2021-01-11 15:47

    poll_wait adds your device (represented by the "struct file") to the list of those that can wake the process up.

    The idea is that the process can use poll (or select or epoll etc) to add a bunch of file descriptors to the list on which it wishes to wait. The poll entry for each driver gets called. Each one adds itself (via poll_wait) to the waiter list.

    Then the core kernel blocks the process in one place. That way, any one of the devices can wake up the process. If you return non-zero mask bits, that means those "ready" attributes (readable/writable/etc) apply now.

    So, in pseudo-code, it's roughly like this:

    foreach fd:
        find device corresponding to fd
        call device poll function to setup wait queues (with poll_wait) and to collect its "ready-now" mask
    
    while time remaining in timeout and no devices are ready:
        sleep
    
    return from system call (either due to timeout or to ready devices)
    

提交回复
热议问题