libev 使用

觀察器

  1. IO
    ev_io_init (ev_io *, callback, int fd, int events) ev_io_set (ev_io *, int fd, int events)api

    I/O watchers check whether a file descriptor is readable or writable in each iteration of the event loop, or, more precisely, when reading would not block the process and writing would at least be able to write some data.
    NOTE :socket

  • 觸發方式:level-triggering(水平觸發 LT)
  • 每一個fd能夠註冊儘量多的watchers, fd要儘量設置爲NOBLOCKING

ps: 什麼叫水平觸發和邊緣觸發?

水平觸發(level-triggered,也被稱爲條件觸發)LT: 只要知足條件,就觸發一個事件(只要有數據沒有被獲取,內核就不斷通知你)
邊緣觸發(edge-triggered)ET: 每當狀態變化時,觸發一個事件。
「舉個讀socket的例子,假定通過長時間的沉默後,如今來了100個字節,這時不管邊緣觸發和條件觸發都會產生一個read ready notification通知應用程序可讀。應用程序讀了50個字節,而後從新調用api等待io事件。這時條件觸發的api會由於還有50個字節可讀,從而> 當即返回用戶一個read ready notification。而邊緣觸發的api會由於可讀這個狀態沒有發生變化而陷入長期等待。所以在使用邊緣觸發的api時,要注意每次都要讀到socket返回EWOULDBLOCK爲止,不然這個socket就算廢了。而使用條件觸發的api 時,若是應用程序不需> 要寫就不要關注socket可寫的事件,不然就會無限次的當即返回一個write ready notification。你們經常使用的select就是屬於條件觸發這一類,長期關注socket寫事件會出現CPU 100%的毛病。oop

相關文章
相關標籤/搜索