poll 系統呼叫 2

2021-06-19 02:00:01 字數 2110 閱讀 8142

過段時間再看poll系統呼叫,還是不知說的啥。還是要概括下,寫成流水帳沒什麼用。

系統呼叫 poll的功能說白了就是當前程序睡眠在fd的事件上,當事件發生後,退出睡眠,並返回發生的事件。

syscall_define3(poll, struct pollfd __user *, ufds, unsigned int, nfds,

int, timeout_msecs)

int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,

struct timespec *end_time)

;struct pollfd ;

*//*the number of  pollfd structures*/

len = min_t(unsigned int, nfds, n_stack_pps);

for (;;)

poll_initwait(&table);

fdcount = do_poll(nfds, head, &table, end_time);

poll_freewait(&table);

for (walk = head; walk; walk = walk->next)

err = fdcount;

return err;

}對該函式搞清楚,在睡眠在**?何時退出該函式?

static int do_poll(unsigned int nfds,  struct poll_list *list,

struct poll_wqueues *wait, struct timespec *end_time)}}

if (count || timed_out)

break;

/*poll函式會在這裡阻塞當前程序:一段時間或者直到事件發生

*當事件發生後,該程序喚醒後,從這裡執行,呼叫各fd的do_pollfd,

*如果有事件發生count > 0,會從該函式退出

*/if (!poll_schedule_timeout(wait, task_interruptible, to, slack))

timed_out = 1;

}return count;

}static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)

/* mask out unneeded events. */

mask &= pollfd->events | pollerr | pollhup;

fput_light(file, fput_needed);}}

pollfd->revents = mask;

return mask;

}static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,

poll_table *wait)

if (tty->ops->write && !tty_is_writelocked(tty) &&

tty_chars_in_buffer(tty) < wakeup_chars &&

tty_write_room(tty) > 0)

mask |= pollout | pollwrnorm;

return mask;

}實現這個功能的是每個具體檔案中都必須呼叫的:

poll_wait(), 就是上面說的:

n_tty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)

->  poll_wait(file, &tty->read_wait, wait);

static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)

/* add a new entry */

static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,

poll_table *p)

poll系統呼叫

上節所述的io復用中的select函式基本機制與poll基本一致,都是採用輪詢的方式來檢視我們所關注的檔案描述符。一 下面我們首先介紹一下poll與select的區別 1.poll將描述符和事件統一到乙個結構體中 2.poll能夠同時監聽的檔案描述符比select多 3.poll事件型別比selec...

poll系統呼叫

1 include 2 int poll struct pollfd fds,nfds t nfds,int timeout fds,pollfd結構型別的陣列 可變長陣列,陣列元素為結構體pollfd 結構體中含有三個成員變數 int,short,short 分別為檔案描述符fd,註冊的事件eve...

IO復用 poll系統呼叫

poll系統呼叫和select類似,也是在指定時間內輪詢一定數量的檔案描述符,以測試其中是否有已就緒的檔案描述符,pool原型如下 includeint poll struct pollfd fds,nfds t nfds,int timeout 1.fds引數是乙個pollfd結構型別的陣列,它指...