2020 11 29 epoll函式的基本使用過程

2021-10-10 23:22:19 字數 1149 閱讀 2883

一般用於伺服器監聽某乙個檔案描述符;和select相似

1、建立紅黑樹,用於監聽檔案描述符

#include

int epoll_create(int size)

功能:建立紅黑樹

引數:size:寫大於0即可

返回值:數的控制代碼

2、節點上樹、節點下樹、修改樹節點

#include

int epoll_ctl(int efd,int op,int fd,struct epoll_event *event)

功能:引數:epfd:樹的根節點(控制代碼)

op:epoll_ctl_add

epoll_ctl_mod

epoll_ctl_del

fd:需要上樹監聽的檔案描述符

event:乙個樹上的節點,

typedef union epoll_dataepoll_data_t;

struct epoll_event這是乙個上下樹節點的全部資訊,包括事件和檔案描述符;

於是使用者態定義接收的陣列也應該是這種型別的結構體

events:可以選擇的事件有很多,包括寫讀什麼的;

epollin寫事件;

epollout讀事件;

返回值:成功返回0,失敗返回1

3、監聽

#include

int epoll_wait(int efd, srtuct epoll_event *ev,int maxevent,int timeout)

功能:引數:

epfd 樹的根節點

ev:用於接收樹變化struct epoll_event陣列元素首位址

maxevent:陣列元素的個數

timeout:-1永久監聽;>0限時間

返回值:變化檔案描述符的個數

使用案例:

int main(int argc, char **ar**)

else if(fd == lfd && evs[i].events&epollout) 同理寫事件

else}}

}}//處理

}

epoll相關函式

int epoll create int size int epoll ctl int epfd,int op,int fd,struct epoll event event 由 epoll create 生成的epoll專用的檔案描述符 要進行的操作例如註冊事件,可能的取值epoll ctl ad...

IO復用 epoll函式

由於poll 和select 的侷限,2.6核心以上引用了event poll機制 就是說的epoll 雖然比前2個實現複雜得多,epoll解決了它們共有的基本效能問題,並增加了新的特性。poll 和select 每次呼叫的時候,都需要所有被監聽的檔案的描述符。核心必須遍歷所有被監視的檔案描述符。當...

linux下的epoll函式

epoll是tcp ip網路程式設計的io服用方法之中一種優於select的函式,相比select,它有兩個優點 1.無需編寫以監視狀態變化為目的的針對所有檔案描述符的迴圈語句。2.呼叫對應於select函式的epoll wait函式時無需每次傳遞監視物件資訊。下面介紹epoll伺服器端實現中需要的...